From 5592034fdb8bf3a72ab3133d69443490f9ad7b78 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 7 Apr 2011 13:18:26 -0700 Subject: (#7012) global rename of strings to faces. This just changes filenames and directories; files are exact copies rather than having additional modifications to make clearer each step of this process. This does leave a currently broken build. :/ --- lib/puppet/application/faces.rb | 95 +++++++++++++++++++++ lib/puppet/application/faces_base.rb | 150 ++++++++++++++++++++++++++++++++++ lib/puppet/application/string.rb | 95 --------------------- lib/puppet/application/string_base.rb | 150 ---------------------------------- 4 files changed, 245 insertions(+), 245 deletions(-) create mode 100644 lib/puppet/application/faces.rb create mode 100644 lib/puppet/application/faces_base.rb delete mode 100644 lib/puppet/application/string.rb delete mode 100644 lib/puppet/application/string_base.rb (limited to 'lib/puppet/application') diff --git a/lib/puppet/application/faces.rb b/lib/puppet/application/faces.rb new file mode 100644 index 000000000..0a6a798ce --- /dev/null +++ b/lib/puppet/application/faces.rb @@ -0,0 +1,95 @@ +require 'puppet/application' +require 'puppet/string' + +class Puppet::Application::String < Puppet::Application + + should_parse_config + run_mode :agent + + option("--debug", "-d") do |arg| + Puppet::Util::Log.level = :debug + end + + option("--verbose", "-v") do + Puppet::Util::Log.level = :info + end + + def list(*arguments) + if arguments.empty? + arguments = %w{terminuses actions} + end + strings.each do |name| + str = "#{name}:\n" + if arguments.include?("terminuses") + begin + terms = terminus_classes(name.to_sym) + str << "\tTerminuses: #{terms.join(", ")}\n" + rescue => detail + puts detail.backtrace if Puppet[:trace] + $stderr.puts "Could not load terminuses for #{name}: #{detail}" + end + end + + if arguments.include?("actions") + begin + actions = actions(name.to_sym) + str << "\tActions: #{actions.join(", ")}\n" + rescue => detail + puts detail.backtrace if Puppet[:trace] + $stderr.puts "Could not load actions for #{name}: #{detail}" + end + end + + print str + end + end + + attr_accessor :verb, :name, :arguments + + def main + # Call the method associated with the provided action (e.g., 'find'). + send(verb, *arguments) + end + + def setup + Puppet::Util::Log.newdestination :console + + load_applications # Call this to load all of the apps + + @verb, @arguments = command_line.args + @arguments ||= [] + + validate + end + + def validate + unless verb + raise "You must specify 'find', 'search', 'save', or 'destroy' as a verb; 'save' probably does not work right now" + end + + unless respond_to?(verb) + raise "Command '#{verb}' not found for 'string'" + end + end + + def strings + Puppet::String.strings + end + + def terminus_classes(indirection) + Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort + end + + def actions(indirection) + return [] unless string = Puppet::String[indirection, '0.0.1'] + string.load_actions + return string.actions.sort { |a, b| a.to_s <=> b.to_s } + end + + def load_applications + command_line.available_subcommands.each do |app| + command_line.require_application app + end + end +end + diff --git a/lib/puppet/application/faces_base.rb b/lib/puppet/application/faces_base.rb new file mode 100644 index 000000000..09d02c079 --- /dev/null +++ b/lib/puppet/application/faces_base.rb @@ -0,0 +1,150 @@ +require 'puppet/application' +require 'puppet/string' + +class Puppet::Application::StringBase < Puppet::Application + should_parse_config + run_mode :agent + + option("--debug", "-d") do |arg| + Puppet::Util::Log.level = :debug + end + + option("--verbose", "-v") do + Puppet::Util::Log.level = :info + end + + option("--format FORMAT") do |arg| + @format = arg.to_sym + end + + option("--mode RUNMODE", "-r") do |arg| + raise "Invalid run mode #{arg}; supported modes are user, agent, master" unless %w{user agent master}.include?(arg) + self.class.run_mode(arg.to_sym) + set_run_mode self.class.run_mode + end + + + attr_accessor :string, :action, :type, :arguments, :format + attr_writer :exit_code + + # This allows you to set the exit code if you don't want to just exit + # immediately but you need to indicate a failure. + def exit_code + @exit_code || 0 + end + + # Override this if you need custom rendering. + def render(result) + render_method = Puppet::Network::FormatHandler.format(format).render_method + if render_method == "to_pson" + jj result + exit(0) + else + result.send(render_method) + end + end + + def preinit + super + trap(:INT) do + $stderr.puts "Cancelling String" + exit(0) + end + + # We need to parse enough of the command line out early, to identify what + # the action is, so that we can obtain the full set of options to parse. + + # TODO: These should be configurable versions, through a global + # '--version' option, but we don't implement that yet... --daniel 2011-03-29 + @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym + @string = Puppet::String[@type, :current] + @format = @string.default_format + + # Now, walk the command line and identify the action. We skip over + # arguments based on introspecting the action and all, and find the first + # non-option word to use as the action. + action = nil + index = -1 + until @action or (index += 1) >= command_line.args.length do + item = command_line.args[index] + if item =~ /^-/ then + option = @string.options.find do |name| + item =~ /^-+#{name.to_s.gsub(/[-_]/, '[-_]')}(?:[ =].*)?$/ + end + if option then + option = @string.get_option(option) + # If we have an inline argument, just carry on. We don't need to + # care about optional vs mandatory in that case because we do a real + # parse later, and that will totally take care of raising the error + # when we get there. --daniel 2011-04-04 + if option.takes_argument? and !item.index('=') then + index += 1 unless + (option.optional_argument? and command_line.args[index + 1] =~ /^-/) + end + elsif option = find_global_settings_argument(item) then + unless Puppet.settings.boolean? option.name then + # As far as I can tell, we treat non-bool options as always having + # a mandatory argument. --daniel 2011-04-05 + index += 1 # ...so skip the argument. + end + else + raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}" + end + else + action = @string.get_action(item.to_sym) + if action.nil? then + raise ArgumentError, "#{@string} does not have an #{item.inspect} action!" + end + @action = action + end + end + + @action or raise ArgumentError, "No action given on the command line!" + + # Finally, we can interact with the default option code to build behaviour + # around the full set of options we now know we support. + @action.options.each do |option| + option = @action.get_option(option) # make it the object. + self.class.option(*option.optparse) # ...and make the CLI parse it. + end + end + + def find_global_settings_argument(item) + Puppet.settings.each do |name, object| + object.optparse_args.each do |arg| + next unless arg =~ /^-/ + # sadly, we have to emulate some of optparse here... + pattern = /^#{arg.sub('[no-]', '').sub(/[ =].*$/, '')}(?:[ =].*)?$/ + pattern.match item and return object + end + end + return nil # nothing found. + end + + def setup + Puppet::Util::Log.newdestination :console + + @arguments = command_line.args + + # Note: because of our definition of where the action is set, we end up + # with it *always* being the first word of the remaining set of command + # line arguments. So, strip that off when we construct the arguments to + # pass down to the string action. --daniel 2011-04-04 + @arguments.delete_at(0) + + # 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 + @arguments << options + end + + + def main + # Call the method associated with the provided action (e.g., 'find'). + if result = @string.send(@action.name, *arguments) + puts render(result) + end + exit(exit_code) + end +end diff --git a/lib/puppet/application/string.rb b/lib/puppet/application/string.rb deleted file mode 100644 index 0a6a798ce..000000000 --- a/lib/puppet/application/string.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'puppet/application' -require 'puppet/string' - -class Puppet::Application::String < Puppet::Application - - should_parse_config - run_mode :agent - - option("--debug", "-d") do |arg| - Puppet::Util::Log.level = :debug - end - - option("--verbose", "-v") do - Puppet::Util::Log.level = :info - end - - def list(*arguments) - if arguments.empty? - arguments = %w{terminuses actions} - end - strings.each do |name| - str = "#{name}:\n" - if arguments.include?("terminuses") - begin - terms = terminus_classes(name.to_sym) - str << "\tTerminuses: #{terms.join(", ")}\n" - rescue => detail - puts detail.backtrace if Puppet[:trace] - $stderr.puts "Could not load terminuses for #{name}: #{detail}" - end - end - - if arguments.include?("actions") - begin - actions = actions(name.to_sym) - str << "\tActions: #{actions.join(", ")}\n" - rescue => detail - puts detail.backtrace if Puppet[:trace] - $stderr.puts "Could not load actions for #{name}: #{detail}" - end - end - - print str - end - end - - attr_accessor :verb, :name, :arguments - - def main - # Call the method associated with the provided action (e.g., 'find'). - send(verb, *arguments) - end - - def setup - Puppet::Util::Log.newdestination :console - - load_applications # Call this to load all of the apps - - @verb, @arguments = command_line.args - @arguments ||= [] - - validate - end - - def validate - unless verb - raise "You must specify 'find', 'search', 'save', or 'destroy' as a verb; 'save' probably does not work right now" - end - - unless respond_to?(verb) - raise "Command '#{verb}' not found for 'string'" - end - end - - def strings - Puppet::String.strings - end - - def terminus_classes(indirection) - Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort - end - - def actions(indirection) - return [] unless string = Puppet::String[indirection, '0.0.1'] - string.load_actions - return string.actions.sort { |a, b| a.to_s <=> b.to_s } - end - - def load_applications - command_line.available_subcommands.each do |app| - command_line.require_application app - end - end -end - diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb deleted file mode 100644 index 09d02c079..000000000 --- a/lib/puppet/application/string_base.rb +++ /dev/null @@ -1,150 +0,0 @@ -require 'puppet/application' -require 'puppet/string' - -class Puppet::Application::StringBase < Puppet::Application - should_parse_config - run_mode :agent - - option("--debug", "-d") do |arg| - Puppet::Util::Log.level = :debug - end - - option("--verbose", "-v") do - Puppet::Util::Log.level = :info - end - - option("--format FORMAT") do |arg| - @format = arg.to_sym - end - - option("--mode RUNMODE", "-r") do |arg| - raise "Invalid run mode #{arg}; supported modes are user, agent, master" unless %w{user agent master}.include?(arg) - self.class.run_mode(arg.to_sym) - set_run_mode self.class.run_mode - end - - - attr_accessor :string, :action, :type, :arguments, :format - attr_writer :exit_code - - # This allows you to set the exit code if you don't want to just exit - # immediately but you need to indicate a failure. - def exit_code - @exit_code || 0 - end - - # Override this if you need custom rendering. - def render(result) - render_method = Puppet::Network::FormatHandler.format(format).render_method - if render_method == "to_pson" - jj result - exit(0) - else - result.send(render_method) - end - end - - def preinit - super - trap(:INT) do - $stderr.puts "Cancelling String" - exit(0) - end - - # We need to parse enough of the command line out early, to identify what - # the action is, so that we can obtain the full set of options to parse. - - # TODO: These should be configurable versions, through a global - # '--version' option, but we don't implement that yet... --daniel 2011-03-29 - @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym - @string = Puppet::String[@type, :current] - @format = @string.default_format - - # Now, walk the command line and identify the action. We skip over - # arguments based on introspecting the action and all, and find the first - # non-option word to use as the action. - action = nil - index = -1 - until @action or (index += 1) >= command_line.args.length do - item = command_line.args[index] - if item =~ /^-/ then - option = @string.options.find do |name| - item =~ /^-+#{name.to_s.gsub(/[-_]/, '[-_]')}(?:[ =].*)?$/ - end - if option then - option = @string.get_option(option) - # If we have an inline argument, just carry on. We don't need to - # care about optional vs mandatory in that case because we do a real - # parse later, and that will totally take care of raising the error - # when we get there. --daniel 2011-04-04 - if option.takes_argument? and !item.index('=') then - index += 1 unless - (option.optional_argument? and command_line.args[index + 1] =~ /^-/) - end - elsif option = find_global_settings_argument(item) then - unless Puppet.settings.boolean? option.name then - # As far as I can tell, we treat non-bool options as always having - # a mandatory argument. --daniel 2011-04-05 - index += 1 # ...so skip the argument. - end - else - raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}" - end - else - action = @string.get_action(item.to_sym) - if action.nil? then - raise ArgumentError, "#{@string} does not have an #{item.inspect} action!" - end - @action = action - end - end - - @action or raise ArgumentError, "No action given on the command line!" - - # Finally, we can interact with the default option code to build behaviour - # around the full set of options we now know we support. - @action.options.each do |option| - option = @action.get_option(option) # make it the object. - self.class.option(*option.optparse) # ...and make the CLI parse it. - end - end - - def find_global_settings_argument(item) - Puppet.settings.each do |name, object| - object.optparse_args.each do |arg| - next unless arg =~ /^-/ - # sadly, we have to emulate some of optparse here... - pattern = /^#{arg.sub('[no-]', '').sub(/[ =].*$/, '')}(?:[ =].*)?$/ - pattern.match item and return object - end - end - return nil # nothing found. - end - - def setup - Puppet::Util::Log.newdestination :console - - @arguments = command_line.args - - # Note: because of our definition of where the action is set, we end up - # with it *always* being the first word of the remaining set of command - # line arguments. So, strip that off when we construct the arguments to - # pass down to the string action. --daniel 2011-04-04 - @arguments.delete_at(0) - - # 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 - @arguments << options - end - - - def main - # Call the method associated with the provided action (e.g., 'find'). - if result = @string.send(@action.name, *arguments) - puts render(result) - end - exit(exit_code) - end -end -- cgit From 8d144d0bf5116c5f04522f2b4cd75699f6480f8e Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 7 Apr 2011 14:20:35 -0700 Subject: (#7012) Update references in code to use face(s) The codebase is now using the new name, faces, uniformly to reference the objects contained. All tests pass. --- lib/puppet/application/config.rb | 4 ++-- lib/puppet/application/configurer.rb | 6 +++--- lib/puppet/application/faces.rb | 18 +++++++++--------- lib/puppet/application/faces_base.rb | 24 ++++++++++++------------ lib/puppet/application/indirection_base.rb | 4 ++-- 5 files changed, 28 insertions(+), 28 deletions(-) (limited to 'lib/puppet/application') diff --git a/lib/puppet/application/config.rb b/lib/puppet/application/config.rb index f6559277b..41a46c339 100644 --- a/lib/puppet/application/config.rb +++ b/lib/puppet/application/config.rb @@ -1,4 +1,4 @@ -require 'puppet/application/string_base' +require 'puppet/application/faces_base' -class Puppet::Application::Config < Puppet::Application::StringBase +class Puppet::Application::Config < Puppet::Application::FacesBase end diff --git a/lib/puppet/application/configurer.rb b/lib/puppet/application/configurer.rb index be018338f..751e6b4d7 100644 --- a/lib/puppet/application/configurer.rb +++ b/lib/puppet/application/configurer.rb @@ -1,5 +1,5 @@ require 'puppet/application' -require 'puppet/string' +require 'puppet/faces' class Puppet::Application::Configurer < Puppet::Application should_parse_config @@ -17,7 +17,7 @@ class Puppet::Application::Configurer < Puppet::Application end def run_command - report = Puppet::String[:configurer, '0.0.1'].synchronize(Puppet[:certname]) - Puppet::String[:report, '0.0.1'].submit(report) + report = Puppet::Faces[:configurer, '0.0.1'].synchronize(Puppet[:certname]) + Puppet::Faces[:report, '0.0.1'].submit(report) end end diff --git a/lib/puppet/application/faces.rb b/lib/puppet/application/faces.rb index 0a6a798ce..904a0cccc 100644 --- a/lib/puppet/application/faces.rb +++ b/lib/puppet/application/faces.rb @@ -1,7 +1,7 @@ require 'puppet/application' -require 'puppet/string' +require 'puppet/faces' -class Puppet::Application::String < Puppet::Application +class Puppet::Application::Faces < Puppet::Application should_parse_config run_mode :agent @@ -18,7 +18,7 @@ class Puppet::Application::String < Puppet::Application if arguments.empty? arguments = %w{terminuses actions} end - strings.each do |name| + faces.each do |name| str = "#{name}:\n" if arguments.include?("terminuses") begin @@ -68,12 +68,12 @@ class Puppet::Application::String < Puppet::Application end unless respond_to?(verb) - raise "Command '#{verb}' not found for 'string'" + raise "Command '#{verb}' not found for 'faces'" end end - def strings - Puppet::String.strings + def faces + Puppet::Faces.faces end def terminus_classes(indirection) @@ -81,9 +81,9 @@ class Puppet::Application::String < Puppet::Application end def actions(indirection) - return [] unless string = Puppet::String[indirection, '0.0.1'] - string.load_actions - return string.actions.sort { |a, b| a.to_s <=> b.to_s } + return [] unless faces = Puppet::Faces[indirection, '0.0.1'] + faces.load_actions + return faces.actions.sort { |a, b| a.to_s <=> b.to_s } end def load_applications diff --git a/lib/puppet/application/faces_base.rb b/lib/puppet/application/faces_base.rb index 09d02c079..6d66ee8a1 100644 --- a/lib/puppet/application/faces_base.rb +++ b/lib/puppet/application/faces_base.rb @@ -1,7 +1,7 @@ require 'puppet/application' -require 'puppet/string' +require 'puppet/faces' -class Puppet::Application::StringBase < Puppet::Application +class Puppet::Application::FacesBase < Puppet::Application should_parse_config run_mode :agent @@ -24,7 +24,7 @@ class Puppet::Application::StringBase < Puppet::Application end - attr_accessor :string, :action, :type, :arguments, :format + attr_accessor :face, :action, :type, :arguments, :format attr_writer :exit_code # This allows you to set the exit code if you don't want to just exit @@ -47,7 +47,7 @@ class Puppet::Application::StringBase < Puppet::Application def preinit super trap(:INT) do - $stderr.puts "Cancelling String" + $stderr.puts "Cancelling Face" exit(0) end @@ -57,8 +57,8 @@ class Puppet::Application::StringBase < Puppet::Application # TODO: These should be configurable versions, through a global # '--version' option, but we don't implement that yet... --daniel 2011-03-29 @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym - @string = Puppet::String[@type, :current] - @format = @string.default_format + @face = Puppet::Faces[@type, :current] + @format = @face.default_format # Now, walk the command line and identify the action. We skip over # arguments based on introspecting the action and all, and find the first @@ -68,11 +68,11 @@ class Puppet::Application::StringBase < Puppet::Application until @action or (index += 1) >= command_line.args.length do item = command_line.args[index] if item =~ /^-/ then - option = @string.options.find do |name| + option = @face.options.find do |name| item =~ /^-+#{name.to_s.gsub(/[-_]/, '[-_]')}(?:[ =].*)?$/ end if option then - option = @string.get_option(option) + option = @face.get_option(option) # If we have an inline argument, just carry on. We don't need to # care about optional vs mandatory in that case because we do a real # parse later, and that will totally take care of raising the error @@ -91,9 +91,9 @@ class Puppet::Application::StringBase < Puppet::Application raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}" end else - action = @string.get_action(item.to_sym) + action = @face.get_action(item.to_sym) if action.nil? then - raise ArgumentError, "#{@string} does not have an #{item.inspect} action!" + raise ArgumentError, "#{@face} does not have an #{item.inspect} action!" end @action = action end @@ -129,7 +129,7 @@ class Puppet::Application::StringBase < Puppet::Application # Note: because of our definition of where the action is set, we end up # with it *always* being the first word of the remaining set of command # line arguments. So, strip that off when we construct the arguments to - # pass down to the string action. --daniel 2011-04-04 + # pass down to the face action. --daniel 2011-04-04 @arguments.delete_at(0) # We copy all of the app options to the end of the call; This allows each @@ -142,7 +142,7 @@ class Puppet::Application::StringBase < Puppet::Application def main # Call the method associated with the provided action (e.g., 'find'). - if result = @string.send(@action.name, *arguments) + if result = @face.send(@action.name, *arguments) puts render(result) end exit(exit_code) diff --git a/lib/puppet/application/indirection_base.rb b/lib/puppet/application/indirection_base.rb index cfa1ea529..7455ebedf 100644 --- a/lib/puppet/application/indirection_base.rb +++ b/lib/puppet/application/indirection_base.rb @@ -1,4 +1,4 @@ -require 'puppet/application/string_base' +require 'puppet/application/faces_base' -class Puppet::Application::IndirectionBase < Puppet::Application::StringBase +class Puppet::Application::IndirectionBase < Puppet::Application::FacesBase end -- cgit