lib/solusvm/cli/base_cli.rb in solusvm-1.0.0.beta3 vs lib/solusvm/cli/base_cli.rb in solusvm-1.1.0.beta1

- old
+ new

@@ -4,11 +4,11 @@ require 'solusvm/version' module Solusvm class BaseCli < Thor include Thor::Actions - + class << self # Overrides the default banner implementation to output the whole command def banner(task, namespace = true, subcommand = false) "#{self.namespace.split(":").join(" ")} #{task.formatted_usage(self, false, false)}" end @@ -38,16 +38,41 @@ @@yaml[key.to_s] end end # Default required options - class_option :api_login, :type => :string, :desc => "API ID; Required.", :aliases => ["-I", "--api-login"], :default => default_option(:id) - class_option :api_key, :type => :string, :desc => "API KEY; Required.", :aliases => ["-K", "--api-key"], :default => default_option(:key) - class_option :api_url, :type => :string, :desc => "API URL; Required.", :aliases => ["-U", "--api-url"], :default => default_option(:url) + class_option :api_login, :type => :string, :desc => "API ID; Required.", :aliases => ["-I", "--api-login"] + class_option :api_key, :type => :string, :desc => "API KEY; Required.", :aliases => ["-K", "--api-key"] + class_option :api_url, :type => :string, :desc => "API URL; Required.", :aliases => ["-U", "--api-url"] + no_tasks do + def api + raise NotImplementedError + end + + # prints one result element per line, in case it is a list + def output(result="", color=nil, force_new_line=(result.to_s !~ /( |\t)$/)) + if api.successful? + Array(result).each do |entry| + say(entry, color, force_new_line) + end + else + say("Request failed: #{api.statusmsg}", color, force_new_line) + end + end + end + protected def configure - Solusvm.config(options[:api_login], options[:api_key], :url => options[:api_url]) + Solusvm.config( + present_or_exit(:api_login, :id, "api_login required"), + present_or_exit(:api_key, :key, "api_key required"), + :url => present_or_exit(:api_url, :url, "api_url required") + ) + end + + def present_or_exit(options_key, default_option_key, message) + options[options_key] || BaseCli.default_option(default_option_key) || (say(message) && raise(SystemExit)) end end end \ No newline at end of file