Class: Puppet::Util::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/util/command_line.rb

Constant Summary collapse

LegacyName =
Hash.new{|h,k| k}.update(
  'agent'      => 'puppetd',
  'cert'       => 'puppetca',
  'doc'        => 'puppetdoc',
  'filebucket' => 'filebucket',
  'apply'      => 'puppet',
  'describe'   => 'pi',
  'queue'      => 'puppetqd',
  'resource'   => 'ralsh',
  'kick'       => 'puppetrun',
  'master'     => 'puppetmasterd',
  'device'     => 'puppetdevice'
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zero = $0, argv = ARGV, stdin = STDIN) ⇒ CommandLine

Returns a new instance of CommandLine.



21
22
23
24
25
26
27
28
# File 'lib/vendor/puppet/util/command_line.rb', line 21

def initialize(zero = $0, argv = ARGV, stdin = STDIN)
  @zero  = zero
  @argv  = argv.dup
  @stdin = stdin

  @subcommand_name, @args = subcommand_and_args(@zero, @argv, @stdin)
  Puppet::Plugins.on_commandline_initialization(:command_line_object => self)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



31
32
33
# File 'lib/vendor/puppet/util/command_line.rb', line 31

def args
  @args
end

#subcommand_nameObject (readonly)

Returns the value of attribute subcommand_name.



30
31
32
# File 'lib/vendor/puppet/util/command_line.rb', line 30

def subcommand_name
  @subcommand_name
end

Class Method Details

.available_subcommandsObject



37
38
39
40
41
42
43
44
# File 'lib/vendor/puppet/util/command_line.rb', line 37

def self.available_subcommands
  absolute_appdirs = $LOAD_PATH.collect do |x|
    File.join(x,'puppet','application')
  end.select{ |x| File.directory?(x) }
  absolute_appdirs.inject([]) do |commands, dir|
    commands + Dir[File.join(dir, '*.rb')].map{|fn| File.basename(fn, '.rb')}
  end.uniq
end

Instance Method Details

#appdirObject



33
34
35
# File 'lib/vendor/puppet/util/command_line.rb', line 33

def appdir
  File.join('puppet', 'application')
end

#available_subcommandsObject

available_subcommands was previously an instance method, not a class method, and we have an unknown number of user-implemented applications that depend on that behaviour. Forwarding allows us to preserve a backward compatible API. –daniel 2011-04-11



49
50
51
# File 'lib/vendor/puppet/util/command_line.rb', line 49

def available_subcommands
  self.class.available_subcommands
end

#executeObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vendor/puppet/util/command_line.rb', line 57

def execute
  if subcommand_name and available_subcommands.include?(subcommand_name) then
    require_application subcommand_name
    app = Puppet::Application.find(subcommand_name).new(self)
    Puppet::Plugins.on_application_initialization(:appliation_object => self)

    # See the note in 'warn_later' down below. --daniel 2011-06-01
    if $delayed_deprecation_warning_for_p_u_cl.is_a? String then
      Puppet.deprecation_warning($delayed_deprecation_warning_for_p_u_cl)
      $delayed_deprecation_warning_for_p_u_cl = true
    end

    app.run
  elsif ! execute_external_subcommand then
    unless subcommand_name.nil? then
      puts "Error: Unknown Puppet subcommand '#{subcommand_name}'"
    end
    puts "See 'puppet help' for help on available puppet subcommands"
  end
end

#execute_external_subcommandObject



78
79
80
81
82
83
84
85
86
# File 'lib/vendor/puppet/util/command_line.rb', line 78

def execute_external_subcommand
  external_command = "puppet-#{subcommand_name}"

  require 'puppet/util'
  path_to_subcommand = Puppet::Util.which(external_command)
  return false unless path_to_subcommand

  exec(path_to_subcommand, *args)
end

#legacy_executable_nameObject



88
89
90
# File 'lib/vendor/puppet/util/command_line.rb', line 88

def legacy_executable_name
  LegacyName[ subcommand_name ]
end

#require_application(application) ⇒ Object



53
54
55
# File 'lib/vendor/puppet/util/command_line.rb', line 53

def require_application(application)
  require File.join(appdir, application)
end