Class: OSDN::CLI::Runner
- Inherits:
-
Object
- Object
- OSDN::CLI::Runner
- Defined in:
- lib/osdn/cli/runner.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #get_command_class(command_name) ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #parse_opt ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/osdn/cli/runner.rb', line 8 def initialize @logger = Logger.new(STDERR) @logger.level = Logger::INFO @logger.formatter = proc { |severity, time, progname, msg| "[%s] %s\n" % [severity, msg] } OSDNClient.configure do |config| ENV['OSDN_API_OVERRIDE_HOST'] and config.host = ENV['OSDN_API_OVERRIDE_HOST'] ENV['OSDN_API_SKIP_SSL_VERIFY'].to_s =~ /^(1|t(rue)?|y(es)?)$/i and config.verify_ssl = false end end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
21 22 23 |
# File 'lib/osdn/cli/runner.rb', line 21 def logger @logger end |
Instance Method Details
#get_command_class(command_name) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/osdn/cli/runner.rb', line 54 def get_command_class(command_name) class_name = command_name.to_s.split('_').map(&:capitalize).join begin return self.class.const_get("OSDN::CLI::Command::#{class_name}") rescue NameError => e logger.fatal "Invalid command name '#{command_name}'. Use 'help' to list commands." exit end false end |
#help ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/osdn/cli/runner.rb', line 99 def help command_name = ARGV.shift if command_name get_command_class(command_name).new(logger).help else puts "#{$0} [global-options] <command> [command-options] [args]" puts "#{$0} help <command>" puts "Global Options:" puts " -h --help Show help message. use 'help <command>' for specific command. " puts " -v --verbose Increase log level (multiple)" puts " -q --quiet Decrease log level (multiple)" puts "Avaiable Commands:" puts " help" OSDN::CLI::Command.constants.each do |c| c = c.to_s.split(/(?=[A-Z])/).join('_').downcase c == 'base' and next puts " %-14s %s" % [c, get_command_class(c).description] end end end |
#parse_opt ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/osdn/cli/runner.rb', line 23 def parse_opt opts = GetoptLong.new( [ '--help', '-h', GetoptLong::NO_ARGUMENT ], [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], [ '--quiet', '-q', GetoptLong::NO_ARGUMENT ], ) opts.ordering = GetoptLong::REQUIRE_ORDER opts.each do |opt, arg| case opt when '--help' help exit 0 when '--verbose' if logger.level == Logger::DEBUG OSDNClient.configure do |config| config.debugging = true end end logger.level > Logger::DEBUG and logger.level -= 1 when '--quiet' logger.level < Logger::UNKNOWN and logger.level += 1 when '--help' help exit end end logger.debug "Loglevel is #{logger.level}" end |
#run ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/osdn/cli/runner.rb', line 65 def run parse_opt command_name = ARGV.shift unless command_name help exit 1 end if command_name == 'help' help exit end command = get_command_class(command_name).new(logger) logger.debug "Run command #{command_name}" begin command.run rescue OSDNClient::ApiError => e begin err = JSON.parse(e.response_body) if err["message"] logger.fatal "#{err["status"]}: #{err["message"]}" elsif err["error_description"] logger.fatal err["error_description"] else logger.fatal "Command failed by ApiError: #{e.response_body}" end rescue logger.fatal "Command failed: #{e.inspect}" end end end |