Skip to content

Commit

Permalink
add core selection dialog if more than one core is matched
Browse files Browse the repository at this point in the history
  • Loading branch information
achiurizo committed Oct 10, 2011
1 parent b7900ba commit c1f635f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/consular/cli.rb
Expand Up @@ -146,13 +146,33 @@ def delete(project = nil)
#
# @api private
def valid_core
core = Consular.cores.detect { |core| core.valid_system? }
unless core
cores = Consular.cores.select { |core| core.valid_system? }
if cores.nil? || cores.empty?
say "No valid core was found!. Exiting...", :red
raise SystemExit
elsif cores.size > 1
core_selection cores
else
cores.first
end
core
end

# Returns a dialoag to select the desired core.
#
# @param [Array<Core>] Array of cores
#
# @return [Core] core from selection
#
# @api private
def core_selection(cores)
say "Multiple cores match the current system:", :yellow
cores.each_with_index do |core, index|
say " [#{index}] - #{core.to_s}"
end
num = ask "Please select the number choice of the core to use: "
cores[num.to_i]
end

# Returns the first comment in file. This is used
# as the title when listing out the scripts.
#
Expand Down
24 changes: 24 additions & 0 deletions spec/cli_spec.rb
Expand Up @@ -49,9 +49,14 @@ def setup!; puts('setup'); end
FileUtils.touch '/tmp/Termfile'
FileUtils.touch Consular.global_path('foo.term')
FileUtils.touch Consular.global_path('foo.yml')
Consular.instance_variable_set(:@cores,[])
Consular.add_core FakeCore
end

after do
Consular.instance_variable_set(:@cores,[])
end

it "should start a Termfile" do
output = capture_io { Consular::CLI.start ['start', '-r=/tmp'] }.join('')
assert_match /process/, output
Expand All @@ -78,6 +83,13 @@ def setup!; puts('setup'); end
capture_io { Consular::CLI.start ['start', 'foo'] }.join('')
end
end

it "should bring up a core selection if more than one matching core" do
Consular::CLI.any_instance.expects(:core_selection).with(anything).returns(FakeCore)
Consular.add_core FakeCore
Consular.add_core FakeCore
assert capture_io { Consular::CLI.start ['start', 'foo'] }.join('')
end
end

describe "setup command" do
Expand All @@ -86,9 +98,14 @@ def setup!; puts('setup'); end
FileUtils.touch '/tmp/Termfile'
FileUtils.touch Consular.global_path('foo.term')
FileUtils.touch Consular.global_path('foo.yml')
Consular.instance_variable_set(:@cores,[])
Consular.add_core FakeCore
end

after do
Consular.instance_variable_set(:@cores,[])
end

it "should setup a Termfile" do
output = capture_io { Consular::CLI.start ['setup', '-r=/tmp'] }.join('')
assert_match /setup/, output
Expand All @@ -115,6 +132,13 @@ def setup!; puts('setup'); end
capture_io { Consular::CLI.start ['setup', 'foo'] }.join('')
end
end

it "should bring up a core selection if more than one matching core" do
Consular::CLI.any_instance.expects(:core_selection).with(anything).returns(FakeCore)
Consular.add_core FakeCore
Consular.add_core FakeCore
assert capture_io { Consular::CLI.start ['setup', 'foo'] }.join('')
end
end

it "init creates a new global script directory and consularc" do
Expand Down

0 comments on commit c1f635f

Please sign in to comment.