From c7ebb220bc79d3a84e55745ed18d0d7b5baeacdd Mon Sep 17 00:00:00 2001 From: elijah Date: Tue, 21 Jun 2016 17:49:42 -0700 Subject: remove highline gem dependency --- bin/leap | 12 ----- leap_cli.gemspec | 3 +- lib/leap_cli/bootstrap.rb | 1 + lib/leap_cli/commands/common.rb | 104 ++++++++++++++++++++++++++++++++++++++++ lib/leap_cli/commands/new.rb | 39 ++++++++------- lib/leap_cli/commands/pre.rb | 4 +- 6 files changed, 132 insertions(+), 31 deletions(-) create mode 100644 lib/leap_cli/commands/common.rb diff --git a/bin/leap b/bin/leap index 9cd3518..7247168 100755 --- a/bin/leap +++ b/bin/leap @@ -27,8 +27,6 @@ rescue LoadError end require 'gli' -require 'highline' -require 'forwardable' require 'leap_cli/lib_ext/gli' # our custom extensions to gli # @@ -40,16 +38,6 @@ require 'leap_cli/lib_ext/gli' # our custom extensions to gli # module LeapCli::Commands extend GLI::App - extend Forwardable - - # delegate highline methods to make them available to sub-commands - @terminal = HighLine.new - def_delegator :@terminal, :ask, 'self.ask' - def_delegator :@terminal, :agree, 'self.agree' - def_delegator :@terminal, :choose, 'self.choose' - def_delegator :@terminal, :say, 'self.say' - def_delegator :@terminal, :color, 'self.color' - def_delegator :@terminal, :list, 'self.list' # make config manager available as 'manager' def self.manager diff --git a/leap_cli.gemspec b/leap_cli.gemspec index 104cb0b..fc3131f 100644 --- a/leap_cli.gemspec +++ b/leap_cli.gemspec @@ -49,10 +49,9 @@ spec = Gem::Specification.new do |s| #s.add_development_dependency('aruba') # console gems - s.add_runtime_dependency('gli','~> 2.12', '>= 2.12.0') + s.add_runtime_dependency('gli','~> 2.14', '>= 2.14.0') # note: gli version is also pinned in leap_cli.rb. s.add_runtime_dependency('command_line_reporter', '~> 3.3') - s.add_runtime_dependency('highline', '~> 1.6') s.add_runtime_dependency('paint', '~> 0.9') # network gems diff --git a/lib/leap_cli/bootstrap.rb b/lib/leap_cli/bootstrap.rb index a6b1759..9ccb3dd 100644 --- a/lib/leap_cli/bootstrap.rb +++ b/lib/leap_cli/bootstrap.rb @@ -6,6 +6,7 @@ module LeapCli module Bootstrap extend self + extend LeapCli::LogCommand # # the argument leapfile_path is only used for tests diff --git a/lib/leap_cli/commands/common.rb b/lib/leap_cli/commands/common.rb new file mode 100644 index 0000000..695a9f6 --- /dev/null +++ b/lib/leap_cli/commands/common.rb @@ -0,0 +1,104 @@ +require 'readline' + +module LeapCli; module Commands + + extend LeapCli::LogCommand + extend LeapCli::Util + extend LeapCli::Util::RemoteCommand + + def path(name) + Path.named_path(name) + end + + # + # keeps prompting the user for a numbered choice, until they pick a good one or bail out. + # + # block is yielded and is responsible for rendering the choices. + # + def numbered_choice_menu(msg, items, &block) + while true + say("\n" + msg + ':') + items.each_with_index(&block) + say("q. quit") + index = ask("number 1-#{items.length}> ") + if index.empty? + next + elsif index =~ /q/ + bail! + else + i = index.to_i - 1 + if i < 0 || i >= items.length + bail! + else + return i + end + end + end + end + + def parse_node_list(nodes) + if nodes.is_a? Config::Object + Config::ObjectList.new(nodes) + elsif nodes.is_a? Config::ObjectList + nodes + elsif nodes.is_a? String + manager.filter!(nodes) + else + bail! "argument error" + end + end + + def say(statement) + if ends_in_whitespace?(statement) + $stdout.print(statement) + $stdout.flush + else + $stdout.puts(statement) + end + end + + def ask(question, options={}) + default = options[:default] + if default + if ends_in_whitespace?(question) + question = question + "|" + default + "| " + else + question = question + "|" + default + "|" + end + end + response = Readline.readline(question, true) # set to false if ever reading passwords. + if response + response = response.strip + if response.empty? + return default + else + return response + end + else + return default + end + end + + def agree(question, options={}) + while true + response = ask(question, options) + if response.nil? + say('Please enter "yes" or "no".') + elsif ["y","yes", "ye"].include?(response.downcase) + return true + elsif ["n", "no"].include?(response.downcase) + return false + else + say('Please enter "yes" or "no".') + end + end + end + + private + + # true if str ends in whitespace before a color escape code. + def ends_in_whitespace?(str) + /[ \t](\e\[\d+(;\d+)*m)?\Z/ =~ str + end + +end; end diff --git a/lib/leap_cli/commands/new.rb b/lib/leap_cli/commands/new.rb index 5c9fd74..6b60e7d 100644 --- a/lib/leap_cli/commands/new.rb +++ b/lib/leap_cli/commands/new.rb @@ -4,7 +4,6 @@ module LeapCli; module Commands desc 'Creates a new provider instance in the specified directory, creating it if necessary.' arg_name 'DIRECTORY' - #skips_pre command :new do |c| c.flag 'name', :desc => "The name of the provider." #, :default_value => 'Example' c.flag 'domain', :desc => "The primary domain of the provider." #, :default_value => 'example.org' @@ -12,19 +11,7 @@ module LeapCli; module Commands c.flag 'contacts', :desc => "Default email address contacts." #, :default_value => 'root' c.action do |global, options, args| - unless args.first - # this should not be needed, but GLI is not making it required. - bail! "Argument DIRECTORY is required." - end - directory = File.expand_path(args.first) - create_provider_directory(global, directory) - options[:domain] ||= ask_string("The primary domain of the provider: ") {|q| q.default = 'example.org'} - options[:name] ||= ask_string("The name of the provider: ") {|q| q.default = 'Example'} - options[:platform] ||= ask_string("File path of the leap_platform directory: ") {|q| q.default = File.expand_path('../leap_platform', directory)} - options[:platform] = "./" + options[:platform] unless options[:platform] =~ /^\// - options[:contacts] ||= ask_string("Default email address contacts: ") {|q| q.default = 'root@' + options[:domain]} - options[:platform] = relative_path(options[:platform]) - create_initial_provider_files(directory, global, options) + new_provider_action(global, options, args) end end @@ -32,13 +19,33 @@ module LeapCli; module Commands DEFAULT_REPO = 'https://leap.se/git/leap_platform.git' + def new_provider_action(global, options, args) + unless args.first + # this should not be needed, but GLI is not making it required. + bail! "Argument DIRECTORY is required." + end + directory = File.expand_path(args.first) + create_provider_directory(global, directory) + options[:domain] ||= ask_string("The primary domain of the provider: ", + default: 'example.org') + options[:name] ||= ask_string("The name of the provider: ", + default: 'Example') + options[:platform] ||= ask_string("File path of the leap_platform directory: ", + default: File.expand_path('../leap_platform', directory)) + options[:platform] = "./" + options[:platform] unless options[:platform] =~ /^\// + options[:contacts] ||= ask_string("Default email address contacts: ", + default: 'root@' + options[:domain]) + options[:platform] = relative_path(options[:platform]) + create_initial_provider_files(directory, global, options) + end + # # don't let the user specify any of the following: y, yes, n, no # they must actually input a real string # - def ask_string(str, &block) + def ask_string(str, options={}) while true - value = ask(str, &block) + value = ask(str, options) if value =~ /^(y|yes|n|no)$/i say "`#{value}` is not a valid value. Try again" else diff --git a/lib/leap_cli/commands/pre.rb b/lib/leap_cli/commands/pre.rb index f4bf7bb..0b7e98b 100644 --- a/lib/leap_cli/commands/pre.rb +++ b/lib/leap_cli/commands/pre.rb @@ -1,9 +1,11 @@ - # # check to make sure we can find the root directory of the platform # module LeapCli; module Commands + extend self # this is a trick to make all instance methods + # available as class methods. + desc 'Verbosity level 0..5' arg_name 'LEVEL' default_value '1' -- cgit v1.2.3