Skip to content

Commit fd5c576

Browse files
authored
Merge pull request #718 from sspreitzer/master_glob
Add glob function
2 parents 9a6a33e + 6092ede commit fd5c576

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

README.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,17 @@ This is useful if the namespace itself is stored in a string:
665665

666666
*Type*: rvalue.
667667

668+
#### `glob`
669+
670+
Dir#glob wrapper that accepts a string or an array of strings of path patterns.
671+
Returns an array of strings of matched paths.
672+
673+
~~~
674+
$confs = glob(['/etc/**/*.conf', '/opt/**/*.conf'])
675+
~~~
676+
677+
*Type*: rvalue.
678+
668679
#### `grep`
669680

670681
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.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# glob.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:glob, :type => :rvalue, :doc => <<-'EOS'
7+
Returns an Array of file entries of a directory or an Array of directories.
8+
Uses same patterns as Dir#glob
9+
EOS
10+
) do |arguments|
11+
12+
raise(Puppet::ParseError, "glob(): Wrong number of arguments given " +
13+
"(#{arguments.size} for 1)") unless arguments.size == 1
14+
15+
pattern = arguments[0]
16+
17+
raise(Puppet::ParseError, 'glob(): Requires either array or string ' +
18+
'to work') unless pattern.is_a?(String) || pattern.is_a?(Array)
19+
20+
Dir.glob(pattern)
21+
end
22+
end

spec/functions/glob_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'glob' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
6+
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
7+
it { is_expected.to run.with_params('').and_return([]) }
8+
it { is_expected.to run.with_params(['']).and_return([]) }
9+
it { is_expected.to run.with_params(['', '']).and_return([]) }
10+
it { is_expected.to run.with_params(['/etc/xyzxyzxyz', '/etcxyzxyzxyz']).and_return([]) }
11+
end

0 commit comments

Comments
 (0)