From 3696d951f70f5b94b49619dfbc57138d5241bbb8 Mon Sep 17 00:00:00 2001 From: Jesse Wolfe Date: Mon, 24 May 2010 17:04:17 -0700 Subject: [#3865] External subcommands This patch allows the puppet single-executable to invoke external, hyphenated subcommands, much like how git does. --- lib/puppet/util/command_line.rb | 15 ++++++++++++++- spec/unit/util/command_line.rb | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/puppet/util/command_line.rb b/lib/puppet/util/command_line.rb index 8a010bb76..8c8eb91d5 100644 --- a/lib/puppet/util/command_line.rb +++ b/lib/puppet/util/command_line.rb @@ -51,10 +51,23 @@ module Puppet require_application subcommand_name Puppet::Application.find(subcommand_name).new(self).run else - abort "Error: Unknown command #{subcommand_name}.\n#{usage_message}" + unless execute_external_subcommand + abort "Error: Unknown command #{subcommand_name}.\n#{usage_message}" + end end end + def execute_external_subcommand + external_command = "puppet-#{subcommand_name}" + + require 'puppet/util' + path_to_subcommand = Puppet::Util.binary( external_command ) + return false unless path_to_subcommand + + system( path_to_subcommand, *args ) + true + end + def legacy_executable_name LegacyName[ subcommand_name ] end diff --git a/spec/unit/util/command_line.rb b/spec/unit/util/command_line.rb index 7413e57d1..31b2d1b03 100644 --- a/spec/unit/util/command_line.rb +++ b/spec/unit/util/command_line.rb @@ -83,4 +83,26 @@ describe Puppet::Util::CommandLine do command_line.legacy_executable_name.should == "puppetmasterd" end + describe "when the subcommand is not implemented" do + it "should find and invoke an executable with a hyphenated name" do + commandline = Puppet::Util::CommandLine.new("puppet", ['whatever', 'argument'], @tty) + Puppet::Util.expects(:binary).with('puppet-whatever').returns('/dev/null/puppet-whatever') + commandline.expects(:system).with('/dev/null/puppet-whatever', 'argument') + + commandline.execute + end + + describe "and an external implementation cannot be found" do + it "should abort and show the usage message" do + commandline = Puppet::Util::CommandLine.new("puppet", ['whatever', 'argument'], @tty) + Puppet::Util.expects(:binary).with('puppet-whatever').returns(nil) + commandline.expects(:system).never + + commandline.expects(:usage_message).returns("the usage message") + commandline.expects(:abort).with{|x| x =~ /the usage message/}.raises("stubbed abort") + + lambda{ commandline.execute }.should raise_error('stubbed abort') + end + end + end end -- cgit v1.2.3