#!/usr/bin/env ruby require 'rubygems' require 'rest_client' require 'gli' require 'pagoda/cli/version' # require 'pagoda/cli/override' # command = ARGV.shift.strip rescue 'help' include GLI::App config_file '.pagodarc' program_desc 'Command line client for Pagoda Box' version Pagoda::CLI::VERSION desc "Username created on pagoda Box" arg_name "Username" flag [:u, :username] desc "Password to connect to Pagoda Box" arg_name "Password" flag [:p, :password] desc 'The app name for Pagoda Box' arg_name 'APP_NAME' flag [:a, :app] desc "Help" switch [:h, :help] require 'pagoda/cli' # GLI::DefaultHelpCommand.skips_pre = true pre do |global,command,options,args| # Pre logic here # Return true to proceed; false to abourt and not call the # chosen command # Use skips_pre before a command to skip this block # on that command only if global[:help] or options[:help] if command GLI.commands[:help].execute(global, {}, [ command.name ]) else GLI.commands[:help].execute(global, {}, []) end false else if command and not global[:username] and not global[:password] puts "it appears this is the first time you have used our client" user, password = Pagoda::Command::Base.ask_for_credentials run(["-u",user,'-p',password,'initconfig', '--force']) global.merge!(:username => user, :password => password) end true end end post do |global,command,options,args| # Post logic here # Use skips_post before a command to skip this # block on that command only end on_error do |exception| if exception.class == RestClient::Unauthorized Pagoda::Command::Base.error "Username or Password is invald" elsif exception.class == RestClient::ResourceNotFound Pagoda::Command::Base.error "It appears we cant find your 'resource', please try again later" elsif exception.class == RestClient::InternalServerError Pagoda::Command::Base.error "It appears pagodabox is having some trouble, please try again later" elsif exception.class == GLI::UnknownGlobalArgument or exception.class == GLI::UnknownCommandArgument or exception.class == GLI::UnknownCommand Pagoda::Command::Base.error " -- #{exception.message} -- " elsif exception.class == RestClient::UnprocessableEntity errors = [] errors << ":: Unable to process your request" body = JSON.load(exception.response.body) body.each do |key, value| value.each do |problem| errors << " +> #{key.gsub(/\_/, "-")} #{problem}" end end Pagoda::Command::Base.error errors elsif exception.class == SystemExit or exception.class == Interrupt else errors = [ "The Client has had an unexpected error:", " -- #{exception.message} -- ", exception.backtrace.join("\n"), "Please report this error to support@pagodabox.com so we can fix it.", "Thank you." ] Pagoda::Command::Base.error errors end # Error logic here # return false to skip default error handling # ENV['GLI_DEBUG'] = 'true' # false end exit run(ARGV)