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

def initialize
  @logger = Logger.new(STDERR)
  @logger.level = Logger::WARN
  @logger.formatter = proc { |severity, time, progname, msg|
    "[%s] %s\n" % [severity, msg]
  }
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/osdn/cli/runner.rb', line 15

def logger
  @logger
end

Instance Method Details

#get_command_class(command_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/osdn/cli/runner.rb', line 48

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

#helpObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/osdn/cli/runner.rb', line 93

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osdn/cli/runner.rb', line 17

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



59
60
61
62
63
64
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
# File 'lib/osdn/cli/runner.rb', line 59

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: #{e.inspect}"
      end
    rescue
      logger.fatal "Command failed: #{e.inspect}"
    end
  end
end