Skip to content

Commit 6092ede

Browse files
author
Sascha Spreitzer
committed
Add glob function
1 parent db8c1fb commit 6092ede

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
@@ -635,6 +635,17 @@ This is useful if the namespace itself is stored in a string:
635635

636636
*Type*: rvalue.
637637

638+
#### `glob`
639+
640+
Dir#glob wrapper that accepts a string or an array of strings of path patterns.
641+
Returns an array of strings of matched paths.
642+
643+
~~~
644+
$confs = glob(['/etc/**/*.conf', '/opt/**/*.conf'])
645+
~~~
646+
647+
*Type*: rvalue.
648+
638649
#### `grep`
639650

640651
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)