diff options
| author | Daniel Pittman <daniel@rimspace.net> | 2011-03-15 11:45:02 -0700 |
|---|---|---|
| committer | Matt Robinson <matt@puppetlabs.com> | 2011-03-15 11:55:59 -0700 |
| commit | c86a980fe9b2f2e109fe7956a1be2705f3fc2ade (patch) | |
| tree | c62ae3e63fac89286140e6879538ba14009663ba /lib/puppet/parameter | |
| parent | 77fbf7fdd3d17c169057a17e8d5829907975d169 (diff) | |
| download | puppet-c86a980fe9b2f2e109fe7956a1be2705f3fc2ade.tar.gz puppet-c86a980fe9b2f2e109fe7956a1be2705f3fc2ade.tar.xz puppet-c86a980fe9b2f2e109fe7956a1be2705f3fc2ade.zip | |
(#4884) Add consistent path validation and behavior
Many path parameters were implementing their own inconsistent validation
and behavior. Now those parameters can have a parent class that makes
things a lot more consistent.
Reviewed-by: Matt Robinson and Max Martin
Diffstat (limited to 'lib/puppet/parameter')
| -rw-r--r-- | lib/puppet/parameter/path.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/puppet/parameter/path.rb b/lib/puppet/parameter/path.rb new file mode 100644 index 000000000..44886afd0 --- /dev/null +++ b/lib/puppet/parameter/path.rb @@ -0,0 +1,42 @@ +require 'puppet/parameter' + +class Puppet::Parameter::Path < Puppet::Parameter + def self.accept_arrays(bool = true) + @accept_arrays = !!bool + end + def self.arrays? + @accept_arrays + end + + def validate_path(paths) + if paths.is_a?(Array) and ! self.class.arrays? then + fail "#{name} only accepts a single path, not an array of paths" + end + + # We *always* support Unix path separators, as Win32 does now too. + absolute = "[/#{::Regexp.quote(::File::SEPARATOR)}]" + win32 = Puppet.features.microsoft_windows? + + Array(paths).each do |path| + next if path =~ %r{^#{absolute}} + next if win32 and path =~ %r{^(?:[a-zA-Z]:)?#{absolute}} + fail("#{name} must be a fully qualified path") + end + + paths + end + + # This will be overridden if someone uses the validate option, which is why + # it just delegates to the other, useful, method. + def unsafe_validate(paths) + validate_path(paths) + end + + # Likewise, this might be overridden, but by default... + def unsafe_munge(paths) + if paths.is_a?(Array) and ! self.class.arrays? then + fail "#{name} only accepts a single path, not an array of paths" + end + paths + end +end |
