Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ for the auto complete process.
You can save a sample YAML file by running:

```
$ completely new
$ completely init
```

This will generate a file named `completely.yaml` with this content:
Expand Down
14 changes: 12 additions & 2 deletions lib/completely/cli.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
require 'mister_bin'
require 'completely/command'
require 'completely/commands/init'
require 'completely/commands/preview'
require 'completely/commands/generate'

module Completely
class CLI
def self.runner
MisterBin::Runner.new handler: Command
runner = MisterBin::Runner.new version: Completely::VERSION,
header: "Completely - Bash Completions Generator",
footer: "Run !txtpur!completely COMMAND --help!txtrst! for more information"

runner.route 'init', to: Commands::Init
runner.route 'preview', to: Commands::Preview
runner.route 'generate', to: Commands::Generate

runner
end
end
end
89 changes: 0 additions & 89 deletions lib/completely/command.rb

This file was deleted.

37 changes: 37 additions & 0 deletions lib/completely/commands/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'mister_bin'

module Completely
module Commands
class Base < MisterBin::Command

class << self
def config_path_usage
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]"
end

def function_usage
option "-f --function NAME", "Modify the name of the function in the generated script"
end
end

protected

def script
@script ||= completions.script
end

def completions
@completions ||= Completions.load(config_path, function_name: args['--function'])
end

def config_path
args['CONFIG_PATH'] || 'completely.yaml'
end

def syntax_warning
say! "\n!txtred!WARNING:\nYour configuration is invalid."
say! "!txtred!All patterns must start with the same word."
end
end
end
end
37 changes: 37 additions & 0 deletions lib/completely/commands/generate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'completely/commands/base'

module Completely
module Commands
class Generate < Base
help "Generate the bash completion script to a file"

usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]"
usage "completely generate (-h|--help)"

function_usage
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"

config_path_usage
param "OUTPUT_PATH", "Path to the output bash script [default: completely.bash]"

def run
wrap = args['--wrap']
output = wrap ? wrapper_function(wrap) : script
File.write output_path, output
say "Saved !txtpur!#{output_path}"
syntax_warning unless completions.valid?
end

private

def wrapper_function(wrapper_name)
completions.wrapper_function wrapper_name
end

def output_path
@output_path ||= args['OUTPUT_PATH'] || "completely.bash"
end

end
end
end
34 changes: 34 additions & 0 deletions lib/completely/commands/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'completely/commands/base'

module Completely
module Commands
class Init < Base
help "Create a new sample YAML configuration file"

usage "completely init [CONFIG_PATH]"
usage "completely init (-h|--help)"

config_path_usage

def run
if File.exist? config_path
raise "File already exists: #{config_path}"
else
File.write config_path, sample
say "Saved !txtpur!#{config_path}"
end
end

private

def sample
@sample ||= File.read sample_path
end

def sample_path
@sample_path ||= File.expand_path "../sample.yaml", __dir__
end

end
end
end
20 changes: 20 additions & 0 deletions lib/completely/commands/preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'completely/commands/base'

module Completely
module Commands
class Preview < Base
help "Generate the bash completion script to STDOUT"

usage "completely preview [CONFIG_PATH --function NAME]"
usage "completely preview (-h|--help)"

function_usage
config_path_usage

def run
puts script
syntax_warning unless completions.valid?
end
end
end
end
8 changes: 8 additions & 0 deletions spec/approvals/cli/commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Completely - Bash Completions Generator

Commands:
init Create a new sample YAML configuration file
preview Generate the bash completion script to STDOUT
generate Generate the bash completion script to a file

Run completely COMMAND --help for more information
File renamed without changes.
23 changes: 23 additions & 0 deletions spec/approvals/cli/generate/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Generate the bash completion script to a file

Usage:
completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]
completely generate (-h|--help)

Options:
-f --function NAME
Modify the name of the function in the generated script

-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

-h --help
Show this help

Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]

OUTPUT_PATH
Path to the output bash script [default: completely.bash]
1 change: 1 addition & 0 deletions spec/approvals/cli/generate/wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Saved completely.bash
47 changes: 0 additions & 47 deletions spec/approvals/cli/help

This file was deleted.

File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions spec/approvals/cli/init/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Create a new sample YAML configuration file

Usage:
completely init [CONFIG_PATH]
completely init (-h|--help)

Options:
-h --help
Show this help

Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
File renamed without changes.
16 changes: 16 additions & 0 deletions spec/approvals/cli/preview/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Generate the bash completion script to STDOUT

Usage:
completely preview [CONFIG_PATH --function NAME]
completely preview (-h|--help)

Options:
-f --function NAME
Modify the name of the function in the generated script

-h --help
Show this help

Parameters:
CONFIG_PATH
Path to the YAML configuration file [default: completely.yaml]
5 changes: 0 additions & 5 deletions spec/approvals/cli/usage

This file was deleted.

4 changes: 4 additions & 0 deletions spec/completely/bin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
describe 'bin/completely' do
subject { CLI.runner }

it "shows list of commands" do
expect{ subject.run }.to output_approval('cli/commands')
end

context "on error" do
it "displays it nicely" do
expect(`bin/completely preview notfound.yaml 2>&1`).to match_approval('cli/error')
Expand Down
Loading