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.



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

def initialize
  @config_path = nil
  @apikey = nil
  @endpoint = nil
  @prog_name = nil
  @insecure = false
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

#endpointObject

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

#insecureObject

Returns the value of attribute insecure.



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

def insecure
  @insecure
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/td/command/runner.rb', line 15

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

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

  op = OptionParser.new
  op.version = TOOLBELT_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 <<EOF
Basic commands:

db             # create/delete/list databases
table          # create/delete/list/import/export/tail tables
query          # issue a query
job            # show/kill/list jobs
import         # manage bulk import sessions (Java based fast processing)
bulk_import    # manage bulk import sessions (Old Ruby-based implementation)
result         # create/delete/list result URLs
sched          # create/delete/list schedules that run a query periodically
schema         # create/delete/modify schemas of tables

Additional commands:

status         # show scheds, jobs, tables and results
apikey         # show/set API key
server         # show status of the Treasure Data server
sample         # create a sample log file
help           # show help messages

Type 'td help COMMAND' for more information on a specific command.
EOF
      if errmsg
        puts "Error: #{errmsg}"
        exit 1
      else
        exit 0
      end
    end
  end

  # there local vars are loaded with the values of the options below
  # here they are preloaded with the defaults
  config_path = @config_path
  apikey = @apikey
  endpoint = @endpoint
  insecure = nil
  $verbose = false
  #$debug = false

  op.on('-c', '--config PATH', "path to the configuration file (default: ~/.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('-e', '--endpoint API_SERVER', "specify the URL for API server to use (default: https://api.treasuredata.com)." ,
                                       "  The URL must contain a scheme (http:// or https:// prefix) to be valid.",
                                       "  Valid IPv4 addresses are accepted as well in place of the host name.") {|e|
    require 'td/command/common'
    Command.validate_api_endpoint(e)
    endpoint = e
  }

  op.on('--insecure', "Insecure access: disable SSL (enabled by default)") {|b|
    insecure = true
  }

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

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

  op.on('-h', '--help', "show help") {
    usage nil
  }

  op.on('--version', "show version") {
    puts op.version
    exit
  }

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

    # NOTE: these information are loaded from by each command through
    #       'TreasureData::Command::get_client' from 'lib/td/command/common.rb'
    require 'td/config'
    if config_path
      Config.path = config_path
    end
    if apikey
      Config.apikey = apikey
      Config.cl_apikey = true
    end
    if endpoint
      Config.endpoint = endpoint
      Config.cl_endpoint = true
    end
    if insecure
      Config.secure = false
    end
  rescue
    usage $!.to_s
  end

  require 'td/command/list'
  if defined?(Encoding)
    #Encoding.default_internal = 'UTF-8' if Encoding.respond_to?(:default_internal)
    Encoding.default_external = 'UTF-8' if Encoding.respond_to?(:default_external)
  end

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

  begin
    method.call(argv)
  rescue ConfigError
    $stderr.puts "TreasureData account is not configured yet."
    $stderr.puts "Run '#{$prog} account' first."
  rescue => e
    # work in progress look ahead development: new exceptions are rendered as simple
    # error messages unless the TD_TOOLBELT_DEBUG variable is not empty.
    # List of new exceptions:
    # => ParameterConfigurationError
    # => BulkImportExecutionError
    # => UpUpdateError
    require 'td/client/api'
    # => APIError
    unless [ParameterConfigurationError, BulkImportExecutionError, UpdateError,
            APIError]
            .include?(e.class) && ENV['TD_TOOLBELT_DEBUG'].nil?
      $stderr.puts "Error #{$!.class}: backtrace:"
      $!.backtrace.each {|b|
        $stderr.puts "  #{b}"
      }
      puts ""
    end
    puts "Error: " + $!.to_s

    require 'socket'
    if e.is_a?(::SocketError)
      $stderr.puts <<EOS

Network dependent error occurred.
If you want to use td command through a proxy,
please set HTTP_PROXY environment variable (e.g. export HTTP_PROXY="host:port")
EOS
    end
    return 1
  end
  return 0
end