Permalink
Browse files

add core selection dialog if more than one core is matched

  • Loading branch information...
1 parent b7900ba commit c1f635f2019224cfb6d6f4c153299bd7f24d96b2 @achiu committed Oct 10, 2011
Showing with 47 additions and 3 deletions.
  1. +23 −3 lib/consular/cli.rb
  2. +24 −0 spec/cli_spec.rb
View
@@ -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.
#
View
@@ -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
@@ -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
@@ -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
@@ -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

0 comments on commit c1f635f

Please sign in to comment.