Skip to content

Commit 6f2c7d3

Browse files
Add parameter to not create empty files when no fragments are defined
By default concat would always create an empty file if no fragment was defined. Sometimes we don't want a file unless it has content.
1 parent 2df5a25 commit 6f2c7d3

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

REFERENCE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The following parameters are available in the `concat` defined type:
5656
* [`show_diff`](#-concat--show_diff)
5757
* [`validate_cmd`](#-concat--validate_cmd)
5858
* [`warn`](#-concat--warn)
59+
* [`create_empty_file`](#-concat--create_empty_file)
5960

6061
##### <a name="-concat--backup"></a>`backup`
6162

@@ -222,6 +223,14 @@ it explicitly if you need it.
222223

223224
Default value: `false`
224225

226+
##### <a name="-concat--create_empty_file"></a>`create_empty_file`
227+
228+
Data type: `Boolean`
229+
230+
Specifies whether to create an empty file if no fragments are defined. Defaults to true.
231+
232+
Default value: `true`
233+
225234
### <a name="concat--fragment"></a>`concat::fragment`
226235

227236
Manages a fragment of text to be compiled into a file.
@@ -310,6 +319,7 @@ Default value: `present`
310319
The following parameters are available in the `concat_file` type.
311320

312321
* [`backup`](#-concat_file--backup)
322+
* [`create_empty_file`](#-concat_file--create_empty_file)
313323
* [`ensure_newline`](#-concat_file--ensure_newline)
314324
* [`force`](#-concat_file--force)
315325
* [`format`](#-concat_file--format)
@@ -336,6 +346,14 @@ resource for execution. Valid options: true, false, or a string representing eit
336346
extension
337347
beginning with ".".'
338348

349+
##### <a name="-concat_file--create_empty_file"></a>`create_empty_file`
350+
351+
Valid values: `true`, `false`, `yes`, `no`
352+
353+
Specifies whether to create an empty file if no fragments are defined.
354+
355+
Default value: `true`
356+
339357
##### <a name="-concat_file--ensure_newline"></a>`ensure_newline`
340358

341359
Valid values: `true`, `false`, `yes`, `no`

lib/puppet/type/concat_file.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ def exists?
181181
DOC
182182
end
183183

184+
newparam(:create_empty_file, boolean: true, parent: Puppet::Parameter::Boolean) do
185+
desc 'Specifies whether to create an empty file if no fragments are defined.'
186+
defaultto true
187+
end
188+
184189
# Autorequire the file we are generating below
185190
# Why is this necessary ?
186191
autorequire(:file) do
@@ -359,6 +364,10 @@ def eval_generate
359364
catalog.resource("File[#{self[:path]}]")[:content] = content
360365
end
361366

367+
if !self[:create_empty_file] && (content.nil? || content.empty?)
368+
catalog.resource("File[#{self[:path]}]")[:ensure] = :absent
369+
end
370+
362371
[catalog.resource("File[#{self[:path]}]")]
363372
end
364373
end

manifests/init.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
# Before 2.0.0, this parameter would add a newline at the end of the warn message. To improve flexibilty, this was removed. Please add
8080
# it explicitly if you need it.
8181
#
82+
# @param create_empty_file
83+
# Specifies whether to create an empty file if no fragments are defined. Defaults to true.
84+
#
8285
define concat (
8386
Enum['present', 'absent'] $ensure = 'present',
8487
Stdlib::Absolutepath $path = $name,
@@ -99,6 +102,7 @@
99102
Optional[String] $seluser = undef,
100103
Optional[String] $format = 'plain',
101104
Optional[Boolean] $force = false,
105+
Boolean $create_empty_file = true,
102106
) {
103107
$safe_name = regsubst($name, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'G')
104108
$default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n"
@@ -138,6 +142,7 @@
138142
validate_cmd => $validate_cmd,
139143
format => $format,
140144
force => $force,
145+
create_empty_file => $create_empty_file,
141146
}
142147

143148
if $_append_header {

spec/acceptance/concat_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@
100100
end
101101
end
102102

103+
describe 'with no fragments declared and create_empty_file = false' do
104+
let(:pp) do
105+
<<-MANIFEST
106+
concat { 'file':
107+
ensure => present,
108+
path => '#{basedir}/file',
109+
mode => '0644',
110+
create_empty_file => false,
111+
}
112+
MANIFEST
113+
end
114+
115+
it 'applies the manifest twice with no stderr' do
116+
idempotent_apply(pp)
117+
expect(file("#{basedir}/file")).not_to be_file
118+
end
119+
end
120+
103121
describe 'when absent with path set' do
104122
let(:pp) do
105123
<<-MANIFEST

spec/unit/type/concat_file_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,8 @@
149149
expect { resource[:format] = 'bar' }.to raise_error(%r{Invalid value "bar"})
150150
end
151151
end
152+
153+
describe 'parameter :create_empty_file' do
154+
it_behaves_like 'Puppet::Parameter::Boolean', :create_empty_file
155+
end
152156
end

0 commit comments

Comments
 (0)