Issue #7124 has been reported by Jeff McCune.
----------------------------------------
Bug #7124: Face actions should be able to easily accept non-positional
arguments as a hash
https://projects.puppetlabs.com/issues/7124
Author: Jeff McCune
Status: Unreviewed
Priority: Normal
Assignee:
Category:
Target version:
Affected Puppet version: 2.7.0rc1
Keywords: face faces hash arguments options
Branch:
# Note #
Again, quickly filing this while on site. Will update with more information
when I'm off-site.
# Overview #
Writing an action that only accepts mandatory arguments is currently awkward.
Desired Behavior:
puppet catalog compareyaml --nodelist nodes.txt --catalogs-old catalogs24/
--catalogs-new catalogs26/
However, the arguments presented to the action block are "awkward" (I will
clarify this later, off site)
In this code, options is actually an array of arguments. The actual hash for
the declared options. is the last element of the array and needs to be popped
off.
Incorrect:
<pre>
Puppet::Face.define(:catalog, '0.0.1') do
action :compareyaml do
summary "This action takes a list of node identifier names as a flat text
file, then loads a catalog in directory A and directory B exactly matching the
name. It them compares the catalogs. This is intended to compare catalogs
generated on older versions of a Puppet Master with catalogs generated on a
newer version of Puppet Master."
option "--nodelist TEXTFILE"
option "--catalogs-old DIRECTORY"
option "--catalogs-new DIRECTORY"
# JJM 2011-04-14 (I _think_ *args is the naive way of doing things)
# when_invoked do |*args|
when_invoked do |options|
options
# JJM 2011-04-14 There needs to be a better way to do this. Look into
# Puppet::Interface::ActionBuilder#option
raise ArgumentError, "Usage: puppet catalog compareyaml node.lst
dir_old/ dir_new/" unless args.length >= 3
nodelist = args[0]
# This will usually be the "old" master version, e.g 0.24.8
dir_old = args[1]
# This will usually be the "new" mater version, e.g 2.6.7
dir_new = args[2]
"Hi!"
end
end
end
</pre>
Correct:
<pre>
require 'puppet/face'
Puppet::Face.define(:catalog, '0.0.1') do
action :compareyaml do
summary "This action takes a list of node identifier names as a flat text
file, then loads a catalog in directory A and directory B exactly matching the
name. It them compares the catalogs. This is intended to compare catalogs
generated on older versions of a Puppet Master with catalogs generated on a
newer version of Puppet Master."
option "--nodelist TEXTFILE"
option "--catalogs-old DIRECTORY"
option "--catalogs-new DIRECTORY"
# JJM 2011-04-14 (I _think_ *args is the naive way of doing things)
# when_invoked do |*args|
when_invoked do |*args|
# JJM The last argument is a hash of named attributes. These are declared
# with the option private method in the action block, e.g.
# option "--nodelist TEXTFILE"
options = args.pop
# Pieter is working on required options patch, which should make this
# code obsolete. [#7013](http://projects.puppetlabs.com/issues/7013)
%w{nodelist catalogs-old catalogs-new}.each do |opt|
key = opt.gsub(/-/, '_').to_sym
raise ArgumentError, "--#{opt} is a required option" unless options[key]
end
raise NotImplementedError, "Hi! I don't do anything yet!"
end
end
end
</pre>
--
You have received this notification because you have either subscribed to it,
or are involved in it.
To change your notification preferences, please click here:
http://projects.puppetlabs.com/my/account
--
You received this message because you are subscribed to the Google Groups
"Puppet Bugs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-bugs?hl=en.