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.



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

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.



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

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#optparseObject (readonly)

Returns the value of attribute optparse.



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

def optparse
  @optparse
end

#source_providerObject (readonly)

Returns the value of attribute source_provider.



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

def source_provider
  @source_provider
end

Instance Method Details

#main(argv = []) ⇒ Object



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

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
    @source_provider = get_source(@options[:source])
  rescue NameError
    STDERR.puts("hotdog: '#{@options[:source]}' is not a valid hotdog source.")
    exit(1)
  end

  begin
    command_name = ( args.shift || "help" )
    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, 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

    options[:formatter] = get_formatter(options[:format])

    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



154
155
156
# File 'lib/hotdog/application.rb', line 154

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

#status_name(status = self.status) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/hotdog/application.rb', line 158

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