Skip to content

Commit 1fced36

Browse files
committed
- Refactor CLI commands
1 parent ded17e1 commit 1fced36

File tree

25 files changed

+343
-265
lines changed

25 files changed

+343
-265
lines changed

lib/completely/cli.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
require 'mister_bin'
2-
require 'completely/command'
2+
require 'completely/commands/init'
3+
require 'completely/commands/preview'
4+
require 'completely/commands/generate'
35

46
module Completely
57
class CLI
68
def self.runner
7-
MisterBin::Runner.new handler: Command
9+
runner = MisterBin::Runner.new version: Completely::VERSION,
10+
header: "Completely - Bash Completions Generator",
11+
footer: "Run !txtpur!completely COMMAND --help!txtrst! for more information"
12+
13+
runner.route 'init', to: Commands::Init
14+
runner.route 'preview', to: Commands::Preview
15+
runner.route 'generate', to: Commands::Generate
16+
17+
runner
818
end
919
end
1020
end

lib/completely/command.rb

Lines changed: 0 additions & 89 deletions
This file was deleted.

lib/completely/commands/base.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'mister_bin'
2+
3+
module Completely
4+
module Commands
5+
class Base < MisterBin::Command
6+
7+
class << self
8+
def config_path_usage
9+
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]"
10+
end
11+
12+
def function_usage
13+
option "-f --function NAME", "Modify the name of the function in the generated script"
14+
end
15+
end
16+
17+
protected
18+
19+
def script
20+
@script ||= completions.script
21+
end
22+
23+
def completions
24+
@completions ||= Completions.load(config_path, function_name: args['--function'])
25+
end
26+
27+
def config_path
28+
args['CONFIG_PATH'] || 'completely.yaml'
29+
end
30+
31+
def syntax_warning
32+
say! "\n!txtred!WARNING:\nYour configuration is invalid."
33+
say! "!txtred!All patterns must start with the same word."
34+
end
35+
end
36+
end
37+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'completely/commands/base'
2+
3+
module Completely
4+
module Commands
5+
class Generate < Base
6+
help "Generate the bash completion script to a file"
7+
8+
usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]"
9+
usage "completely generate (-h|--help)"
10+
11+
function_usage
12+
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"
13+
14+
config_path_usage
15+
param "OUTPUT_PATH", "Path to the output bash script [default: completely.bash]"
16+
17+
def run
18+
wrap = args['--wrap']
19+
output = wrap ? wrapper_function(wrap) : script
20+
File.write output_path, output
21+
say "Saved !txtpur!#{output_path}"
22+
syntax_warning unless completions.valid?
23+
end
24+
25+
private
26+
27+
def wrapper_function(wrapper_name)
28+
completions.wrapper_function wrapper_name
29+
end
30+
31+
def output_path
32+
@output_path ||= args['OUTPUT_PATH'] || "completely.bash"
33+
end
34+
35+
end
36+
end
37+
end

lib/completely/commands/init.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'completely/commands/base'
2+
3+
module Completely
4+
module Commands
5+
class Init < Base
6+
help "Create a new sample YAML configuration file"
7+
8+
usage "completely init [CONFIG_PATH]"
9+
usage "completely init (-h|--help)"
10+
11+
config_path_usage
12+
13+
def run
14+
if File.exist? config_path
15+
raise "File already exists: #{config_path}"
16+
else
17+
File.write config_path, sample
18+
say "Saved !txtpur!#{config_path}"
19+
end
20+
end
21+
22+
private
23+
24+
def sample
25+
@sample ||= File.read sample_path
26+
end
27+
28+
def sample_path
29+
@sample_path ||= File.expand_path "../sample.yaml", __dir__
30+
end
31+
32+
end
33+
end
34+
end

lib/completely/commands/preview.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'completely/commands/base'
2+
3+
module Completely
4+
module Commands
5+
class Preview < Base
6+
help "Generate the bash completion script to STDOUT"
7+
8+
usage "completely preview [CONFIG_PATH --function NAME]"
9+
usage "completely preview (-h|--help)"
10+
11+
function_usage
12+
config_path_usage
13+
14+
def run
15+
puts script
16+
syntax_warning unless completions.valid?
17+
end
18+
end
19+
end
20+
end

spec/approvals/cli/commands

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Completely - Bash Completions Generator
2+
3+
Commands:
4+
init Create a new sample YAML configuration file
5+
preview Generate the bash completion script to STDOUT
6+
generate Generate the bash completion script to a file
7+
8+
Run completely COMMAND --help for more information
File renamed without changes.

0 commit comments

Comments
 (0)