Class: OSDN::CLI::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/osdn/cli/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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
      config.verify_ssl_host = false
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/osdn/cli/runner.rb', line 22

def logger
  @logger
end

Instance Method Details

#get_command_class(command_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/osdn/cli/runner.rb', line 55

def get_command_class(command_name)
  class_name = command_name.to_s.split('_').map(&:capitalize).join
  begin
    return OSDN::CLI::Command.const_get(class_name)
  rescue NameError => e
    logger.fatal "Invalid command name '#{command_name}'. Use 'help' to list commands."
    exit
  end
  false
end

#helpObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/osdn/cli/runner.rb', line 104

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_optObject



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
53
# File 'lib/osdn/cli/runner.rb', line 24

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

#runObject



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
98
99
100
101
102
# File 'lib/osdn/cli/runner.rb', line 66

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
    Signal.trap "INT" do
      puts :INT
      exit
    end
    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} #{e.message} (#{e.code}): #{e.response_body} #{e.response_headers}"
    end
  end
end