summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Wolfe <jes5199@gmail.com>2010-04-22 12:04:10 -0700
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commit7656ba73ddfd883b36a01c81147ae69e80773bce (patch)
tree5bc7cad1485bb9749ce387336b30794e3ae93aa1 /lib
parent63e2e56d3172bdc80aaca5f5ddde5811728e3c76 (diff)
downloadpuppet-7656ba73ddfd883b36a01c81147ae69e80773bce.tar.gz
puppet-7656ba73ddfd883b36a01c81147ae69e80773bce.tar.xz
puppet-7656ba73ddfd883b36a01c81147ae69e80773bce.zip
feature #2276 Single Executable: CommandLine can be instantiated
refactor CommandLine to be an object Signed-off-by: Jesse Wolfe <jes5199@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/util/command_line.rb44
1 files changed, 43 insertions, 1 deletions
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