Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions lib/puppet/parser/functions/glob.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions spec/functions/glob_spec.rb
Original file line number Diff line number Diff line change
@@ -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