Skip to content

Commit 764fb36

Browse files
committed
- Add support for reading config from stdin and writing to stdout
1 parent 830f425 commit 764fb36

File tree

7 files changed

+53
-23
lines changed

7 files changed

+53
-23
lines changed

lib/completely/commands/base.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,25 @@ def script
3535
end
3636

3737
def completions
38-
@completions ||= Completions.load(config_path, function_name: args['--function'])
38+
@completions ||= if config_path == '-'
39+
raise Error, "Nothing is piped on stdin" if $stdin.tty?
40+
41+
Completions.read $stdin, function_name: args['--function']
42+
else
43+
Completions.load config_path, function_name: args['--function']
44+
end
3945
end
4046

4147
def config_path
4248
@config_path ||= args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml'
4349
end
4450

4551
def output_path
46-
@output_path ||= args['OUTPUT_PATH'] || ENV['COMPLETELY_OUTPUT_PATH'] || "#{config_basename}.bash"
52+
@output_path ||= args['OUTPUT_PATH'] || ENV['COMPLETELY_OUTPUT_PATH'] || stdout || "#{config_basename}.bash"
53+
end
54+
55+
def stdout
56+
@stdout ||= config_path == '-' ? '-' : nil
4757
end
4858

4959
def config_basename

lib/completely/commands/generate.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Completely
44
module Commands
55
class Generate < Base
6-
help 'Generate the bash completion script to a file'
6+
help 'Generate the bash completion script'
77

88
usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]'
99
usage 'completely generate (-h|--help)'
@@ -12,10 +12,19 @@ class Generate < Base
1212
option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the ' \
1313
'script. This is useful if you wish to embed it directly in your script.'
1414

15-
param_config_path
15+
param 'CONFIG_PATH', <<~USAGE
16+
Path to the YAML configuration file [default: completely.yaml].
17+
Use '-' to read from stdin.
18+
19+
Can also be set by an environment variable.
20+
USAGE
21+
1622
param 'OUTPUT_PATH', <<~USAGE
1723
Path to the output bash script.
18-
When not provided, the name of the input file will be used with a .bash extension.
24+
Use '-' for stdout.
25+
26+
When not provided, the name of the input file will be used with a .bash extension, unless the input is stdin - in this case the default will be to output to stdout.
27+
1928
Can also be set by an environment variable.
2029
USAGE
2130

@@ -26,8 +35,12 @@ class Generate < Base
2635
def run
2736
wrap = args['--wrap']
2837
output = wrap ? wrapper_function(wrap) : script
29-
File.write output_path, output
30-
say "Saved m`#{output_path}`"
38+
if output_path == '-'
39+
puts output
40+
else
41+
File.write output_path, output
42+
say "Saved m`#{output_path}`"
43+
end
3144
syntax_warning unless completions.valid?
3245
end
3346

lib/completely/completions.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ class Completions
66
attr_reader :config
77

88
class << self
9-
def load(config_path, function_name: nil)
10-
config = Config.load config_path
11-
new config, function_name: function_name
9+
def load(path, function_name: nil)
10+
new Config.load(path), function_name: function_name
11+
end
12+
13+
def read(io, function_name: nil)
14+
new Config.read(io), function_name: function_name
1215
end
1316
end
1417

lib/completely/config.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ class Config
33
attr_reader :config, :options
44

55
class << self
6-
def load(config_path)
7-
begin
8-
config = YAML.load_file config_path, aliases: true
9-
rescue ArgumentError
10-
# :nocov:
11-
config = YAML.load_file config_path
12-
# :nocov:
13-
end
14-
15-
new config
6+
def parse(str)
7+
new YAML.load(str, aliases: true)
8+
rescue Psych::Exception => e
9+
raise ParseError, "Invalid YAML: #{e.message}"
1610
end
11+
12+
def load(path) = parse(File.read(path))
13+
def read(io) = parse(io.read)
1714
end
1815

1916
def initialize(config)

lib/completely/exceptions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Completely
22
class Error < StandardError; end
33
class InstallError < Error; end
4+
class ParseError < Error; end
45
end

spec/approvals/cli/commands

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Completely - Bash Completions Generator
33
Commands:
44
init Create a new sample YAML configuration file
55
preview Generate the bash completion script to STDOUT
6-
generate Generate the bash completion script to a file
6+
generate Generate the bash completion script
77
test Test completions
88
install Install a bash completion script
99
uninstall Uninstall a bash completion script

spec/approvals/cli/generate/help

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Generate the bash completion script to a file
1+
Generate the bash completion script
22

33
Usage:
44
completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]
@@ -18,12 +18,18 @@ Options:
1818
Parameters:
1919
CONFIG_PATH
2020
Path to the YAML configuration file [default: completely.yaml].
21+
Use '-' to read from stdin.
22+
2123
Can also be set by an environment variable.
2224

2325
OUTPUT_PATH
2426
Path to the output bash script.
27+
Use '-' for stdout.
28+
2529
When not provided, the name of the input file will be used with a .bash
26-
extension.
30+
extension, unless the input is stdin - in this case the default will be to
31+
output to stdout.
32+
2733
Can also be set by an environment variable.
2834

2935
Environment Variables:

0 commit comments

Comments
 (0)