Skip to content

Commit c5a877e

Browse files
committed
- Add support for generate --install
1 parent 85615b7 commit c5a877e

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/completely/commands/generate.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ class Generate < Base
66
help 'Generate the bash completion script to file or stdout'
77

88
usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]'
9+
usage 'completely generate [CONFIG_PATH --install PROGRAM --function NAME]'
910
usage 'completely generate (-h|--help)'
1011

1112
option_function
1213
option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the ' \
1314
'script. This is useful if you wish to embed it directly in your script.'
1415

16+
option '-i --install PROGRAM', 'Install the generated script as completions for PROGRAM.'
17+
1518
param 'CONFIG_PATH', <<~USAGE
1619
Path to the YAML configuration file [default: completely.yaml].
1720
Use '-' to read from stdin.
@@ -35,17 +38,34 @@ class Generate < Base
3538
def run
3639
wrap = args['--wrap']
3740
output = wrap ? wrapper_function(wrap) : script
41+
42+
if args['--install']
43+
install output
44+
else
45+
show_or_save output
46+
end
47+
end
48+
49+
private
50+
51+
def install(content)
52+
installer = Installer.from_string program: args['--install'], string: content
53+
success = installer.install force: true
54+
raise InstallError, "Failed running command:\nnb`#{installer.install_command_string}`" unless success
55+
say "Saved m`#{installer.target_path}`"
56+
say 'You may need to restart your session to test it'
57+
end
58+
59+
def show_or_save(content)
3860
if output_path == '-'
39-
puts output
61+
puts content
4062
else
41-
File.write output_path, output
63+
File.write output_path, content
4264
say "Saved m`#{output_path}`"
4365
end
4466
syntax_warning unless completions.valid?
4567
end
4668

47-
private
48-
4969
def wrapper_function(wrapper_name)
5070
completions.wrapper_function wrapper_name
5171
end

spec/approvals/cli/generate/help

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Generate the bash completion script to file or stdout
22

33
Usage:
44
completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]
5+
completely generate [CONFIG_PATH --install PROGRAM --function NAME]
56
completely generate (-h|--help)
67

78
Options:
@@ -12,6 +13,9 @@ Options:
1213
Wrap the completion script inside a function that echos the script. This is
1314
useful if you wish to embed it directly in your script.
1415

16+
-i --install PROGRAM
17+
Install the generated script as completions for PROGRAM.
18+
1519
-h --help
1620
Show this help
1721

0 commit comments

Comments
 (0)