Class: Hotdog::Application
- Inherits:
-
Object
- Object
- Hotdog::Application
- Defined in:
- lib/hotdog/application.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#optparse ⇒ Object
readonly
Returns the value of attribute optparse.
-
#source_provider ⇒ Object
readonly
Returns the value of attribute source_provider.
Instance Method Summary collapse
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #main(argv = []) ⇒ Object
- #status ⇒ Object
- #status_name(status = self.status) ⇒ Object
Constructor Details
#initialize ⇒ Application
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 = { endpoint: nil, api_key: nil, application_key: nil, application: self, confdir: find_confdir(File.(".")), 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()` end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
77 78 79 |
# File 'lib/hotdog/application.rb', line 77 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
78 79 80 |
# File 'lib/hotdog/application.rb', line 78 def end |
#optparse ⇒ Object (readonly)
Returns the value of attribute optparse.
79 80 81 |
# File 'lib/hotdog/application.rb', line 79 def optparse @optparse end |
#source_provider ⇒ Object (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([: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 = .merge(Hash[loaded.map { |key, value| [Symbol === key ? key : key.to_s.to_sym, value] }]) end end args = @optparse.order(argv) begin if Hash === [:source_alias] source_name = [:source_alias].fetch([:source], [:source]) else source_name = [: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 === [:command_alias] command_alias = [: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"], ) exit(1) end @optparse. = "Usage: hotdog #{command_name} [options]" command.(@optparse, ) begin args = command.(@optparse, command_args + args) rescue OptionParser::ParseError => error STDERR.puts("hotdog: #{error.message}") command.(@optparse, ["--help"]) exit(1) end if [:format] == "ltsv" [:headers] = true end if Hash === [:format_alias] format_name = [:format_alias].fetch([:format], [:format]) else format_name = [:format] end [:formatter] = get_formatter(format_name) if ( [:debug] or [:verbose] ) and ( [:verbosity] < VERBOSITY_DEBUG ) [:verbosity] = VERBOSITY_DEBUG end if VERBOSITY_DEBUG <= [:verbosity] [:logger].level = Logger::DEBUG else if VERBOSITY_INFO <= [:verbosity] [:logger].level = Logger::INFO else [:logger].level = Logger::WARN end end command.run(args, ) rescue Interrupt STDERR.puts("Interrupt") rescue Errno::EPIPE => error STDERR.puts(error) rescue => error raise # to show error stacktrace end end |
#status ⇒ Object
176 177 178 |
# File 'lib/hotdog/application.rb', line 176 def status() .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 |