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.



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

def initialize()
  @logger = Logger.new(STDERR).tap { |logger|
    logger.level = Logger::INFO
  }
  @optparse = OptionParser.new
  @optparse.version = Hotdog::VERSION
  @options = {
    endpoint: ENV.fetch("DATADOG_HOST", "https://app.datadoghq.com"),
    api_key: ENV["DATADOG_API_KEY"],
    application_key: ENV["DATADOG_APPLICATION_KEY"],
    application: self,
    confdir: find_confdir(File.expand_path(".")),
    debug: false,
    expiry: 3600,
    fixed_string: false,
    force: false,
    format: "plain",
    headers: false,
    listing: false,
    logger: @logger,
    max_time: 5,
    offline: false,
    print0: false,
    print1: true,
    primary_tag: nil,
    tags: [],
    display_search_tags: false,
    verbose: false,
  }
  define_options
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



46
47
48
# File 'lib/hotdog/application.rb', line 46

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



47
48
49
# File 'lib/hotdog/application.rb', line 47

def options
  @options
end

#optparseObject (readonly)

Returns the value of attribute optparse.



48
49
50
# File 'lib/hotdog/application.rb', line 48

def optparse
  @optparse
end

Instance Method Details

#api_keyObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hotdog/application.rb', line 108

def api_key()
  if options[:api_key]
    options[:api_key]
  else
    update_api_key!
    if options[:api_key]
      options[:api_key]
    else
      raise("DATADOG_API_KEY is not set")
    end
  end
end

#application_keyObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hotdog/application.rb', line 121

def application_key()
  if options[:application_key]
    options[:application_key]
  else
    update_application_key!
    if options[:application_key]
      options[:application_key]
    else
      raise("DATADOG_APPLICATION_KEY is not set")
    end
  end
end

#main(argv = []) ⇒ Object



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

def main(argv=[])
  config = File.join(options[:confdir], "config.yml")
  if File.file?(config)
    loaded = YAML.load(ERB.new(File.read(config)).result)
    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
    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]
      options[:logger].level = Logger::DEBUG
    else
      options[:logger].level = Logger::INFO
    end

    command.run(args, @options)
  rescue Interrupt
    STDERR.puts("Interrupt")
  rescue Errno::EPIPE => error
    STDERR.puts(error)
  rescue => error
    STDERR.puts(error)
    if @options[:debug]
      raise # to show error stacktrace
    else
      exit(1)
    end
  end
end