Skip to content

Commit 4997c1f

Browse files
authored
Merge pull request #318 from DannyBen/add/bashly-doc
Add `bashly doc` command to show reference in the terminal
2 parents 5701dbd + 47539d3 commit 4997c1f

File tree

13 files changed

+1576
-0
lines changed

13 files changed

+1576
-0
lines changed

lib/bashly/cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def self.runner
1414
runner.route 'validate', to: Commands::Validate
1515
runner.route 'generate', to: Commands::Generate
1616
runner.route 'add', to: Commands::Add
17+
runner.route 'doc', to: Commands::Doc
1718

1819
runner
1920
end

lib/bashly/commands/doc.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module Bashly
2+
module Commands
3+
class Doc < Base
4+
summary 'Show bashly reference documentation'
5+
help 'This command displays bite-sized help for all the bashly configuration options in the terminal.'
6+
7+
usage 'bashly doc [SEARCH] [--index]'
8+
usage 'bashly doc (-h|--help)'
9+
10+
option '-i --index', 'Show option keys only'
11+
param 'SEARCH', 'Search for options that match this text'
12+
13+
example 'bashly doc command'
14+
example 'bashly doc command.flags'
15+
example 'bashly doc flag. -i'
16+
example 'bashly doc catch_all'
17+
18+
def run
19+
if args['--index']
20+
puts data.keys
21+
else
22+
show
23+
end
24+
end
25+
26+
private
27+
28+
def show
29+
data.each do |key, info|
30+
show_key key
31+
show_help info['help']
32+
show_example info['example'] if info['example']
33+
show_url info['url'] if info['url']
34+
end
35+
end
36+
37+
def show_key(key)
38+
say "!txtgrn!#{key}"
39+
say ''
40+
end
41+
42+
def show_url(url)
43+
say " See !undblu!#{url}"
44+
say ''
45+
end
46+
47+
def show_example(example)
48+
example = word_wrap " #{example}"
49+
example.gsub!(/^(\s*- )?(\s*\w+):/, '\1!txtblu!\2!txtrst!:')
50+
example.gsub!(/^(\s*- )/, '!txtylw!\1!txtrst!')
51+
example.gsub!(/^(\s*#.+)/, '!txtpur!\1!txtrst!')
52+
say example
53+
say ''
54+
end
55+
56+
def show_help(help)
57+
help = word_wrap " #{help}"
58+
help.gsub!(/`([^`]+)`/, '!txtgrn!\1!txtrst!')
59+
say help
60+
say ''
61+
end
62+
63+
def data
64+
return raw_data unless args['SEARCH']
65+
66+
result = raw_data.select { |k, _v| k.== args['SEARCH'] }
67+
return result if result.any?
68+
69+
result = raw_data.select { |k, _v| k.include? args['SEARCH'] }
70+
return result if result.any?
71+
72+
raise Error, "No match"
73+
end
74+
75+
def raw_data
76+
@raw_data ||= begin
77+
result = {}
78+
Dir["#{docs_dir}/*.yml"].sort.each do |path|
79+
result.merge! YAML.load_file(path)
80+
end
81+
result
82+
end
83+
end
84+
85+
def docs_dir
86+
@docs_dir ||= File.expand_path '../docs', __dir__
87+
end
88+
end
89+
end
90+
end

lib/bashly/docs/arg.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
arg:
2+
help: Define positional arguments.
3+
url: https://bashly.dannyb.co/configuration/argument/
4+
example: |-
5+
args:
6+
- name: user
7+
help: AWS Username.
8+
required: true
9+
10+
- name: role
11+
help: User role.
12+
default: admin
13+
allowed:
14+
- admin
15+
- guest
16+
17+
arg.allowed:
18+
help: Specify a list of allowed values. Can be used in conjunction with `default` and `required`.
19+
url: https://bashly.dannyb.co/configuration/argument/#allowed
20+
example: |-
21+
args:
22+
- name: region
23+
help: Region to connect to
24+
25+
# allowed + required
26+
allowed: [eu, us]
27+
required: true
28+
29+
- name: environment
30+
help: Environment to connect to
31+
32+
# allowed + default
33+
allowed: [stage, production, development]
34+
default: development
35+
36+
arg.default:
37+
help: Specify the value to apply when not provided by the user.
38+
url: https://bashly.dannyb.co/configuration/argument/#default
39+
example: |-
40+
args:
41+
- name: images
42+
help: Image files to convert.
43+
default: "*.jpg"
44+
45+
arg.help:
46+
help: Specify the help message for this argument.
47+
url: https://bashly.dannyb.co/configuration/argument/#help
48+
example: |-
49+
args:
50+
- name: user
51+
help: AWS Username.
52+
required: true
53+
54+
arg.name:
55+
help: Specify the lowercase name of the argument.
56+
url: https://bashly.dannyb.co/configuration/argument/#name
57+
example: |-
58+
args:
59+
- name: user
60+
help: AWS Username.
61+
required: true
62+
63+
arg.repeatable:
64+
help: |-
65+
Specify that this argument can be provided multiple times.
66+
67+
The argument below will be received as a space-delimited string which needs to be converted to an array with:
68+
`eval "data=(${args[file]})"`
69+
70+
url: https://bashly.dannyb.co/configuration/argument/#repeatable
71+
example: |-
72+
args:
73+
- name: file
74+
help: One or more files to process
75+
required: true
76+
repeatable: true
77+
78+
arg.required:
79+
help: Specify that this argument is required.
80+
url: https://bashly.dannyb.co/configuration/argument/#required
81+
example: |-
82+
args:
83+
- name: region
84+
help: Region to connect to
85+
required: true
86+
87+
arg.validate:
88+
help: Apply custom validation functions.
89+
90+
url: https://bashly.dannyb.co/configuration/argument/#validate
91+
example: |-
92+
args:
93+
- name: path
94+
validate: file_exists

0 commit comments

Comments
 (0)