Class: TreasureData::Command::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/td/command/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



7
8
9
10
11
# File 'lib/td/command/runner.rb', line 7

def initialize
  @config_path = nil
  @apikey = nil
  @prog_name = nil
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



13
14
15
# File 'lib/td/command/runner.rb', line 13

def apikey
  @apikey
end

#config_pathObject

Returns the value of attribute config_path.



13
14
15
# File 'lib/td/command/runner.rb', line 13

def config_path
  @config_path
end

#prog_nameObject

Returns the value of attribute prog_name.



13
14
15
# File 'lib/td/command/runner.rb', line 13

def prog_name
  @prog_name
end

Instance Method Details

#run(argv = ARGV) ⇒ Object



15
16
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
47
48
49
50
51
52
53
54
55
56
57
58
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/td/command/runner.rb', line 15

def run(argv=ARGV)
  require 'td/version'
  require 'optparse'

  $prog = @prog_name || File.basename($0)

  op = OptionParser.new
  op.version = TreasureData::VERSION
  op.banner = <<EOF
usage: #{$prog} [options] COMMAND [args]

options:
EOF

  op.summary_indent = "  "

  (class<<self;self;end).module_eval do
    define_method(:usage) do |errmsg|
      require 'td/command/list'
      puts op.to_s
      puts ""
      puts "commands:"
      TreasureData::Command::List.show_help(op.summary_indent)
      puts ""
      puts "Type '#{$prog} help COMMAND' for more information on a specific command."
      if errmsg
        puts "error: #{errmsg}"
        exit 1
      else
        exit 0
      end
    end
  end

  config_path = @config_path
  apikey = @apikey
  $verbose = false
  #$debug = false

  op.on('-c', '--config PATH', "path to config file (~/.td/td.conf)") {|s|
    config_path = s
  }

  op.on('-k', '--apikey KEY', "use this API key instead of reading the config file") {|s|
    apikey = s
  }

  op.on('-v', '--verbose', "verbose mode", TrueClass) {|b|
    $verbose = b
  }

  #op.on('-d', '--debug', "debug mode", TrueClass) {|b|
  #	$debug = b
  #}

  begin
    op.order!(argv)
    usage nil if argv.empty?
    cmd = argv.shift

    require 'td/config'
    if config_path
      TreasureData::Config.path = config_path
    end
    if apikey
      TreasureData::Config.apikey = apikey
    end
  rescue
    usage $!.to_s
  end

  require 'td/command/list'

  method = TreasureData::Command::List.get_method(cmd)
  unless method
    $stderr.puts "'#{cmd}' is not a td command. Run '#{$prog}' to show the list."
    TreasureData::Command::List.show_guess(cmd)
    exit 1
  end

  begin
    method.call(argv)
  rescue TreasureData::ConfigError
    $stderr.puts "TreasureData account is not configured yet."
    $stderr.puts "Run '#{$prog} account' first."
  rescue
    $stderr.puts "error #{$!.class}: backtrace:"
    $!.backtrace.each {|b|
      $stderr.puts "  #{b}"
    }
    puts ""
    puts $!
  end
end