class Puppet::Application::FaceBase

Attributes

action[RW]
arguments[RW]
face[RW]
render_as[R]
type[RW]

Public Instance Methods

find_application_argument(item) click to toggle source
    # File lib/puppet/application/face_base.rb
169 def find_application_argument(item)
170   self.class.option_parser_commands.each do |options, function|
171     options.each do |option|
172       next unless option =~ /^-/
173       pattern = /^#{option.sub('[no-]', '').sub(/[ =].*$/, '')}(?:[ =].*)?$/
174       next unless pattern.match(item)
175       return {
176         :argument => option =~ /[ =]/,
177         :optional => option =~ /[ =]\[/
178       }
179     end
180   end
181   return nil                  # not found
182 end
find_global_settings_argument(item) click to toggle source
    # File lib/puppet/application/face_base.rb
157 def find_global_settings_argument(item)
158   Puppet.settings.each do |name, object|
159     object.optparse_args.each do |arg|
160       next unless arg =~ /^-/
161       # sadly, we have to emulate some of optparse here...
162       pattern = /^#{arg.sub('[no-]', '').sub(/[ =].*$/, '')}(?:[ =].*)?$/
163       pattern.match item and return object
164     end
165   end
166   return nil                  # nothing found.
167 end
main() click to toggle source
    # File lib/puppet/application/face_base.rb
209 def main
210   status = false
211 
212   # Call the method associated with the provided action (e.g., 'find').
213   unless @action
214     puts Puppet::Face[:help, :current].help(@face.name)
215     raise _("%{face} does not respond to action %{arg}") % { face: face, arg: arguments.first }
216   end
217 
218   # We need to do arity checking here because this is generic code
219   # calling generic methods – that have argument defaulting.  We need to
220   # make sure we don't accidentally pass the options as the first
221   # argument to a method that takes one argument.  eg:
222   #
223   #   puppet facts find
224   #   => options => {}
225   #      @arguments => [{}]
226   #   => @face.send :bar, {}
227   #
228   #   def face.bar(argument, options = {})
229   #   => bar({}, {})  # oops!  we thought the options were the
230   #                   # positional argument!!
231   #
232   # We could also fix this by making it mandatory to pass the options on
233   # every call, but that would make the Ruby API much more annoying to
234   # work with; having the defaulting is a much nicer convention to have.
235   #
236   # We could also pass the arguments implicitly, by having a magic
237   # 'options' method that was visible in the scope of the action, which
238   # returned the right stuff.
239   #
240   # That sounds attractive, but adds complications to all sorts of
241   # things, especially when you think about how to pass options when you
242   # are writing Ruby code that calls multiple faces.  Especially if
243   # faces are involved in that. ;)
244   #
245   # --daniel 2011-04-27
246   if (arity = @action.positional_arg_count) > 0
247     unless (count = arguments.length) == arity then
248       raise ArgumentError, n_("puppet %{face} %{action} takes %{arg_count} argument, but you gave %{given_count}", "puppet %{face} %{action} takes %{arg_count} arguments, but you gave %{given_count}", arity - 1) % { face: @face.name, action: @action.name, arg_count: arity-1, given_count: count-1 }
249     end
250   end
251 
252   if @face.deprecated?
253     Puppet.deprecation_warning(_("'puppet %{face}' is deprecated and will be removed in a future release") % { face: @face.name })
254   end
255 
256   result = @face.send(@action.name, *arguments)
257   puts render(result, arguments) unless result.nil?
258   status = true
259 
260 # We need an easy way for the action to set a specific exit code, so we
261 # rescue SystemExit here; This allows each action to set the desired exit
262 # code by simply calling Kernel::exit.  eg:
263 #
264 #   exit(2)
265 #
266 # --kelsey 2012-02-14
267 rescue SystemExit => detail
268   status = detail.status
269 
270 rescue => detail
271   Puppet.log_exception(detail)
272   Puppet.err _("Try 'puppet help %{face} %{action}' for usage") % { face: @face.name, action: @action.name }
273 
274 ensure
275   exit status
276 end
parse_options() click to toggle source
Calls superclass method Puppet::Application#parse_options
    # File lib/puppet/application/face_base.rb
 63 def parse_options
 64   # We need to parse enough of the command line out early, to identify what
 65   # the action is, so that we can obtain the full set of options to parse.
 66 
 67   # REVISIT: These should be configurable versions, through a global
 68   # '--version' option, but we don't implement that yet... --daniel 2011-03-29
 69   @type = Puppet::Util::ConstantInflector.constant2file(self.class.name.to_s.sub(/.+:/, '')).to_sym
 70   @face = Puppet::Face[@type, :current]
 71 
 72   # Now, walk the command line and identify the action.  We skip over
 73   # arguments based on introspecting the action and all, and find the first
 74   # non-option word to use as the action.
 75   action_name = nil
 76   index       = -1
 77   until action_name or (index += 1) >= command_line.args.length do
 78     item = command_line.args[index]
 79     if item =~ /^-/
 80       option = @face.options.find do |name|
 81         item =~ /^-+#{name.to_s.gsub(/[-_]/, '[-_]')}(?:[ =].*)?$/
 82       end
 83       if option
 84         option = @face.get_option(option)
 85         # If we have an inline argument, just carry on.  We don't need to
 86         # care about optional vs mandatory in that case because we do a real
 87         # parse later, and that will totally take care of raising the error
 88         # when we get there. --daniel 2011-04-04
 89         if option.takes_argument? and !item.index('=')
 90           index += 1 unless
 91             (option.optional_argument? and command_line.args[index + 1] =~ /^-/)
 92         end
 93       else
 94         option = find_global_settings_argument(item)
 95         if option
 96           unless Puppet.settings.boolean? option.name
 97             # As far as I can tell, we treat non-bool options as always having
 98             # a mandatory argument. --daniel 2011-04-05
 99             # ... But, the mandatory argument will not be the next item if an = is
100             # employed in the long form of the option. --jeffmccune 2012-09-18
101             index += 1 unless item =~ /^--#{option.name}=/
102           end
103         else 
104           option = find_application_argument(item)
105           if option
106             index += 1 if (option[:argument] and not option[:optional])
107           else
108             raise OptionParser::InvalidOption.new(item.sub(/=.*$/, ''))
109           end
110         end
111       end
112     else
113       # Stash away the requested action name for later, and try to fetch the
114       # action object it represents; if this is an invalid action name that
115       # will be nil, and handled later.
116       action_name = item.to_sym
117       @action = Puppet::Face.find_action(@face.name, action_name)
118       @face   = @action.face if @action
119     end
120   end
121 
122   if @action.nil?
123     @action = @face.get_default_action()
124     if @action
125       @is_default_action = true
126     else
127       # First try to handle global command line options
128       # But ignoring invalid options as this is a invalid action, and
129       # we want the error message for that instead.
130       begin
131         super
132       rescue OptionParser::InvalidOption
133       end
134 
135       face   = @face.name
136       action = action_name.nil? ? 'default' : "'#{action_name}'"
137       msg = _("'%{face}' has no %{action} action.  See `puppet help %{face}`.") % { face: face, action: action }
138 
139       Puppet.err(msg)
140       Puppet::Util::Log.force_flushqueue()
141 
142       exit false
143     end
144   end
145 
146   # Now we can interact with the default option code to build behaviour
147   # around the full set of options we now know we support.
148   @action.options.each do |o|
149     o = @action.get_option(o) # make it the object.
150     self.class.option(*o.optparse) # ...and make the CLI parse it.
151   end
152 
153   # ...and invoke our parent to parse all the command line options.
154   super
155 end
preinit() click to toggle source
Calls superclass method Puppet::Application#preinit
   # File lib/puppet/application/face_base.rb
55 def preinit
56   super
57   Signal.trap(:INT) do
58     $stderr.puts _("Cancelling Face")
59     exit(0)
60   end
61 end
render(result, args_and_options) click to toggle source
   # File lib/puppet/application/face_base.rb
39 def render(result, args_and_options)
40   hook = action.when_rendering(render_as.name)
41 
42   if hook
43     # when defining when_rendering on your action you can optionally
44     # include arguments and options
45     if hook.arity > 1
46       result = hook.call(result, *args_and_options)
47     else
48       result = hook.call(result)
49     end
50   end
51 
52   render_as.render(result)
53 end
render_as=(format) click to toggle source
   # File lib/puppet/application/face_base.rb
34 def render_as=(format)
35   @render_as = Puppet::Network::FormatHandler.format(format)
36   @render_as or raise ArgumentError, _("I don't know how to render '%{format}'") % { format: format }
37 end
setup() click to toggle source
    # File lib/puppet/application/face_base.rb
184 def setup
185   Puppet::Util::Log.newdestination :console
186 
187   @arguments = command_line.args
188 
189   # Note: because of our definition of where the action is set, we end up
190   # with it *always* being the first word of the remaining set of command
191   # line arguments.  So, strip that off when we construct the arguments to
192   # pass down to the face action. --daniel 2011-04-04
193   # Of course, now that we have default actions, we should leave the
194   # "action" name on if we didn't actually consume it when we found our
195   # action.
196   @arguments.delete_at(0) unless @is_default_action
197 
198   # We copy all of the app options to the end of the call; This allows each
199   # action to read in the options.  This replaces the older model where we
200   # would invoke the action with options set as global state in the
201   # interface object.  --daniel 2011-03-28
202   @arguments << options
203 
204   # If we don't have a rendering format, set one early.
205   self.render_as ||= (@action.render_as || :console)
206 end