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
16 changes: 16 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ In this code example, `match` looks for a line beginning with export
followed by HTTP_PROXY and delete it. If multiple lines match, an
error will be raised unless the `multiple => true` parameter is set.

Encoding example:

file_line { "XScreenSaver":
ensure => present,
path => '/root/XScreenSaver'
line => "*lock: 10:00:00",
match => '^*lock:',
encoding => "iso-8859-1",
}

Files with special characters that are not valid UTF-8 will give the
error message "invalid byte sequence in UTF-8". In this case, determine
the correct file encoding and specify the correct encoding using the
encoding attribute, the value of which needs to be a valid Ruby character
encoding.

**Autorequires:** If Puppet is managing the file that contains the line being managed, the `file_line` resource autorequires that file.

##### Parameters
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/file_line/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def lines
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
@lines ||= File.readlines(resource[:path])
@lines ||= File.readlines(resource[:path], :encoding => resource[:encoding])
end

def match_regex
Expand Down
21 changes: 21 additions & 0 deletions lib/puppet/type/file_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
followed by HTTP_PROXY and delete it. If multiple lines match, an
error will be raised unless the `multiple => true` parameter is set.

Encoding example:

file_line { "XScreenSaver":
ensure => present,
path => '/root/XScreenSaver'
line => "*lock: 10:00:00",
match => '^*lock:',
encoding => "iso-8859-1",
}

Files with special characters that are not valid UTF-8 will give the
error message "invalid byte sequence in UTF-8". In this case, determine
the correct file encoding and specify the correct encoding using the
encoding attribute, the value of which needs to be a valid Ruby character
encoding.

**Autorequires:** If Puppet is managing the file that will contain the line
being managed, the file_line resource will autorequire that file.
EOT
Expand Down Expand Up @@ -107,6 +123,11 @@
defaultto true
end

newparam(:encoding) do
desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1'
defaultto 'UTF-8'
end

# Autorequire the file resource if it's being managed
autorequire(:file) do
self[:path]
Expand Down
7 changes: 6 additions & 1 deletion spec/unit/puppet/type/file_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@
it 'should default to replace => true' do
expect(file_line[:replace]).to eq :true
end

it 'should default to encoding => UTF-8' do
expect(file_line[:encoding]).to eq 'UTF-8'
end
it 'should accept encoding => iso-8859-1' do
expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :present, :encoding => 'iso-8859-1', :line => 'bar') }.not_to raise_error
end
it "should autorequire the file it manages" do
catalog = Puppet::Resource::Catalog.new
file = Puppet::Type.type(:file).new(:name => tmp_path)
Expand Down