diff options
author | Sascha Spreitzer <sspreitz@redhat.com> | 2017-02-04 13:30:59 +0100 |
---|---|---|
committer | Sascha Spreitzer <sspreitz@redhat.com> | 2017-02-05 12:21:38 +0100 |
commit | 6092ede8d86746038df4a367191d232003652c55 (patch) | |
tree | ceec2b7519bb26fe02e323ce885ac26d82fbf321 | |
parent | db8c1fbb2394d93fe3156b17c840455f1b3e2c76 (diff) |
Add glob function
-rw-r--r-- | README.markdown | 11 | ||||
-rw-r--r-- | lib/puppet/parser/functions/glob.rb | 22 | ||||
-rwxr-xr-x | spec/functions/glob_spec.rb | 11 |
3 files changed, 44 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index a4c30b5..457ab6a 100644 --- a/README.markdown +++ b/README.markdown @@ -635,6 +635,17 @@ This is useful if the namespace itself is stored in a string: *Type*: rvalue. +#### `glob` + +Dir#glob wrapper that accepts a string or an array of strings of path patterns. +Returns an array of strings of matched paths. + + ~~~ + $confs = glob(['/etc/**/*.conf', '/opt/**/*.conf']) + ~~~ + +*Type*: rvalue. + #### `grep` Searches through an array and returns any elements that match the provided regular expression. For example, `grep(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['aaa','aaaddd']. *Type*: rvalue. diff --git a/lib/puppet/parser/functions/glob.rb b/lib/puppet/parser/functions/glob.rb new file mode 100644 index 0000000..54cdda6 --- /dev/null +++ b/lib/puppet/parser/functions/glob.rb @@ -0,0 +1,22 @@ +# +# glob.rb +# + +module Puppet::Parser::Functions + newfunction(:glob, :type => :rvalue, :doc => <<-'EOS' + Returns an Array of file entries of a directory or an Array of directories. + Uses same patterns as Dir#glob + EOS + ) do |arguments| + + raise(Puppet::ParseError, "glob(): Wrong number of arguments given " + + "(#{arguments.size} for 1)") unless arguments.size == 1 + + pattern = arguments[0] + + raise(Puppet::ParseError, 'glob(): Requires either array or string ' + + 'to work') unless pattern.is_a?(String) || pattern.is_a?(Array) + + Dir.glob(pattern) + end +end diff --git a/spec/functions/glob_spec.rb b/spec/functions/glob_spec.rb new file mode 100755 index 0000000..06439da --- /dev/null +++ b/spec/functions/glob_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'glob' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('').and_return([]) } + it { is_expected.to run.with_params(['']).and_return([]) } + it { is_expected.to run.with_params(['', '']).and_return([]) } + it { is_expected.to run.with_params(['/etc/xyzxyzxyz', '/etcxyzxyzxyz']).and_return([]) } +end |