Skip to content

Commit c525031

Browse files
authored
Merge pull request #282 from DannyBen/add/command-function
Add command function directive
2 parents 45781fc + bc29d58 commit c525031

File tree

6 files changed

+25
-2
lines changed

6 files changed

+25
-2
lines changed

lib/bashly/config_validator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def assert_command(key, value)
119119
assert_optional_string "#{key}.footer", value['footer']
120120
assert_optional_string "#{key}.group", value['group']
121121
assert_optional_string "#{key}.filename", value['filename']
122+
assert_optional_string "#{key}.function", value['function']
122123

123124
assert_boolean "#{key}.private", value['private']
124125
assert_boolean "#{key}.default", value['default']
@@ -142,6 +143,10 @@ def assert_command(key, value)
142143
assert_uniq "#{key}.flags", value['flags'], 'short'
143144
assert_uniq "#{key}.args", value['args'], 'name'
144145

146+
if value['function']
147+
assert value['function'].match(/^[a-z0-9_]+$/), "#{key}.function must contain lowercase alphanumeric characters and underscores only"
148+
end
149+
145150
if value['default']
146151
assert value['args'], "#{key}.default makes no sense without args"
147152
end

lib/bashly/script/command.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def option_keys
99
alias args catch_all commands completions
1010
default dependencies environment_variables examples
1111
extensible expose filename filters flags
12-
footer group help name
12+
footer function group help name
1313
private version
1414
short
1515
]
@@ -168,7 +168,7 @@ def flags
168168

169169
# Returns a unique name, suitable to be used in a bash function
170170
def function_name
171-
full_name.to_underscore
171+
options['function'] || full_name.to_underscore
172172
end
173173

174174
# Returns the name of the command, including its parent name (in case
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#<Bashly::ConfigurationError: root.function must contain lowercase alphanumeric characters and underscores only>

spec/bashly/script/command_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@
220220
it "returns the full name underscored" do
221221
expect(subject.function_name).to eq "docker_container_run"
222222
end
223+
224+
context "when function is provided by the user's config" do
225+
let(:fixture) { :custom_function }
226+
227+
it "returns the requested function name" do
228+
expect(subject.function_name).to eq "my_custom_function"
229+
end
230+
end
223231
end
224232

225233
describe '#full_name' do

spec/fixtures/script/commands.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
name: run
103103
filename: ops/run_command.sh
104104

105+
:custom_function:
106+
name: run
107+
function: my_custom_function
108+
105109
:custom_header:
106110
name: check
107111

spec/fixtures/script/validations.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
help: command.filters should be an array of strings
8787
filters: [1, 2]
8888

89+
:command_function:
90+
name: invalid
91+
help: command.function must match /[a-z0-9_]+/
92+
function: myCamelCaseFunction
93+
8994
:command_version:
9095
name: invalid
9196
help: version should be a string or a number

0 commit comments

Comments
 (0)