require 'paint' require 'optparse' # A Ruby class to call the App42 REST API. module App42 module Command class Client # read access attr_reader :kclass attr_reader :command attr_reader :args attr_reader :options # @return [Object] def initialize(arg = []) @args = args @options = {} @should_exit = true @exception_msg = nil end # pre load all commands file def load_command_file Dir[File.join(File.dirname(__FILE__), "*.rb")].each { |file| require file } end # @param args # call for require files # Initiate client object def self.app42(*args ) begin action = args.shift.strip rescue 'help' App42::Command::Client.new.load_command_file App42::Command::Client.new.start(action, args) rescue Interrupt => e @exception_msg = e.message rescue OptionParser::MissingArgument => e @exception_msg = e.message rescue OptionParser::InvalidOption => e @exception_msg = e.message rescue OptionParser::AmbiguousOption => e @exception_msg = e.message rescue => error @exception_msg = e.message ensure puts Paint["#{@exception_msg}", :red] @should_exit = true if @should_exit.nil? exit @should_exit end end # Collect all the available options for all commands # Some duplicates exists to capture all scenarios def parse_options(command, args) opts_parser = OptionParser.new do |opts| opts.banner = "\nAvailable options:\n\n" opts.on('--api_key API_KEY') { |api| @options[:api] = api } opts.on('--secret_key SECRET_KEY') { |secret| @options[:secret] = secret } opts.on('-a API_KEY') { |api| @options[:api] = api } opts.on('-s SECRET_KEY') { |secret| @options[:secret] = secret } opts.on('--app NAME') { |name| @options[:name] = name } opts.on('--name NAME') { |name| @options[:name] = name } opts.on('--instance INSTANCE') { |instance| @options[:instance] = instance } opts.on('-i INSTANCE') { |instance| @options[:instance] = instance } opts.on('--service SERVICE') { |service| @options[:service] = service } opts.on('-s SERVICE') { |service| @options[:service] = service } opts.on('-h', '--help') { puts "#{App42::Base::Help.usage(command)}\n"; exit! } end opts_parser.parse!(args) self end # @param [Object] command # @param [Object] args def start(command, args = []) if is_available?(command) parse_options(command, args) execute command cmd = App42::Command.const_get(@kclass.to_s.capitalize) begin cmd.new(@options).send(@command) rescue Interrupt puts Paint[" Command cancelled.", :red] exit! rescue Exception => e puts e end elsif command == 'help' send(command) else puts Paint["app42: Unknown command [#{command}]", :red] App42::Base::Help.how_to end end # @return true OR false def is_available? command App42::Base::APP42_COMMAND.include?(command) end # choose action respective of class def execute command case command when 'version' set_cmd(:config, :version) when 'list' set_cmd(:config, :list) when 'apps' set_cmd(:app, :apps) when 'deploy' set_cmd(:app, :deploy) when 'setupInfra' set_cmd(:app, :setup_infra) when 'update' set_cmd(:app, :deploy) when 'keys' set_cmd(:user, :keys) when 'clearKeys' set_cmd(:user, :clear) when 'addKeys' set_cmd(:user, :add) when 'scale' set_cmd(:app, :scale) when 'descale' set_cmd(:app, :descale) when 'appState' set_cmd(:info, :state) when 'appInfo' set_cmd(:info, :info) when 'logs' set_cmd(:info, :logs) when 'start' set_cmd(:app, :start) when 'stop' set_cmd(:app, :stop) when 'restart' set_cmd(:app, :restart) when 'deleteInfra' set_cmd(:app, :delete) when 'releases' set_cmd(:info, :releases) when 'iaasProviders' set_cmd(:config, :iaas_providers) when 'frameworks' set_cmd(:config, :frameworks) when 'runtimes' set_cmd(:config, :runtimes) when 'vmconfigs' set_cmd(:config, :memory) when 'app42-update' set_cmd(:config, :update) when 'supportedServices' set_cmd(:service, :app42pass_services) when 'services' set_cmd(:service, :services) when 'serviceInfo' set_cmd(:service, :info) when 'createService' set_cmd(:service, :create) when 'deleteService' set_cmd(:service, :delete) when 'resetServicePassword' set_cmd(:service, :reset_pass) when 'bindIP' set_cmd(:service, :service_bind) when 'unbindIP' set_cmd(:service, :service_unbind) when 'bindInfo' set_cmd(:service, :service_bindInfo) else puts Paint["app42: Unknown command [#{action}]", :red] App42::Base::Help.how_to end end # Set class and respective methods def set_cmd(kclass, command) @kclass = kclass @command = command end # invoke help options def help App42::Base::Help.commands end end end end