Class: NewRelic::Cli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/cli/command.rb

Direct Known Subclasses

Deployments, Install

Defined Under Namespace

Classes: CommandFailure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_line_args) ⇒ Command

Returns a new instance of Command.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/new_relic/cli/command.rb', line 32

def initialize(command_line_args)
  if Hash === command_line_args
    # command line args is an options hash
    command_line_args.each do |key, value|
      instance_variable_set("@#{key}", value.to_s) if value
    end
  else
    # parse command line args.  Throw an exception on a bad arg.
    @options = options do |opts|
      opts.on('-h', 'Show this help') { raise CommandFailure, opts.to_s }
    end
    @leftover = @options.parse(command_line_args)
  end
rescue OptionParser::ParseError => e
  raise CommandFailure.new(e.message, @options)
end

Instance Attribute Details

#leftoverObject

Returns the value of attribute leftover.



14
15
16
# File 'lib/new_relic/cli/command.rb', line 14

def leftover
  @leftover
end

Class Method Details

.inherited(subclass) ⇒ Object



50
51
52
# File 'lib/new_relic/cli/command.rb', line 50

def self.inherited(subclass)
  @commands << subclass
end

.runObject



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
# File 'lib/new_relic/cli/command.rb', line 57

def self.run
  @command_names = @commands.map { |c| c.command }

  extra = []
  options = ARGV.options do |opts|
    script_name = File.basename($0)

    # TODO: MAJOR VERSION - remove newrelic, deprecated since version x.xx
    if /newrelic$/.match?(script_name)
      $stdout.puts "warning: the 'newrelic' script has been renamed 'newrelic_rpm'"
      script_name = 'newrelic_rpm'
    end

    opts.banner = "Usage: #{script_name} [ #{@command_names.join(' | ')} ] [options]"
    opts.separator("use '#{script_name} <command> -h' to see detailed command options")
    opts
  end
  extra = options.order!
  command = extra.shift
  # just make it a little easier on them
  command = 'deployments' if command.include?('deploy')
  if command.nil?
    STDERR.puts options
  elsif !@command_names.include?(command)
    STDERR.puts "Unrecognized command: #{command}"
    STDERR.puts options
  else
    command_class = @commands.find { |c| c.command == command }
    command_class.new(extra).run
  end
rescue OptionParser::InvalidOption => e
  raise NewRelic::Cli::Command::CommandFailure, e.message
end

Instance Method Details

#err(message) ⇒ Object



28
29
30
# File 'lib/new_relic/cli/command.rb', line 28

def err(message)
  STDERR.puts message
end

#info(message) ⇒ Object



24
25
26
# File 'lib/new_relic/cli/command.rb', line 24

def info(message)
  STDOUT.puts message
end