diff options
-rw-r--r-- | lib/puppet/defaults.rb | 2 | ||||
-rw-r--r-- | lib/puppet/reference/providers.rb | 5 | ||||
-rw-r--r-- | lib/puppet/util/command_line.rb | 76 | ||||
-rw-r--r-- | spec/unit/util/command_line.rb | 85 |
4 files changed, 65 insertions, 103 deletions
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index b0995c2a9..dab1120df 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -6,7 +6,7 @@ module Puppet var = nil require 'puppet/util/command_line' - name = Puppet::Util::CommandLine.legacy_executable_name + name = Puppet::Util::CommandLine.new.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 df02178e3..33d85061e 100644 --- a/lib/puppet/reference/providers.rb +++ b/lib/puppet/reference/providers.rb @@ -8,8 +8,9 @@ providers = Puppet::Util::Reference.newreference :providers, :title => "Provider end types.sort! { |a,b| a.name.to_s <=> b.name.to_s } - unless Puppet::Util::CommandLine.args.empty? - types.reject! { |type| ! Puppet::Util::CommandLine.args.include?(type.name.to_s) } + command_line = Puppet::Util::CommandLine.new + unless command_line.args.empty? + types.reject! { |type| ! command_line.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 e6656985f..4ab6d317c 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -1,16 +1,6 @@ module Puppet module Util class CommandLine - 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', @@ -24,57 +14,37 @@ module Puppet '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$/, '') + def initialize( zero = $0, argv = ARGV, stdin = STDIN ) + @zero = zero + @argv = argv.dup + @stdin = stdin - 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 + @subcommand_name, @args = subcommand_and_args( @zero, @argv, @stdin ) end - def self.appdir + attr :subcommand_name + attr :args + + def appdir File.join('puppet', 'application') end - def self.available_subcommands - appdir = File.join('puppet', 'application') + def available_subcommands absolute_appdir = $:.collect { |x| File.join(x,'puppet','application') }.detect{ |x| File.directory?(x) } Dir[File.join(absolute_appdir, '*.rb')].map{|fn| File.basename(fn, '.rb')} end - def self.usage_message + def usage_message usage = "Usage: puppet command <space separated arguments>" available = "Available commands are: #{available_subcommands.sort.join(', ')}" [usage, available].join("\n") end - def initialize( zero = $0, argv = ARGV, stdin = STDIN ) - @zero = zero - @argv = argv.dup - @stdin = stdin - - @subcommand_name, @args = self.class.subcommand_and_args( @zero, @argv, @stdin ) - end - - attr :subcommand_name - attr :args - def execute if subcommand_name.nil? - puts self.class.usage_message - elsif self.class.available_subcommands.include?(subcommand_name) #subcommand - require File.join(self.class.appdir, subcommand_name) + puts usage_message + elsif available_subcommands.include?(subcommand_name) #subcommand + require File.join(appdir, subcommand_name) Puppet::Application.find(subcommand_name).new(self).run else abort "Error: Unknown command #{subcommand_name}.\n#{usage_message}" @@ -84,6 +54,24 @@ module Puppet def legacy_executable_name LegacyName[ subcommand_name ] end + + private + + def subcommand_and_args( zero, argv, 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 end end diff --git a/spec/unit/util/command_line.rb b/spec/unit/util/command_line.rb index e20f51a99..7413e57d1 100644 --- a/spec/unit/util/command_line.rb +++ b/spec/unit/util/command_line.rb @@ -12,102 +12,75 @@ describe Puppet::Util::CommandLine do end it "should pull off the first argument if it looks like a subcommand" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", %w( client --help whatever.pp ), @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", %w( client --help whatever.pp ), @tty ) - command.should == "client" - args.should == %w( --help whatever.pp ) + command_line.subcommand_name.should == "client" + command_line.args.should == %w( --help whatever.pp ) end it "should use 'apply' if the first argument looks like a .pp file" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", %w( whatever.pp ), @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", %w( whatever.pp ), @tty ) - command.should == "apply" - args.should == %w( whatever.pp ) + command_line.subcommand_name.should == "apply" + command_line.args.should == %w( whatever.pp ) end it "should use 'apply' if the first argument looks like a .rb file" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", %w( whatever.rb ), @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", %w( whatever.rb ), @tty ) - command.should == "apply" - args.should == %w( whatever.rb ) + command_line.subcommand_name.should == "apply" + command_line.args.should == %w( whatever.rb ) end it "should use 'apply' if the first argument looks like a flag" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", %w( --debug ), @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", %w( --debug ), @tty ) - command.should == "apply" - args.should == %w( --debug ) + command_line.subcommand_name.should == "apply" + command_line.args.should == %w( --debug ) end it "should use 'apply' if the first argument is -" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", %w( - ), @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", %w( - ), @tty ) - command.should == "apply" - args.should == %w( - ) + command_line.subcommand_name.should == "apply" + command_line.args.should == %w( - ) end it "should return nil if the first argument is --help" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", %w( --help ), @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", %w( --help ), @tty ) - command.should == nil + command_line.subcommand_name.should == nil end it "should return nil if there are no arguments on a tty" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", [], @tty ) + command_line = Puppet::Util::CommandLine.new("puppet", [], @tty ) - command.should == nil - args.should == [] + command_line.subcommand_name.should == nil + command_line.args.should == [] end it "should use 'apply' if there are no arguments on a pipe" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppet", [], @pipe ) + command_line = Puppet::Util::CommandLine.new("puppet", [], @pipe ) - command.should == "apply" - args.should == [] - end - - it "should provide a convenience method that only returns the subcommand" do - Puppet::Util::CommandLine.expects(:subcommand_and_args).with("puppet", [], @pipe ).returns(["command", ['args']]) - command = Puppet::Util::CommandLine.subcommand_name( "puppet", [], @pipe ) - command.should == "command" - end - - it "should provide a convenience method that only returns the args" do - Puppet::Util::CommandLine.expects(:subcommand_and_args).with("puppet", [], @pipe ).returns(["command", ['args']]) - args = Puppet::Util::CommandLine.args( "puppet", [], @pipe ) - args.should == ['args'] + command_line.subcommand_name.should == "apply" + command_line.args.should == [] end it "should return the executable name if it is not puppet" do - command, args = Puppet::Util::CommandLine.subcommand_and_args("puppetmasterd", [], @tty ) + command_line = Puppet::Util::CommandLine.new("puppetmasterd", [], @tty ) - command.should == "puppetmasterd" + command_line.subcommand_name.should == "puppetmasterd" end it "should translate subcommand names into their legacy equivalent" do - Puppet::Util::CommandLine.legacy_executable_name("puppet", ["master"], @tty).should == "puppetmasterd" + command_line = Puppet::Util::CommandLine.new("puppet", ["master"], @tty) + command_line.legacy_executable_name.should == "puppetmasterd" end it "should leave legacy command names alone" do - Puppet::Util::CommandLine.legacy_executable_name("puppetmasterd", [], @tty).should == "puppetmasterd" - end - - describe "when instantiated" do - it "should provide the results of subcommand and args" do - Puppet::Util::CommandLine.expects(:subcommand_and_args).with("puppet", [], @pipe).returns(["command", ['args']]) - commandline = Puppet::Util::CommandLine.new("puppet", [], @pipe) - - commandline.subcommand_name.should == 'command' - commandline.args.should == ['args'] - end - - it "should provide the legacy executable name" do - Puppet::Util::CommandLine.expects(:subcommand_and_args).with("puppet", ['master'], @pipe).returns(["master", []]) - commandline = Puppet::Util::CommandLine.new("puppet", ['master'], @pipe) - - commandline.legacy_executable_name.should == 'puppetmasterd' - end + command_line = Puppet::Util::CommandLine.new("puppetmasterd", [], @tty) + command_line.legacy_executable_name.should == "puppetmasterd" end end |