diff options
author | Jesse Wolfe <jes5199@gmail.com> | 2010-04-30 14:54:07 -0700 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 63e2e56d3172bdc80aaca5f5ddde5811728e3c76 (patch) | |
tree | 38e3607fe83223d135f1c327682dbd8698bb138d /lib/puppet | |
parent | b6e2ce6a85c953fcd57a3b837ccaa794a634dc22 (diff) | |
download | puppet-63e2e56d3172bdc80aaca5f5ddde5811728e3c76.tar.gz puppet-63e2e56d3172bdc80aaca5f5ddde5811728e3c76.tar.xz puppet-63e2e56d3172bdc80aaca5f5ddde5811728e3c76.zip |
feature #2276 Single Executable: subcommand method
Extract the logic to determine the subcommand name into a method.
Signed-off-by: Jesse Wolfe <jes5199@gmail.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/application.rb | 2 | ||||
-rw-r--r-- | lib/puppet/application/apply.rb | 8 | ||||
-rw-r--r-- | lib/puppet/application/cert.rb | 2 | ||||
-rw-r--r-- | lib/puppet/application/describe.rb | 2 | ||||
-rw-r--r-- | lib/puppet/application/doc.rb | 4 | ||||
-rw-r--r-- | lib/puppet/application/filebucket.rb | 15 | ||||
-rw-r--r-- | lib/puppet/application/resource.rb | 7 | ||||
-rw-r--r-- | lib/puppet/defaults.rb | 15 | ||||
-rw-r--r-- | lib/puppet/reference/providers.rb | 4 | ||||
-rw-r--r-- | lib/puppet/util/command_line.rb | 45 |
10 files changed, 66 insertions, 38 deletions
diff --git a/lib/puppet/application.rb b/lib/puppet/application.rb index 1245b1006..7b404a906 100644 --- a/lib/puppet/application.rb +++ b/lib/puppet/application.rb @@ -25,7 +25,7 @@ require 'optparse' # # # dispatch is called to know to what command to call # dispatch do -# ARGV.shift +# Puppet::Util::CommandLine.args.shift # end # # option("--arg ARGUMENT") do |v| diff --git a/lib/puppet/application/apply.rb b/lib/puppet/application/apply.rb index 787ce375f..d977cf1d0 100644 --- a/lib/puppet/application/apply.rb +++ b/lib/puppet/application/apply.rb @@ -66,10 +66,10 @@ Puppet::Application.new(:apply) do command(:parseonly) do # Set our code or file to use. - if options[:code] or ARGV.length == 0 + if options[:code] or Puppet::Util::CommandLine.args.length == 0 Puppet[:code] = options[:code] || STDIN.read else - Puppet[:manifest] = ARGV.shift + Puppet[:manifest] = Puppet::Util::CommandLine.args.shift end begin Puppet::Resource::TypeCollection.new(Puppet[:environment]).perform_initial_import @@ -82,10 +82,10 @@ Puppet::Application.new(:apply) do command(:main) do # Set our code or file to use. - if options[:code] or ARGV.length == 0 + if options[:code] or Puppet::Util::CommandLine.args.length == 0 Puppet[:code] = options[:code] || STDIN.read else - Puppet[:manifest] = ARGV.shift + Puppet[:manifest] = Puppet::Util::CommandLine.args.shift end # Collect our facts. diff --git a/lib/puppet/application/cert.rb b/lib/puppet/application/cert.rb index f48e5301a..7a7784b09 100644 --- a/lib/puppet/application/cert.rb +++ b/lib/puppet/application/cert.rb @@ -44,7 +44,7 @@ Puppet::Application.new(:cert) do if @all hosts = :all else - hosts = ARGV.collect { |h| puts h; h.downcase } + hosts = Puppet::Util::CommandLine.args.collect { |h| puts h; h.downcase } end begin @ca.apply(:revoke, :to => hosts) if @mode == :destroy diff --git a/lib/puppet/application/describe.rb b/lib/puppet/application/describe.rb index d3d335496..ea4ac162c 100644 --- a/lib/puppet/application/describe.rb +++ b/lib/puppet/application/describe.rb @@ -202,7 +202,7 @@ Puppet::Application.new(:describe,"#{$0} [options] [type]") do end setup do - options[:types] = ARGV.dup + options[:types] = Puppet::Util::CommandLine.args.dup unless options[:list] || options[:types].size > 0 handle_help(nil) end diff --git a/lib/puppet/application/doc.rb b/lib/puppet/application/doc.rb index 32f9ba75c..0a11d6045 100644 --- a/lib/puppet/application/doc.rb +++ b/lib/puppet/application/doc.rb @@ -70,7 +70,7 @@ Puppet::Application.new(:doc) do files += env.modulepath files << File.dirname(env[:manifest]) end - files += ARGV + files += Puppet::Util::CommandLine.args Puppet.info "scanning: %s" % files.inspect Puppet.settings.setdefaults("puppetdoc", "document_all" => [false, "Document all resources"] @@ -167,7 +167,7 @@ Puppet::Application.new(:doc) do setup do # sole manifest documentation - if ARGV.size > 0 + if Puppet::Util::CommandLine.args.size > 0 options[:mode] = :rdoc @manifest = true end diff --git a/lib/puppet/application/filebucket.rb b/lib/puppet/application/filebucket.rb index ed67009aa..cd7c854af 100644 --- a/lib/puppet/application/filebucket.rb +++ b/lib/puppet/application/filebucket.rb @@ -12,18 +12,23 @@ Puppet::Application.new(:filebucket) do option("--remote","-r") option("--verbose","-v") + class << self + attr :args + end + dispatch do - ARGV.shift + @args = Puppet::Util::CommandLine.args + args.shift end command(:get) do - md5 = ARGV.shift + md5 = args.shift out = @client.getfile(md5) print out end command(:backup) do - ARGV.each do |file| + args.each do |file| unless FileTest.exists?(file) $stderr.puts "%s: no such file" % file next @@ -38,8 +43,8 @@ Puppet::Application.new(:filebucket) do end command(:restore) do - file = ARGV.shift - md5 = ARGV.shift + file = args.shift + md5 = args.shift @client.restore(file, md5) end diff --git a/lib/puppet/application/resource.rb b/lib/puppet/application/resource.rb index ae4349850..78aed95c5 100644 --- a/lib/puppet/application/resource.rb +++ b/lib/puppet/application/resource.rb @@ -38,11 +38,12 @@ Puppet::Application.new(:resource) do end command(:main) do - type = ARGV.shift or raise "You must specify the type to display" + args = Puppet::Util::CommandLine.args + type = args.shift or raise "You must specify the type to display" typeobj = Puppet::Type.type(type) or raise "Could not find type #{type}" - name = ARGV.shift + name = args.shift params = {} - ARGV.each do |setting| + args.each do |setting| if setting =~ /^(\w+)=(.+)$/ params[$1] = $2 else diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index a9cb48419..66f038307 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -5,19 +5,8 @@ module Puppet conf = nil var = nil - legacy_name = 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', - }) - name = legacy_name[ $puppet_subcommand_name ] || $0.gsub(/.+#{File::SEPARATOR}/,'').sub(/\.rb$/, '') + require 'puppet/util/command_line' + name = Puppet::Util::CommandLine.legacy_executable_name # Make File.expand_path happy require 'etc' diff --git a/lib/puppet/reference/providers.rb b/lib/puppet/reference/providers.rb index d425d803e..df02178e3 100644 --- a/lib/puppet/reference/providers.rb +++ b/lib/puppet/reference/providers.rb @@ -8,8 +8,8 @@ providers = Puppet::Util::Reference.newreference :providers, :title => "Provider end types.sort! { |a,b| a.name.to_s <=> b.name.to_s } - unless ARGV.empty? - types.reject! { |type| ! ARGV.include?(type.name.to_s) } + unless Puppet::Util::CommandLine.args.empty? + types.reject! { |type| ! Puppet::Util::CommandLine.args.include?(type.name.to_s) } end ret = "Details about this host:\n\n" diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb index f231ee7f8..5fe07b28d 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -1,12 +1,45 @@ module Puppet module Util module CommandLine - def self.shift_subcommand_from_argv( argv = ARGV, stdin = STDIN ) - case argv.first - when nil; "apply" unless stdin.tty? # ttys get usage info - when "--help"; nil # help should give you usage, not the help for `puppet apply` - when /^-|\.pp$|\.rb$/; "apply" - else argv.shift + def self.subcommand_name(*args) + subcommand_name, args = subcommand_and_args(*args) + return subcommand_name + end + + def self.args(*args) + subcommand_name, args = subcommand_and_args(*args) + return args + end + + 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', + }) + + def self.legacy_executable_name(*args) + LegacyName[ subcommand_name(*args) ] + end + + def self.subcommand_and_args( zero = $0, argv = ARGV, stdin = STDIN ) + zero = zero.gsub(/.*#{File::SEPARATOR}/,'').sub(/\.rb$/, '') + + if zero == 'puppet' + case argv.first + when nil; [ stdin.tty? ? nil : "apply", argv] # ttys get usage info + when "--help"; [nil, argv] # help should give you usage, not the help for `puppet apply` + when /^-|\.pp$|\.rb$/; ["apply", argv] + else [ argv.first, argv[1..-1] ] + end + else + [ zero, argv ] end end end |