Skip to content

Commit 1d89df9

Browse files
committed
Merge pull request puppetlabs#505 from gibbsoft/dos2unix
(MODULES-2410) Add new functions dos2unix and unix2dos
2 parents e84090d + 4cbe846 commit 1d89df9

File tree

6 files changed

+153
-7
lines changed

6 files changed

+153
-7
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
##2015-08-13 - Supported Release 4.8.1
2+
###Summary
3+
4+
Adds some new functions.
5+
6+
####Features
7+
- Add new functions: `dos2unix` and `unix2dos`
8+
9+
####Bugfixes
10+
- n/a
11+
12+
####Improvements
13+
- n/a
14+
115
## 2015-08-10 - Supported Release 4.8.0
216
### Summary
317
This release adds a function for reading metadata.json from any module, and expands file\_line's abilities.

README.markdown

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ Returns the difference between two arrays. The returned array is a copy of the o
199199

200200
Returns the `dirname` of a path. For example, `dirname('/path/to/a/file.ext')` returns '/path/to/a'. *Type*: rvalue.
201201

202+
#### `dos2unix`
203+
204+
Returns the Unix version of the given string. Very useful when using a File resource with a cross-platform template. *Type*: rvalue.
205+
206+
~~~
207+
file{$config_file:
208+
ensure => file,
209+
content => dos2unix(template('my_module/settings.conf.erb')),
210+
}
211+
~~~
212+
213+
See also [unix2dos](#unix2dos).
214+
202215
#### `downcase`
203216

204217
Converts the case of a string or of all strings in an array to lowercase. *Type*: rvalue.
@@ -471,7 +484,7 @@ Returns the highest value of all arguments. Requires at least one argument. *Typ
471484

472485
#### `member`
473486

474-
This function determines if a variable is a member of an array. The variable can be either a string, array, or fixnum. For example, `member(['a','b'], 'b')` and `member(['a','b','c'], ['b','c'])` return 'true', while `member(['a','b'], 'c')` and `member(['a','b','c'], ['c','d'])` return 'false'. *Note*: This function does not support nested arrays. If the first argument contains nested arrays, it will not recurse through them.
487+
This function determines if a variable is a member of an array. The variable can be either a string, array, or fixnum. For example, `member(['a','b'], 'b')` and `member(['a','b','c'], ['b','c'])` return 'true', while `member(['a','b'], 'c')` and `member(['a','b','c'], ['c','d'])` return 'false'. *Note*: This function does not support nested arrays. If the first argument contains nested arrays, it will not recurse through them.
475488

476489
*Type*: rvalue.
477490

@@ -520,7 +533,7 @@ From a list of values, returns the first value that is not undefined or an empty
520533
#### `prefix`
521534

522535
Applies a prefix to all elements in an array, or to the keys in a hash.
523-
For example:
536+
For example:
524537
* `prefix(['a','b','c'], 'p')` returns ['pa','pb','pc']
525538
* `prefix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'pa'=>'b','pb'=>'c','pc'=>'d'}.
526539

@@ -697,6 +710,19 @@ Returns a union of two arrays, without duplicates. For example, `union(["a","b",
697710

698711
Removes duplicates from strings and arrays. For example, `unique("aabbcc")` returns 'abc', and `unique(["a","a","b","b","c","c"])` returns ["a","b","c"]. *Type*: rvalue.
699712

713+
#### `unix2dos`
714+
715+
Returns the DOS version of the given string. Very useful when using a File resource with a cross-platform template. *Type*: rvalue.
716+
717+
~~~
718+
file{$config_file:
719+
ensure => file,
720+
content => unix2dos(template('my_module/settings.conf.erb')),
721+
}
722+
~~~
723+
724+
See also [dos2unix](#dos2unix).
725+
700726
#### `upcase`
701727

702728
Converts an object, array or hash of objects that respond to upcase to uppercase. For example, `upcase('abcd')` returns 'ABCD'. *Type*: rvalue.
@@ -1002,7 +1028,7 @@ Instead, use:
10021028

10031029
#### `values`
10041030

1005-
Returns the values of a given hash. For example, given `$hash = {'a'=1, 'b'=2, 'c'=3} values($hash)` returns [1,2,3].
1031+
Returns the values of a given hash. For example, given `$hash = {'a'=1, 'b'=2, 'c'=3} values($hash)` returns [1,2,3].
10061032

10071033
*Type*: rvalue.
10081034

@@ -1048,7 +1074,3 @@ To report or research a bug with any part of this module, please go to
10481074
##Contributors
10491075

10501076
The list of contributors can be found at: https://github.com/puppetlabs/puppetlabs-stdlib/graphs/contributors
1051-
1052-
1053-
1054-
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Custom Puppet function to convert dos to unix format
2+
module Puppet::Parser::Functions
3+
newfunction(:dos2unix, :type => :rvalue, :arity => 1, :doc => <<-EOS
4+
Returns the Unix version of the given string.
5+
Takes a single string argument.
6+
EOS
7+
) do |arguments|
8+
9+
unless arguments[0].is_a?(String)
10+
raise(Puppet::ParseError, 'dos2unix(): Requires string as argument')
11+
end
12+
13+
arguments[0].gsub(/\r\n/, "\n")
14+
end
15+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Custom Puppet function to convert unix to dos format
2+
module Puppet::Parser::Functions
3+
newfunction(:unix2dos, :type => :rvalue, :arity => 1, :doc => <<-EOS
4+
Returns the DOS version of the given string.
5+
Takes a single string argument.
6+
EOS
7+
) do |arguments|
8+
9+
unless arguments[0].is_a?(String)
10+
raise(Puppet::ParseError, 'unix2dos(): Requires string as argument')
11+
end
12+
13+
arguments[0].gsub(/\r*\n/, "\r\n")
14+
end
15+
end

spec/functions/dos2unix_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'spec_helper'
2+
3+
describe 'dos2unix' do
4+
context 'Checking parameter validity' do
5+
it { is_expected.not_to eq(nil) }
6+
it do
7+
is_expected.to run.with_params.and_raise_error(ArgumentError, /Wrong number of arguments/)
8+
end
9+
it do
10+
is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, /Wrong number of arguments/)
11+
end
12+
it do
13+
is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError)
14+
end
15+
it do
16+
is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError)
17+
end
18+
it do
19+
is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
20+
end
21+
end
22+
23+
context 'Converting from dos to unix format' do
24+
sample_text = "Hello\r\nWorld\r\n"
25+
desired_output = "Hello\nWorld\n"
26+
27+
it 'should output unix format' do
28+
should run.with_params(sample_text).and_return(desired_output)
29+
end
30+
end
31+
32+
context 'Converting from unix to unix format' do
33+
sample_text = "Hello\nWorld\n"
34+
desired_output = "Hello\nWorld\n"
35+
36+
it 'should output unix format' do
37+
should run.with_params(sample_text).and_return(desired_output)
38+
end
39+
end
40+
end

spec/functions/unix2dos_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'spec_helper'
2+
3+
describe 'unix2dos' do
4+
context 'Checking parameter validity' do
5+
it { is_expected.not_to eq(nil) }
6+
it do
7+
is_expected.to run.with_params.and_raise_error(ArgumentError, /Wrong number of arguments/)
8+
end
9+
it do
10+
is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, /Wrong number of arguments/)
11+
end
12+
it do
13+
is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError)
14+
end
15+
it do
16+
is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError)
17+
end
18+
it do
19+
is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
20+
end
21+
end
22+
23+
context 'Converting from unix to dos format' do
24+
sample_text = "Hello\nWorld\n"
25+
desired_output = "Hello\r\nWorld\r\n"
26+
27+
it 'should output dos format' do
28+
should run.with_params(sample_text).and_return(desired_output)
29+
end
30+
end
31+
32+
context 'Converting from dos to dos format' do
33+
sample_text = "Hello\r\nWorld\r\n"
34+
desired_output = "Hello\r\nWorld\r\n"
35+
36+
it 'should output dos format' do
37+
should run.with_params(sample_text).and_return(desired_output)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)