diff options
author | Jesse Wolfe <jes5199@gmail.com> | 2010-04-30 15:47:15 -0700 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 5683fd983b9a165ffbb8f08e67cfe903ec0e41b7 (patch) | |
tree | b1a908be53502201b5ee0f1b37aae8e57a14bf37 | |
parent | d038a1d3ddffdf1366c78fe31118e9f15c1c6ed1 (diff) | |
download | puppet-5683fd983b9a165ffbb8f08e67cfe903ec0e41b7.tar.gz puppet-5683fd983b9a165ffbb8f08e67cfe903ec0e41b7.tar.xz puppet-5683fd983b9a165ffbb8f08e67cfe903ec0e41b7.zip |
Feature #2276 Single Executable: Pass a commandline object to the application
Refactor so that the command line options only get parsed once
Signed-off-by: Jesse Wolfe <jes5199@gmail.com>
-rw-r--r-- | lib/puppet/application.rb | 5 | ||||
-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 | 2 | ||||
-rw-r--r-- | lib/puppet/application/resource.rb | 2 | ||||
-rw-r--r-- | lib/puppet/util/command_line.rb | 2 | ||||
-rwxr-xr-x | spec/unit/application/apply.rb | 4 | ||||
-rw-r--r-- | spec/unit/application/cert.rb | 10 | ||||
-rwxr-xr-x | spec/unit/application/doc.rb | 6 | ||||
-rw-r--r-- | spec/unit/application/filebucket.rb | 2 | ||||
-rwxr-xr-x | spec/unit/application/resource.rb | 13 |
13 files changed, 32 insertions, 30 deletions
diff --git a/lib/puppet/application.rb b/lib/puppet/application.rb index 49a146ffe..f72714b12 100644 --- a/lib/puppet/application.rb +++ b/lib/puppet/application.rb @@ -229,7 +229,7 @@ class Puppet::Application end end - attr_reader :options, :opt_parser + attr_reader :options, :opt_parser, :command_line # Every app responds to --version option("--version", "-V") do |arg| @@ -250,7 +250,8 @@ class Puppet::Application def preinit end - def initialize + def initialize(command_line = nil) + @command_line = command_line || Puppet::Util::CommandLine.new @opt_parser = self.class.new_option_parser( self ) @options = {} diff --git a/lib/puppet/application/apply.rb b/lib/puppet/application/apply.rb index 88385f0a0..fde0c9b8d 100644 --- a/lib/puppet/application/apply.rb +++ b/lib/puppet/application/apply.rb @@ -66,10 +66,10 @@ class Puppet::Application::Apply < Puppet::Application def parseonly # Set our code or file to use. - if options[:code] or Puppet::Util::CommandLine.args.length == 0 + if options[:code] or command_line.args.length == 0 Puppet[:code] = options[:code] || STDIN.read else - Puppet[:manifest] = Puppet::Util::CommandLine.args.shift + Puppet[:manifest] = command_line.args.shift end begin Puppet::Resource::TypeCollection.new(Puppet[:environment]).perform_initial_import @@ -82,10 +82,10 @@ class Puppet::Application::Apply < Puppet::Application def main # Set our code or file to use. - if options[:code] or Puppet::Util::CommandLine.args.length == 0 + if options[:code] or command_line.args.length == 0 Puppet[:code] = options[:code] || STDIN.read else - Puppet[:manifest] = Puppet::Util::CommandLine.args.shift + Puppet[:manifest] = command_line.args.shift end # Collect our facts. diff --git a/lib/puppet/application/cert.rb b/lib/puppet/application/cert.rb index 92da03ca7..a4ee5fef3 100644 --- a/lib/puppet/application/cert.rb +++ b/lib/puppet/application/cert.rb @@ -44,7 +44,7 @@ class Puppet::Application::Cert < Puppet::Application if @all hosts = :all else - hosts = Puppet::Util::CommandLine.args.collect { |h| puts h; h.downcase } + hosts = command_line.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 45f017a48..ae33fb92a 100644 --- a/lib/puppet/application/describe.rb +++ b/lib/puppet/application/describe.rb @@ -203,7 +203,7 @@ class Puppet::Application::Describe < Puppet::Application end def setup - options[:types] = Puppet::Util::CommandLine.args.dup + options[:types] = command_line.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 295cd6771..74cde98bd 100644 --- a/lib/puppet/application/doc.rb +++ b/lib/puppet/application/doc.rb @@ -70,7 +70,7 @@ class Puppet::Application::Doc < Puppet::Application files += env.modulepath files << File.dirname(env[:manifest]) end - files += Puppet::Util::CommandLine.args + files += command_line.args Puppet.info "scanning: %s" % files.inspect Puppet.settings.setdefaults("puppetdoc", "document_all" => [false, "Document all resources"] @@ -167,7 +167,7 @@ class Puppet::Application::Doc < Puppet::Application def setup # sole manifest documentation - if Puppet::Util::CommandLine.args.size > 0 + if command_line.args.size > 0 options[:mode] = :rdoc @manifest = true end diff --git a/lib/puppet/application/filebucket.rb b/lib/puppet/application/filebucket.rb index ddc46e394..8e930f5e4 100644 --- a/lib/puppet/application/filebucket.rb +++ b/lib/puppet/application/filebucket.rb @@ -15,7 +15,7 @@ class Puppet::Application::Filebucket < Puppet::Application attr :args def run_command - @args = Puppet::Util::CommandLine.args + @args = command_line.args command = args.shift return send(command) if %w[get backup restore].include? command help diff --git a/lib/puppet/application/resource.rb b/lib/puppet/application/resource.rb index 52320e7a1..6a8c4e47c 100644 --- a/lib/puppet/application/resource.rb +++ b/lib/puppet/application/resource.rb @@ -38,7 +38,7 @@ class Puppet::Application::Resource < Puppet::Application end def main - args = Puppet::Util::CommandLine.args + args = command_line.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 = args.shift diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb index 6e67fd706..e6656985f 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -75,7 +75,7 @@ module Puppet 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 + Puppet::Application.find(subcommand_name).new(self).run else abort "Error: Unknown command #{subcommand_name}.\n#{usage_message}" end diff --git a/spec/unit/application/apply.rb b/spec/unit/application/apply.rb index 10202e196..a1a7e7b31 100755 --- a/spec/unit/application/apply.rb +++ b/spec/unit/application/apply.rb @@ -204,7 +204,7 @@ describe Puppet::Application::Apply do end it "should set the code to run from STDIN if no arguments" do - Puppet::Util::CommandLine.stubs(:args).returns([]) + @apply.command_line.stubs(:args).returns([]) STDIN.stubs(:read).returns("code to run") Puppet.expects(:[]=).with(:code,"code to run") @@ -213,7 +213,7 @@ describe Puppet::Application::Apply do end it "should set the manifest if some files are passed on command line" do - Puppet::Util::CommandLine.stubs(:args).returns(['site.pp']) + @apply.command_line.stubs(:args).returns(['site.pp']) Puppet.expects(:[]=).with(:manifest,"site.pp") diff --git a/spec/unit/application/cert.rb b/spec/unit/application/cert.rb index 3fb2f1ed4..5970355e8 100644 --- a/spec/unit/application/cert.rb +++ b/spec/unit/application/cert.rb @@ -110,7 +110,7 @@ describe Puppet::Application::Cert do @cert_app.all = false @ca = stub_everything 'ca' @cert_app.ca = @ca - Puppet::Util::CommandLine.stubs(:args).returns([]) + @cert_app.command_line.stubs(:args).returns([]) end it "should delegate to the CertificateAuthority" do @@ -128,7 +128,7 @@ describe Puppet::Application::Cert do end it "should delegate to ca.apply with the hosts given on command line" do - Puppet::Util::CommandLine.stubs(:args).returns(["host"]) + @cert_app.command_line.stubs(:args).returns(["host"]) @ca.expects(:apply).with { |mode,to| to[:to] == ["host"]} @@ -136,7 +136,7 @@ describe Puppet::Application::Cert do end it "should send the currently set digest" do - Puppet::Util::CommandLine.stubs(:args).returns(["host"]) + @cert_app.command_line.stubs(:args).returns(["host"]) @cert_app.handle_digest(:digest) @ca.expects(:apply).with { |mode,to| to[:digest] == :digest} @@ -146,7 +146,7 @@ describe Puppet::Application::Cert do it "should delegate to ca.apply with current set mode" do @cert_app.mode = "currentmode" - Puppet::Util::CommandLine.stubs(:args).returns(["host"]) + @cert_app.command_line.stubs(:args).returns(["host"]) @ca.expects(:apply).with { |mode,to| mode == "currentmode" } @@ -155,7 +155,7 @@ describe Puppet::Application::Cert do it "should revoke cert if mode is clean" do @cert_app.mode = :destroy - Puppet::Util::CommandLine.stubs(:args).returns(["host"]) + @cert_app.command_line.stubs(:args).returns(["host"]) @ca.expects(:apply).with { |mode,to| mode == :revoke } @ca.expects(:apply).with { |mode,to| mode == :destroy } diff --git a/spec/unit/application/doc.rb b/spec/unit/application/doc.rb index 3bda25f2f..c1184925e 100755 --- a/spec/unit/application/doc.rb +++ b/spec/unit/application/doc.rb @@ -128,11 +128,11 @@ describe Puppet::Application::Doc do before :each do Puppet::Log.stubs(:newdestination) - Puppet::Util::CommandLine.stubs(:args).returns([]) + @doc.command_line.stubs(:args).returns([]) end it "should default to rdoc mode if there are command line arguments" do - Puppet::Util::CommandLine.stubs(:args).returns(["1"]) + @doc.command_line.stubs(:args).returns(["1"]) @doc.stubs(:setup_rdoc) @doc.options.expects(:[]=).with(:mode,:rdoc) @@ -304,7 +304,7 @@ describe Puppet::Application::Doc do @doc.stubs(:exit) File.stubs(:expand_path).with('modules').returns('modules') File.stubs(:expand_path).with('manifests').returns('manifests') - Puppet::Util::CommandLine.stubs(:args).returns([]) + @doc.command_line.stubs(:args).returns([]) end it "should set document_all on --all" do diff --git a/spec/unit/application/filebucket.rb b/spec/unit/application/filebucket.rb index 68af45c40..58c7f7c75 100644 --- a/spec/unit/application/filebucket.rb +++ b/spec/unit/application/filebucket.rb @@ -152,7 +152,7 @@ describe Puppet::Application::Filebucket do end it "should use the first non-option parameter as the dispatch" do - Puppet::Util::CommandLine.stubs(:args).returns([:get]) + @filebucket.command_line.stubs(:args).returns(['get']) @filebucket.expects(:get) diff --git a/spec/unit/application/resource.rb b/spec/unit/application/resource.rb index 2e90cc288..3487b2c3c 100755 --- a/spec/unit/application/resource.rb +++ b/spec/unit/application/resource.rb @@ -135,12 +135,12 @@ describe Puppet::Application::Resource do before :each do @type = stub_everything 'type', :properties => [] - Puppet::Util::CommandLine.stubs(:args).returns(['type']) + @resource.command_line.stubs(:args).returns(['type']) Puppet::Type.stubs(:type).returns(@type) end it "should raise an error if no type is given" do - Puppet::Util::CommandLine.stubs(:args).returns([]) + @resource.command_line.stubs(:args).returns([]) lambda { @resource.main }.should raise_error end @@ -168,19 +168,20 @@ describe Puppet::Application::Resource do end it "should search for resources" do + @resource.command_line.stubs(:args).returns(['type']) Puppet::Resource.expects(:search).with('https://host:8139/production/resources/type/', {}).returns([]) @resource.main end it "should describe the given resource" do - Puppet::Util::CommandLine.stubs(:args).returns(['type', 'name']) + @resource.command_line.stubs(:args).returns(['type', 'name']) x = stub_everything 'resource' Puppet::Resource.expects(:find).with('https://host:8139/production/resources/type/name').returns(x) @resource.main end it "should add given parameters to the object" do - Puppet::Util::CommandLine.stubs(:args).returns(['type','name','param=temp']) + @resource.command_line.stubs(:args).returns(['type','name','param=temp']) res = stub "resource" res.expects(:save).with('https://host:8139/production/resources/type/name').returns(res) @@ -209,14 +210,14 @@ describe Puppet::Application::Resource do end it "should describe the given resource" do - Puppet::Util::CommandLine.stubs(:args).returns(['type','name']) + @resource.command_line.stubs(:args).returns(['type','name']) x = stub_everything 'resource' Puppet::Resource.expects(:find).with('type/name').returns(x) @resource.main end it "should add given parameters to the object" do - Puppet::Util::CommandLine.stubs(:args).returns(['type','name','param=temp']) + @resource.command_line.stubs(:args).returns(['type','name','param=temp']) res = stub "resource" res.expects(:save).with('type/name').returns(res) |