Skip to content

Commit b575d27

Browse files
authored
Merge pull request #19 from DannyBen/add/config-path-env-var
Add in/out path configuration via environment variables
2 parents 31e2da3 + daf27be commit b575d27

File tree

23 files changed

+222
-49
lines changed

23 files changed

+222
-49
lines changed

lib/completely/commands/base.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ module Commands
55
class Base < MisterBin::Command
66

77
class << self
8-
def config_path_usage
9-
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]"
8+
def param_config_path
9+
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
1010
end
1111

12-
def function_usage
12+
def option_function
1313
option "-f --function NAME", "Modify the name of the function in the generated script"
1414
end
1515

16-
def debug_usage
16+
def environment_config_path
17+
environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
18+
end
19+
20+
def environment_debug
1721
environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested"
1822
end
1923
end
@@ -29,7 +33,15 @@ def completions
2933
end
3034

3135
def config_path
32-
args['CONFIG_PATH'] || 'completely.yaml'
36+
@config_path ||= args['CONFIG_PATH'] || ENV['COMPLETELY_CONFIG_PATH'] || 'completely.yaml'
37+
end
38+
39+
def script_path
40+
@script_path ||= args['SCRIPT_PATH'] || ENV['COMPLETELY_SCRIPT_PATH'] || "#{config_basename}.bash"
41+
end
42+
43+
def config_basename
44+
File.basename config_path, File.extname(config_path)
3345
end
3446

3547
def syntax_warning

lib/completely/commands/generate.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ module Commands
55
class Generate < Base
66
help "Generate the bash completion script to a file"
77

8-
usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]"
8+
usage "completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]"
99
usage "completely generate (-h|--help)"
1010

11-
function_usage
11+
option_function
1212
option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script"
1313

14-
config_path_usage
15-
param "OUTPUT_PATH", "Path to the output bash script [default: completely.bash]"
16-
debug_usage
14+
param_config_path
15+
param "SCRIPT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable"
16+
17+
environment_config_path
18+
environment "COMPLETELY_SCRIPT_PATH", "Path to the output bash script"
19+
environment_debug
1720

1821
def run
1922
wrap = args['--wrap']
2023
output = wrap ? wrapper_function(wrap) : script
21-
File.write output_path, output
22-
say "Saved !txtpur!#{output_path}"
24+
File.write script_path, output
25+
say "Saved !txtpur!#{script_path}"
2326
syntax_warning unless completions.valid?
2427
end
2528

@@ -29,10 +32,6 @@ def wrapper_function(wrapper_name)
2932
completions.wrapper_function wrapper_name
3033
end
3134

32-
def output_path
33-
@output_path ||= args['OUTPUT_PATH'] || "completely.bash"
34-
end
35-
3635
end
3736
end
3837
end

lib/completely/commands/init.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class Init < Base
88
usage "completely init [CONFIG_PATH]"
99
usage "completely init (-h|--help)"
1010

11-
config_path_usage
11+
param_config_path
12+
environment_config_path
1213

1314
def run
1415
if File.exist? config_path

lib/completely/commands/preview.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ class Preview < Base
88
usage "completely preview [CONFIG_PATH --function NAME]"
99
usage "completely preview (-h|--help)"
1010

11-
function_usage
12-
config_path_usage
13-
debug_usage
11+
option_function
12+
param_config_path
13+
environment_config_path
14+
environment_debug
1415

1516
def run
1617
puts script

lib/completely/commands/test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ class Test < Base
1818

1919
param "COMPLINE", "The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space."
2020

21-
environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
21+
environment_config_path
2222
environment "COMPLETELY_SCRIPT_PATH", "Path to a completions script. When set, this script will be tested instead of the completely configuration file"
2323
environment "COMPLETELY_SCRIPT_FUNCTION", "The main completion function to call when using a custom script. If not set, the basename of the script path will be used, prefixed by an underscore"
24-
debug_usage
24+
environment_debug
2525

2626
example %q[completely test "mygit pu"]
2727
example %q[completely test "mygit pull "]

lib/completely/templates/template.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<%= function_name %>() {
88
local cur compline
99
_init_completion -s || return
10+
1011
cur=${COMP_WORDS[COMP_CWORD]}
1112
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1213

@@ -20,7 +21,10 @@
2021
case "$compline" in
2122
% patterns.each do |pattern|
2223
% next if pattern.empty?
23-
<%= pattern.case_string %>) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;;
24+
<%= pattern.case_string %>)
25+
COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur"))
26+
;;
27+
2428
% end
2529
esac
2630
} &&
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Saved hello.bash
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Saved spec/tmp/tada.bash

spec/approvals/cli/generate/help

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Generate the bash completion script to a file
22

33
Usage:
4-
completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]
4+
completely generate [CONFIG_PATH SCRIPT_PATH --function NAME --wrap NAME]
55
completely generate (-h|--help)
66

77
Options:
@@ -18,11 +18,20 @@ Options:
1818
Parameters:
1919
CONFIG_PATH
2020
Path to the YAML configuration file [default: completely.yaml]
21+
Can also be set by an environment variable
2122

22-
OUTPUT_PATH
23-
Path to the output bash script [default: completely.bash]
23+
SCRIPT_PATH
24+
Path to the output bash script. When not provided, the name of the input
25+
file will be used with a .bash extension
26+
Can also be set by an environment variable
2427

2528
Environment Variables:
29+
COMPLETELY_CONFIG_PATH
30+
Path to a completely configuration file [default: completely.yaml]
31+
32+
COMPLETELY_SCRIPT_PATH
33+
Path to the output bash script
34+
2635
COMPLETELY_DEBUG
2736
It not empty, the generated script will include an additional debugging
2837
snippet that outputs the compline and current word to a text file when a

spec/approvals/cli/generated-script

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,27 @@
77
_mygit_completions() {
88
local cur compline
99
_init_completion -s || return
10+
1011
cur=${COMP_WORDS[COMP_CWORD]}
1112
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1213

1314
case "$compline" in
14-
'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;
15-
'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;
16-
'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;
17-
*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
15+
'status'*)
16+
COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))
17+
;;
18+
19+
'commit'*)
20+
COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))
21+
;;
22+
23+
'init'*)
24+
COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))
25+
;;
26+
27+
*)
28+
COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))
29+
;;
30+
1831
esac
1932
} &&
2033
complete -F _mygit_completions mygit

0 commit comments

Comments
 (0)