Class: Ath::Command

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

Constant Summary collapse

MAX_LIST_QUERY =
20

Instance Method Summary collapse

Constructor Details

#initialize(shell:, command:, arg: nil) ⇒ Command

Returns a new instance of Command.



4
5
6
7
8
# File 'lib/ath/command.rb', line 4

def initialize(shell:, command:, arg: nil)
  @shell = shell
  @command = command
  @arg = arg
end

Instance Method Details

#runObject



10
11
12
13
14
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
# File 'lib/ath/command.rb', line 10

def run
  out = nil

  case @command
  when 'debug'
    if @arg == 'true' or @arg == 'false'
      @shell.options[:debug] = (@arg =~ /true/)
    else
      out = !!@shell.options.fetch(:debug)
    end
  when 'desc'
    if @arg
      query_execution = @shell.driver.get_query_execution(query_execution_id: @arg)
      out = JSON.pretty_generate(query_execution.to_h)
    else
      out = "Usage: /desc QUERY_EXECUTION_ID"
    end
  when 'help'
    out = usage
  when 'list'
    query_executions = @shell.driver.list_query_executions
    query_executions.sort_by! {|qe| qe.status.submission_date_time }.reverse!

    lines = query_executions.map do |qe|
      line = [
        qe.status.submission_date_time,
        qe.query_execution_id,
        qe.status.state,
      ]

      if qe.query.length > MAX_LIST_QUERY
        line << qe.query.slice(0, MAX_LIST_QUERY) + '..'
      else
        line << qe.query
      end

      line.join("\s")
    end

    if @arg
      lines = lines.slice(0, @arg.to_i)
    end

    out = lines.join("\n")
  when 'output_location'
    if @arg
      @shell.driver.output_location = @arg
    else
      out = @shell.driver.output_location
    end
  when 'pager'
    if @arg
      @shell.pager = @arg
    else
      @shell.pager = nil
      out = "Using stdout"
    end
  when 'region'
    if @arg
      @shell.driver.region = @arg
    else
      out = @shell.driver.region
    end
  when 'result'
    if @arg
      out = @shell.driver.get_query_execution_result(query_execution_id: @arg)
    else
      out = "Usage: /result QUERY_EXECUTION_ID"
    end
  when 'save'
    if @arg
      query_execution_id, path = @arg.split(/\s+/, 2)
      path ||= Dir.pwd
      path = @shell.driver.save_query_execution_result(query_execution_id: query_execution_id, path: path)
      out = "Save to #{path}"
    else
      out = "Usage: /result QUERY_EXECUTION_ID [PATH]"
    end
  when 'stop'
    if @arg
      @shell.driver.stop_query_execution(query_execution_id: @arg)
    else
      out = "Usage: /stop QUERY_EXECUTION_ID"
    end
  when 'use'
    if @arg
      @shell.driver.database = @arg
    else
      out = "Usage: /use DATABASE"
    end
  else
    raise Ath::Error, "Unknown command: #{@command}"
  end

  out
end