From 05b434dca10bbc18d794358a9d08db89a46424f9 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Mon, 28 Mar 2011 21:37:05 -0700 Subject: (#6758) Pass options as an argument to string actions. Earlier in their implementation the String prototype would set global state on a String object to reflect options set on the command line. As we move strings away from a CLI-only prototype, this becomes troublesome because we can easily have, for example, HTTP access to a string, which means load balancers can really make this confusing. It also encourages global state pollution, where one invocation can adversely influence another. A better approach is that we pass options to the string action invocation directly; this makes the interaction stateless. Changes required to your code to adapt to the new world: - action(:foo) do |some, args| + action(:foo) do |some, args, options={}| if options[:whatever] then Reviewed-By: Pieter van de Bruggen --- lib/puppet/application/string_base.rb | 13 +++++-------- lib/puppet/string.rb | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb index bc627adde..762fbfda8 100644 --- a/lib/puppet/application/string_base.rb +++ b/lib/puppet/application/string_base.rb @@ -63,11 +63,12 @@ class Puppet::Application::StringBase < Puppet::Application def setup Puppet::Util::Log.newdestination :console + # We copy all of the app options to the end of the call; This allows each + # action to read in the options. This replaces the older model where we + # would invoke the action with options set as global state in the + # interface object. --daniel 2011-03-28 @verb = command_line.args.shift - @arguments = command_line.args - @arguments ||= [] - - @arguments = Array(@arguments) + @arguments = Array(command_line.args) << options @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym @@ -78,10 +79,6 @@ class Puppet::Application::StringBase < Puppet::Application @string = Puppet::String[@type, :current] @format ||= @string.default_format - # We copy all of the app options to the string. - # This allows each action to read in the options. - @string.options = options - validate end diff --git a/lib/puppet/string.rb b/lib/puppet/string.rb index 783b6afe0..04db1f33b 100644 --- a/lib/puppet/string.rb +++ b/lib/puppet/string.rb @@ -53,7 +53,7 @@ class Puppet::String self.default_format = format.to_sym end - attr_accessor :type, :verb, :version, :arguments, :options + attr_accessor :type, :verb, :version, :arguments attr_reader :name def initialize(name, version, &block) -- cgit