Skip to content

Commit 9a845d1

Browse files
jkroepkeekohl
andauthored
Add function systemd::systemd_escape (#243)
* Add function systemd::systemd_escape * Add function systemd::systemd_escape * disable combine * trim value * fix tests * Mock systemd * Update README.md Co-authored-by: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl> * Optimize unit tests * Remove lint issues Co-authored-by: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
1 parent a56a396 commit 9a845d1

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ loginctl_user { 'foo':
423423
or as a hash via the `systemd::loginctl_users` parameter.
424424

425425
### Systemd Escape Function
426-
Escapes strings as `systemd-escape` command does.
426+
Partially escape strings as `systemd-escape` command does.
427+
428+
This functions only escapes a subset of chars. Non-ASCII character will not escape.
427429

428430
```puppet
429431
$result = systemd::escape('foo::bar/')
@@ -437,6 +439,23 @@ $result = systemd::escape('/mnt/foobar/', true)
437439
```
438440
`$result` would be `mnt-foobar`.
439441

442+
### Systemd Escape Function (uses systemd-escape)
443+
Escape strings by call the `systemd-escape` command in the background.
444+
445+
It's highly recommend running the function as [deferred function](https://puppet.com/docs/puppet/6/deferring_functions.html) since it executes the command on the agent.
446+
447+
```puppet
448+
$result = Deferred('systemd::systemd_escape', ["foo::bar"])
449+
```
450+
`$result` would be `foo::bar-`
451+
452+
or path escape as if with `-p` option.
453+
454+
```puppet
455+
$result = Deferred('systemd::systemd_escape', ["/mnt/foo-bar/", true])
456+
```
457+
`$result` would be `mnt-foo\x2dbar`.
458+
440459
## Transfer Notice
441460

442461
This plugin was originally authored by [Camptocamp](http://www.camptocamp.com).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# @summary Escape strings by call the `systemd-escape` command in the background.
4+
Puppet::Functions.create_function(:'systemd::systemd_escape') do
5+
# @param input Input string
6+
# @param path Use path (-p) ornon-path style escaping.
7+
dispatch :escape do
8+
param 'String', :input
9+
optional_param 'Optional[Boolean]', :path
10+
return_type 'String'
11+
end
12+
13+
def escape(input, path = false)
14+
args = []
15+
16+
args.push('--path') if path
17+
18+
args.push(input)
19+
exec_systemd(args)
20+
end
21+
22+
def exec_systemd(*args)
23+
exec_args = { failonfail: true, combine: false }
24+
escaped = Puppet::Util::Execution.execute(['systemd-escape', args], **exec_args)
25+
escaped.strip
26+
end
27+
end

spec/functions/systemd_escape_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
describe 'systemd::systemd_escape' do
5+
context 'with path false' do
6+
it do
7+
allow(Puppet::Util::Execution).to receive(:execute).with(['systemd-escape', [['abcöäüß']]], { combine: false, failonfail: true }).and_return("abc\\xc3\\xb6\\xc3\\xa4\\xc3\\xbc\\xc3\\x9f\n")
8+
9+
is_expected.to run.with_params('abcöäüß', false).and_return('abc\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f')
10+
end
11+
end
12+
13+
context 'with path true' do
14+
it do
15+
allow(Puppet::Util::Execution).to receive(:execute).with(['systemd-escape', [%w[--path abcöäüß]]], { combine: false, failonfail: true }).and_return("abc\\xc3\\xb6\\xc3\\xa4\\xc3\\xbc\\xc3\\x9f\n")
16+
17+
is_expected.to run.with_params('abcöäüß', true).and_return('abc\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f')
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)