Added shortname support to config.rb and refactored addargs
This commit is contained in:
Родитель
ab42534ae2
Коммит
2ff15c015b
|
@ -1,4 +1,6 @@
|
||||||
0.23.2 (misspiggy)
|
0.23.2 (misspiggy)
|
||||||
|
Added shortname support to config.rb and refactored addargs
|
||||||
|
|
||||||
Fixed the problem in cron jobs where environment settings
|
Fixed the problem in cron jobs where environment settings
|
||||||
tended to multiple. (#749)
|
tended to multiple. (#749)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
require 'puppet'
|
require 'puppet'
|
||||||
require 'sync'
|
require 'sync'
|
||||||
require 'puppet/transportable'
|
require 'puppet/transportable'
|
||||||
|
require 'getoptlong'
|
||||||
|
|
||||||
|
|
||||||
# The class for handling configuration files.
|
# The class for handling configuration files.
|
||||||
class Puppet::Util::Config
|
class Puppet::Util::Config
|
||||||
|
@ -67,19 +69,12 @@ class Puppet::Util::Config
|
||||||
# Generate the list of valid arguments, in a format that GetoptLong can
|
# Generate the list of valid arguments, in a format that GetoptLong can
|
||||||
# understand, and add them to the passed option list.
|
# understand, and add them to the passed option list.
|
||||||
def addargs(options)
|
def addargs(options)
|
||||||
require 'getoptlong'
|
|
||||||
|
|
||||||
# Hackish, but acceptable. Copy the current ARGV for restarting.
|
# Hackish, but acceptable. Copy the current ARGV for restarting.
|
||||||
Puppet.args = ARGV.dup
|
Puppet.args = ARGV.dup
|
||||||
|
|
||||||
# Add all of the config parameters as valid options.
|
# Add all of the config parameters as valid options.
|
||||||
self.each { |param, obj|
|
self.each { |name, element|
|
||||||
if self.boolean?(param)
|
element.getopt_args.each { |args| options << args }
|
||||||
options << ["--#{param}", GetoptLong::NO_ARGUMENT]
|
|
||||||
options << ["--no-#{param}", GetoptLong::NO_ARGUMENT]
|
|
||||||
else
|
|
||||||
options << ["--#{param}", GetoptLong::REQUIRED_ARGUMENT]
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return options
|
return options
|
||||||
|
@ -195,10 +190,17 @@ class Puppet::Util::Config
|
||||||
@config.include?(name)
|
@config.include?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# check to see if a short name is already defined
|
||||||
|
def shortinclude?(short)
|
||||||
|
short = short.intern if name.is_a? String
|
||||||
|
@shortnames.include?(short)
|
||||||
|
end
|
||||||
|
|
||||||
# Create a new config object
|
# Create a new config object
|
||||||
def initialize
|
def initialize
|
||||||
@order = []
|
@order = []
|
||||||
@config = {}
|
@config = {}
|
||||||
|
@shortnames = {}
|
||||||
|
|
||||||
@created = []
|
@created = []
|
||||||
@returned = {}
|
@returned = {}
|
||||||
|
@ -499,7 +501,14 @@ class Puppet::Util::Config
|
||||||
if @config.include?(name)
|
if @config.include?(name)
|
||||||
raise Puppet::Error, "Parameter %s is already defined" % name
|
raise Puppet::Error, "Parameter %s is already defined" % name
|
||||||
end
|
end
|
||||||
@config[name] = newelement(hash)
|
tryconfig = newelement(hash)
|
||||||
|
if short = tryconfig.short
|
||||||
|
if other = @shortnames[short]
|
||||||
|
raise ArgumentError, "Parameter %s is already using short name '%s'" % [other.name, short]
|
||||||
|
end
|
||||||
|
@shortnames[short] = tryconfig
|
||||||
|
end
|
||||||
|
@config[name] = tryconfig
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -858,7 +867,7 @@ Generated on #{Time.now}.
|
||||||
# The base element type.
|
# The base element type.
|
||||||
class CElement
|
class CElement
|
||||||
attr_accessor :name, :section, :default, :parent, :setbycli
|
attr_accessor :name, :section, :default, :parent, :setbycli
|
||||||
attr_reader :desc
|
attr_reader :desc, :short
|
||||||
|
|
||||||
# Unset any set value.
|
# Unset any set value.
|
||||||
def clear
|
def clear
|
||||||
|
@ -885,6 +894,15 @@ Generated on #{Time.now}.
|
||||||
@desc = value.gsub(/^\s*/, '')
|
@desc = value.gsub(/^\s*/, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# get the arguments in getopt format
|
||||||
|
def getopt_args
|
||||||
|
if short
|
||||||
|
[["--#{name}", "-#{short}", GetoptLong::REQUIRED_ARGUMENT]]
|
||||||
|
else
|
||||||
|
[["--#{name}", GetoptLong::REQUIRED_ARGUMENT]]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def hook=(block)
|
def hook=(block)
|
||||||
meta_def :handle, &block
|
meta_def :handle, &block
|
||||||
end
|
end
|
||||||
|
@ -929,6 +947,14 @@ Generated on #{Time.now}.
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# short name for the celement
|
||||||
|
def short=(value)
|
||||||
|
if value.to_s.length != 1
|
||||||
|
raise ArgumentError, "Short names can only be one character."
|
||||||
|
end
|
||||||
|
@short = value.to_s
|
||||||
|
end
|
||||||
|
|
||||||
# Convert the object to a config statement.
|
# Convert the object to a config statement.
|
||||||
def to_config
|
def to_config
|
||||||
str = @desc.gsub(/^/, "# ") + "\n"
|
str = @desc.gsub(/^/, "# ") + "\n"
|
||||||
|
@ -1101,6 +1127,17 @@ Generated on #{Time.now}.
|
||||||
|
|
||||||
# A simple boolean.
|
# A simple boolean.
|
||||||
class CBoolean < CElement
|
class CBoolean < CElement
|
||||||
|
# get the arguments in getopt format
|
||||||
|
def getopt_args
|
||||||
|
if short
|
||||||
|
[["--#{name}", "-#{short}", GetoptLong::NO_ARGUMENT],
|
||||||
|
["--no-#{name}", GetoptLong::NO_ARGUMENT]]
|
||||||
|
else
|
||||||
|
[["--#{name}", GetoptLong::NO_ARGUMENT],
|
||||||
|
["--no-#{name}", GetoptLong::NO_ARGUMENT]]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def munge(value)
|
def munge(value)
|
||||||
case value
|
case value
|
||||||
when true, "true": return true
|
when true, "true": return true
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
require 'facter'
|
require 'facter'
|
||||||
require 'puppet/util/warnings'
|
require 'puppet/util/warnings'
|
||||||
|
require 'forwardable'
|
||||||
|
|
||||||
module Puppet::Util::SUIDManager
|
module Puppet::Util::SUIDManager
|
||||||
include Puppet::Util::Warnings
|
include Puppet::Util::Warnings
|
||||||
|
|
|
@ -10,6 +10,8 @@ require 'puppettest/parsertesting'
|
||||||
class TestConfig < Test::Unit::TestCase
|
class TestConfig < Test::Unit::TestCase
|
||||||
include PuppetTest
|
include PuppetTest
|
||||||
include PuppetTest::ParserTesting
|
include PuppetTest::ParserTesting
|
||||||
|
CElement = Puppet::Util::Config::CElement
|
||||||
|
CBoolean = Puppet::Util::Config::CBoolean
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
|
@ -535,34 +537,46 @@ yay = /a/path
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_argadding
|
def test_addargs
|
||||||
c = mkconfig
|
@config.setdefaults("testing",
|
||||||
|
:onboolean => [true, "An on bool"],
|
||||||
|
:offboolean => [false, "An off bool"],
|
||||||
|
:string => ["a string", "A string arg"],
|
||||||
|
:file => ["/path/to/file", "A file arg"]
|
||||||
|
)
|
||||||
|
|
||||||
assert_nothing_raised {
|
should = []
|
||||||
@config.setdefaults("testing",
|
@config.each { |name, element|
|
||||||
:onboolean => [true, "An on bool"],
|
element.expects(:getopt_args).returns([name])
|
||||||
:offboolean => [false, "An off bool"],
|
should << name
|
||||||
:string => ["a string", "A string arg"],
|
|
||||||
:file => ["/path/to/file", "A file arg"]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
options = []
|
result = []
|
||||||
|
assert_nothing_raised("Add args failed") do
|
||||||
|
@config.addargs(result)
|
||||||
|
end
|
||||||
|
assert_equal(should, result, "Did not call addargs correctly.")
|
||||||
|
|
||||||
@config.addargs(options)
|
end
|
||||||
|
|
||||||
@config.each { |param, obj|
|
def test_addargs_functional
|
||||||
opt = "--%s" % param
|
@config.setdefaults("testing",
|
||||||
assert(options.find { |ary|
|
:onboolean => [true, "An on bool"],
|
||||||
ary[0] == opt
|
:string => ["a string", "A string arg"]
|
||||||
}, "Argument %s was not added" % opt)
|
)
|
||||||
|
result = []
|
||||||
if @config.boolean?(param)
|
should = []
|
||||||
o = "--no-%s" % param
|
assert_nothing_raised("Add args failed") do
|
||||||
assert(options.find { |ary|
|
@config.addargs(result)
|
||||||
ary[0] == o
|
end
|
||||||
}, "Boolean off %s was not added" % o)
|
@config.each do |name, element|
|
||||||
|
if name == :onboolean
|
||||||
|
should << ["--onboolean", GetoptLong::NO_ARGUMENT]
|
||||||
|
should << ["--no-onboolean", GetoptLong::NO_ARGUMENT]
|
||||||
|
elsif name == :string
|
||||||
|
should << ["--string", GetoptLong::REQUIRED_ARGUMENT]
|
||||||
end
|
end
|
||||||
}
|
end
|
||||||
|
assert_equal(should, result, "Add args functional test failed")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_usesection
|
def test_usesection
|
||||||
|
@ -1243,6 +1257,55 @@ inttest = 27
|
||||||
# And make sure other params are unchanged
|
# And make sure other params are unchanged
|
||||||
assert_equal("unval", @config[:unchanged], "Unchanged value has somehow changed")
|
assert_equal("unval", @config[:unchanged], "Unchanged value has somehow changed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Test to make sure that we can set and get a short name
|
||||||
|
def test_celement_short_name
|
||||||
|
element = nil
|
||||||
|
assert_nothing_raised("Could not create celement") do
|
||||||
|
element = CElement.new :short => "n", :desc => "anything"
|
||||||
|
end
|
||||||
|
assert_equal("n", element.short, "Short value is not retained")
|
||||||
|
|
||||||
|
assert_raise(ArgumentError,"Allowed multicharactered short names.") do
|
||||||
|
element = CElement.new :short => "no", :desc => "anything"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Test to make sure that no two celements have the same short name
|
||||||
|
def test_celement_short_name_not_duplicated
|
||||||
|
config = mkconfig
|
||||||
|
assert_nothing_raised("Could not create celement with short name.") do
|
||||||
|
config.setdefaults(:main,
|
||||||
|
:one => { :default => "blah", :desc => "anything", :short => "o" })
|
||||||
|
end
|
||||||
|
assert_nothing_raised("Could not create second celement with short name.") do
|
||||||
|
config.setdefaults(:main,
|
||||||
|
:two => { :default => "blah", :desc => "anything", :short => "i" })
|
||||||
|
end
|
||||||
|
assert_raise(ArgumentError, "Could create second celement with duplicate short name.") do
|
||||||
|
config.setdefaults(:main,
|
||||||
|
:three => { :default => "blah", :desc => "anything", :short => "i" })
|
||||||
|
end
|
||||||
|
# make sure that when the above raises an expection that the config is not included
|
||||||
|
assert(!config.include?(:three), "Invalid configuration item was retained")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tell getopt which arguments are valid
|
||||||
|
def test_get_getopt_args
|
||||||
|
element = CElement.new :name => "foo", :desc => "anything"
|
||||||
|
assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
|
||||||
|
|
||||||
|
element.short = "n"
|
||||||
|
assert_equal([["--foo", "-n", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args")
|
||||||
|
|
||||||
|
element = CBoolean.new :name => "foo", :desc => "anything"
|
||||||
|
assert_equal([["--foo", GetoptLong::NO_ARGUMENT], ["--no-foo", GetoptLong::NO_ARGUMENT]],
|
||||||
|
element.getopt_args, "Did not produce appropriate getopt args")
|
||||||
|
|
||||||
|
element.short = "n"
|
||||||
|
assert_equal([["--foo", "-n", GetoptLong::NO_ARGUMENT],["--no-foo", GetoptLong::NO_ARGUMENT]],
|
||||||
|
element.getopt_args, "Did not produce appropriate getopt args")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# $Id$
|
# $Id$
|
||||||
|
|
Загрузка…
Ссылка в новой задаче