Class: Hotdog::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/hotdog/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



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
# File 'lib/hotdog/application.rb', line 40

def initialize()
  @logger = Logger.new(STDERR)
  @optparse = OptionParser.new
  @optparse.version = Hotdog::VERSION
  @options = {
    endpoint: nil,
    api_key: nil,
    application_key: nil,
    application: self,
    confdir: find_confdir(File.expand_path(".")),
    debug: false,
    expiry: 3600,
    fixed_string: false,
    force: false,
    format: "text",
    headers: false,
    source: "datadog",
    status: nil,
    listing: false,
    logger: @logger,
    max_time: 5,
    offline: false,
    print0: false,
    print1: true,
    print2: false,
    primary_tag: nil,
    tags: [],
    display_search_tags: false,
    verbose: false,
    verbosity: VERBOSITY_NULL,
  }.reject { |key, val|
    # reject nil values to declare sensible default later in subcommand
    val.nil?
  }
  @source_provider = nil # will be initialized later in `main()`
  define_options
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



77
78
79
# File 'lib/hotdog/application.rb', line 77

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



78
79
80
# File 'lib/hotdog/application.rb', line 78

def options
  @options
end

#optparseObject (readonly)

Returns the value of attribute optparse.



79
80
81
# File 'lib/hotdog/application.rb', line 79

def optparse
  @optparse
end

#source_providerObject (readonly)

Returns the value of attribute source_provider.



80
81
82
# File 'lib/hotdog/application.rb', line 80

def source_provider
  @source_provider
end

Instance Method Details

#main(argv = []) ⇒ Object



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
# File 'lib/hotdog/application.rb', line 82

def main(argv=[])
  config = File.join(options[:confdir], "config.yml")
  if File.file?(config)
    begin
      loaded = YAML.load(ERB.new(File.read(config)).result)
    rescue => error
      STDERR.puts("hotdog: failed to load configuration file at #{config.inspect}: #{error}")
      exit(1)
    end
    if Hash === loaded
      @options = @options.merge(Hash[loaded.map { |key, value| [Symbol === key ? key : key.to_s.to_sym, value] }])
    end
  end
  args = @optparse.order(argv)

  begin
    if Hash === @options[:source_alias]
      source_name = @options[:source_alias].fetch(@options[:source], @options[:source])
    else
      source_name = @options[:source]
    end
    @source_provider = get_source(source_name)
  rescue NameError
    STDERR.puts("hotdog: '#{source_name}' is not a valid hotdog source.")
    exit(1)
  end

  begin
    given_command_name = ( args.shift || "help" )
    if Hash === @options[:command_alias]
      command_alias = @options[:command_alias].fetch(given_command_name, given_command_name)
      if Array === command_alias
        command_name, *command_args = command_alias
      else
        command_name, *command_args = Shellwords.shellsplit(command_alias)
      end
    else
      command_name = given_command_name
      command_args = []
    end
    begin
      command = get_command(command_name)
    rescue NameError
      STDERR.puts("hotdog: '#{command_name}' is not a hotdog command.")
      get_command("help").run(["commands"], options)
      exit(1)
    end

    @optparse.banner = "Usage: hotdog #{command_name} [options]"
    command.define_options(@optparse, @options)

    begin
      args = command.parse_options(@optparse, command_args + args)
    rescue OptionParser::ParseError => error
      STDERR.puts("hotdog: #{error.message}")
      command.parse_options(@optparse, ["--help"])
      exit(1)
    end

    if options[:format] == "ltsv"
      options[:headers] = true
    end

    if Hash === @options[:format_alias]
      format_name = @options[:format_alias].fetch(@options[:format], @options[:format])
    else
      format_name = @options[:format]
    end
    options[:formatter] = get_formatter(format_name)

    if ( options[:debug] or options[:verbose] ) and ( options[:verbosity] < VERBOSITY_DEBUG )
      options[:verbosity] = VERBOSITY_DEBUG
    end

    if VERBOSITY_DEBUG <= options[:verbosity]
      options[:logger].level = Logger::DEBUG
    else
      if VERBOSITY_INFO <= options[:verbosity]
        options[:logger].level = Logger::INFO
      else
        options[:logger].level = Logger::WARN
      end
    end

    command.run(args, @options)
  rescue Interrupt
    STDERR.puts("Interrupt")
  rescue Errno::EPIPE => error
    STDERR.puts(error)
  rescue => error
    raise # to show error stacktrace
  end
end

#statusObject



176
177
178
# File 'lib/hotdog/application.rb', line 176

def status()
  options.fetch(:status, STATUS_RUNNING)
end

#status_name(status = self.status) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/hotdog/application.rb', line 180

def status_name(status=self.status)
  {
    STATUS_PENDING       => "pending",
    STATUS_RUNNING       => "running",
    STATUS_SHUTTING_DOWN => "shutting-down",
    STATUS_TERMINATED    => "terminated",
    STATUS_STOPPING      => "stopping",
    STATUS_STOPPED       => "stopped",
  }.fetch(status, "unknown")
end