require "magellan/cli" require 'thor' require 'active_support/core_ext/string/inflections' module Magellan module Cli class Command < Base class << self # override Thor::Base.start method def start(given_args = ARGV, config = {}) # class_options verbose and version are defined in Magellan::Cli::Base if ARGV.include?("-v") || ARGV.include?("--version") info(File.basename($0) << " " << Magellan::Cli::VERSION) end begin super(given_args, config) rescue Magellan::Cli::Error => e error(e.message) exit(1) rescue => e error("[#{e.class}] #{e.message}") verbose(" " << e.backtrace.join("\n "), ARGV.include?("-V") || ARGV.include?("--verbose")) exit(1) end end # overwrite Magellan::Cli::Base.help method def help(shell, subcommand = false) super(shell, subcommand) shell.say shell.say "RESOURCES:" shell.say " " << RESOURCES.keys.join(", ") shell.say " type `#{File.basename($0)} help RESOURCE` for more detail" shell.say end end RESOURCES =\ { "Organization" => "organization", "Team" => "team", "Project" => "project", "Stage" => "stage", "ClientVersion" => "client_version", #"TransactionRouter" => "tr", "Worker" => "worker", "Image" => "image", "Container" => "container", "Cloudsql" => "cloudsql", } RESOURCES.each do |classname, name| desc "#{name} SUBCOMMAND ...ARGS", "manage #{name.pluralize}" subcommand name, ::Magellan::Cli::Resources.const_get(classname) end COMMAND_ORDER = %w[login] + RESOURCES.values #desc "direct SUBCOMMAND ...ARGS", "send request directly" #subcommand "direct", ::Magellan::Cli::Direct desc "login", "login to the Magellan server" method_option :email, aliases: "-e", desc: "email address for login" method_option :password, aliases: "-p", desc: "password for login" def login unless email = options[:email] print "email: " email = STDIN.gets.strip end unless password = options[:password] print "password: " password = STDIN.noecho(&:gets).chomp puts "" end Magellan::Cli::Http.new.login!(email, password) end end end end