Skip to content

Commit

Permalink
(BOLT-196) Allow --nodes to be declared multiple times
Browse files Browse the repository at this point in the history
- Allow for the user to declare --nodes multiple times along a cli and merge them
  into a single array, previously it would allow for last nodes declaration to win and miss any previous
  calls on cli

- Ensure we are still having a uniq list after parsing nodes each pass

- As we now take nodes multiple times we need to ensure we are still getting a unique list after we have appended
  to the original list of items, previously this was on a single pass of nodes

Closes #103
  • Loading branch information
cyberious authored and joshcooper committed Oct 26, 2017
1 parent c59edaa commit 4b2f043
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/bolt/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ class CLI
attr_accessor :options

def initialize(argv)
@argv = argv
@options = {}
@argv = argv
@options = {
nodes: []
}

@parser = create_option_parser(@options)
end
Expand All @@ -111,7 +113,8 @@ def create_option_parser(results)
'* protocol is `ssh` by default, may be `ssh` or `winrm`',
'* port is `22` by default for SSH, `5985` for winrm (Optional)'
) do |nodes|
results[:nodes] = parse_nodes(nodes)
results[:nodes] += parse_nodes(nodes)
results[:nodes].uniq!
end
opts.on('-u', '--user USER',
"User to authenticate as (Optional)") do |user|
Expand Down Expand Up @@ -283,7 +286,7 @@ def validate(options)
"unknown argument(s) #{options[:leftovers].join(', ')}"
end

unless options[:nodes] || options[:mode] == 'plan'
unless !options[:nodes].empty? || options[:mode] == 'plan'
raise Bolt::CLIError, "option --nodes must be specified"
end

Expand Down
13 changes: 9 additions & 4 deletions spec/bolt/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
expect(cli.parse).to include(nodes: %w[foo bar])
end

it "accepts multiple nodes across multiple declarations" do
cli = Bolt::CLI.new(%w[command run --nodes foo,bar --nodes bar,more,bars])
expect(cli.parse).to include(nodes: %w[foo bar more bars])
end

it "reads from stdin when --nodes is '-'" do
nodes = <<NODES
foo
Expand Down Expand Up @@ -255,7 +260,7 @@
cli = Bolt::CLI.new(%w[plan run my::plan kj=2hv iuhg=iube 2whf=lcv
--modulepath .])
result = cli.parse
expect(result[:task_options]).to eq('kj' => '2hv',
expect(result[:task_options]).to eq('kj' => '2hv',
'iuhg' => 'iube',
'2whf' => 'lcv')
end
Expand All @@ -265,7 +270,7 @@
cli = Bolt::CLI.new(['plan', 'run', 'my::plan', '--params', json_args,
'--modulepath', '.'])
result = cli.parse
expect(result[:task_options]).to eq('kj' => '2hv',
expect(result[:task_options]).to eq('kj' => '2hv',
'iuhg' => 'iube',
'2whf' => 'lcv')
end
Expand All @@ -292,7 +297,7 @@
cli = Bolt::CLI.new(%W[plan run my::plan --params @#{file.path}
--modulepath .])
result = cli.parse
expect(result[:task_options]).to eq('kj' => '2hv',
expect(result[:task_options]).to eq('kj' => '2hv',
'iuhg' => 'iube',
'2whf' => 'lcv')
end
Expand All @@ -313,7 +318,7 @@
cli = Bolt::CLI.new(%w[plan run my::plan --params - --modulepath .])
allow(STDIN).to receive(:read).and_return(json_args)
result = cli.parse
expect(result[:task_options]).to eq('kj' => '2hv',
expect(result[:task_options]).to eq('kj' => '2hv',
'iuhg' => 'iube',
'2whf' => 'lcv')
end
Expand Down

0 comments on commit 4b2f043

Please sign in to comment.