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.



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

def initialize()
  @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: 1800,
    fixed_string: false,
    force: false,
    format: "plain",
    headers: false,
    listing: false,
    logger: Logger.new(STDERR).tap { |logger|
      logger.level = Logger::INFO
    },
    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

#optionsObject (readonly)

Returns the value of attribute options.



42
43
44
# File 'lib/hotdog/application.rb', line 42

def options
  @options
end

#optparseObject (readonly)

Returns the value of attribute optparse.



43
44
45
# File 'lib/hotdog/application.rb', line 43

def optparse
  @optparse
end

Instance Method Details

#main(argv = []) ⇒ Object



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

def main(argv=[])
  config = File.join(options[:confdir], "config.yml")
  if File.file?(config)
    loaded = YAML.load(File.read(config))
    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 = ( args.shift || "help" )
    get_command(command).tap do |cmd|
      @optparse.banner = "Usage: hotdog #{command} [options]"
      cmd.define_options(@optparse, @options)
      args = cmd.parse_options(@optparse, args)
      unless options[:api_key]
        raise("DATADOG_API_KEY is not set")
      end

      unless options[:application_key]
        raise("DATADOG_APPLICATION_KEY is not set")
      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

      cmd.run(args, @options)
    end
  rescue OptionParser::ParseError => error
    STDERR.puts("hotdog: #{error.message}")
    require "hotdog/commands/help"
    get_command(command).tap do |cmd|
      if Hotdog::Commands::Help === cmd
        STDERR.puts("hotdog: '#{command}' is not a hotdog command.")
      else
        cmd.parse_options(@optparse, ["--help"])
      end
    end
    exit(1)
  rescue Errno::EPIPE
    # nop
  end
end