diff options
| author | Jesse Wolfe <jes5199@gmail.com> | 2010-04-22 12:04:10 -0700 |
|---|---|---|
| committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
| commit | 7656ba73ddfd883b36a01c81147ae69e80773bce (patch) | |
| tree | 5bc7cad1485bb9749ce387336b30794e3ae93aa1 | |
| parent | 63e2e56d3172bdc80aaca5f5ddde5811728e3c76 (diff) | |
feature #2276 Single Executable: CommandLine can be instantiated
refactor CommandLine to be an object
Signed-off-by: Jesse Wolfe <jes5199@gmail.com>
| -rwxr-xr-x | bin/puppet | 18 | ||||
| -rw-r--r-- | lib/puppet/util/command_line.rb | 44 | ||||
| -rw-r--r-- | spec/unit/util/command_line.rb | 17 |
3 files changed, 61 insertions, 18 deletions
diff --git a/bin/puppet b/bin/puppet index a326ba148..9b7c7d64d 100755 --- a/bin/puppet +++ b/bin/puppet @@ -69,21 +69,5 @@ #Puppet::Application[:apply].run # this is so the RDoc::usage hack can find this file -appdir = File.join('puppet', 'application') -absolute_appdir = $:.collect { |x| File.join(x,'puppet','application') }.detect{ |x| File.directory?(x) } -builtins = Dir[File.join(absolute_appdir, '*.rb')].map{|fn| File.basename(fn, '.rb')} - -usage = "Usage: puppet command <space separated arguments>" -available = "Available commands are: #{builtins.sort.join(', ')}" - require 'puppet/util/command_line' -subcommand_name = Puppet::Util::CommandLine.subcommand_name - -if subcommand_name.nil? - puts usage, available -elsif builtins.include?(subcommand_name) #subcommand - require File.join(appdir, subcommand_name) - Puppet::Application[subcommand_name].run -else - abort "Error: Unknown command #{subcommand_name}.\n#{usage}\n#{available}" -end +Puppet::Util::CommandLine.new.execute diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb index 5fe07b28d..6e67fd706 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -1,6 +1,6 @@ module Puppet module Util - module CommandLine + class CommandLine def self.subcommand_name(*args) subcommand_name, args = subcommand_and_args(*args) return subcommand_name @@ -42,6 +42,48 @@ module Puppet [ zero, argv ] end end + + def self.appdir + File.join('puppet', 'application') + end + + def self.available_subcommands + appdir = File.join('puppet', 'application') + 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 + 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) + Puppet::Application[subcommand_name].run + else + abort "Error: Unknown command #{subcommand_name}.\n#{usage_message}" + end + end + + def legacy_executable_name + LegacyName[ subcommand_name ] + end end end end diff --git a/spec/unit/util/command_line.rb b/spec/unit/util/command_line.rb index a07438f9e..e20f51a99 100644 --- a/spec/unit/util/command_line.rb +++ b/spec/unit/util/command_line.rb @@ -93,4 +93,21 @@ describe Puppet::Util::CommandLine 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 + end + end |
