Skip to content

Commit 0b0ee99

Browse files
committed
Add systemd::escape function
Add a systemd escape function. ```puppet systemd::escape('/foo/bar') ``` returns `-foo-bar` while the path escape can be executed as ```puppet systemd::escape('/foo/bar',true) ``` to return 'foo-bar'
1 parent b0dee23 commit 0b0ee99

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,20 @@ loginctl_user { 'foo':
373373

374374
or as a hash via the `systemd::loginctl_users` parameter.
375375

376+
### Systemd Escape Function
377+
Escapes strings as `systemd-escape` command does.
378+
379+
```puppet
380+
$result = systemd::escape('foo::bar/')
381+
```
382+
`$result` would be `foo::bar-`
383+
384+
or path escape as if with `-p` option.
385+
386+
```puppet
387+
$result = systemd::escape('/mnt/foobar/', true)
388+
```
389+
`$result` would be `mnt-foobar`.
376390

377391
## Transfer Notice
378392

REFERENCE.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535

3636
* [`loginctl_user`](#loginctl_user): An arbitrary name used as the identity of the resource.
3737

38+
### Functions
39+
40+
* [`systemd::escape`](#systemdescape): Escape strings as systemd-escape does.
41+
3842
### Data types
3943

4044
* [`Systemd::Dropin`](#systemddropin): custom datatype that validates filenames/paths for valid systemd dropin files
@@ -1112,6 +1116,60 @@ An arbitrary name used as the identity of the resource.
11121116
The specific backend to use for this `loginctl_user` resource. You will seldom need to specify this --- Puppet will
11131117
usually discover the appropriate provider for your platform.
11141118

1119+
## Functions
1120+
1121+
### <a name="systemdescape"></a>`systemd::escape`
1122+
1123+
Type: Puppet Language
1124+
1125+
Escape strings as systemd-escape does.
1126+
1127+
#### Examples
1128+
1129+
##### Escaping a string
1130+
1131+
```puppet
1132+
$result = systemd::escape('foo::bar')
1133+
```
1134+
1135+
##### Escaping a path
1136+
1137+
```puppet
1138+
$result = systemd::escape('/mnt/foobar',true)
1139+
```
1140+
1141+
#### `systemd::escape(String[1] $input, Boolean $path = false)`
1142+
1143+
The systemd::escape function.
1144+
1145+
Returns: `String` String
1146+
1147+
##### Examples
1148+
1149+
###### Escaping a string
1150+
1151+
```puppet
1152+
$result = systemd::escape('foo::bar')
1153+
```
1154+
1155+
###### Escaping a path
1156+
1157+
```puppet
1158+
$result = systemd::escape('/mnt/foobar',true)
1159+
```
1160+
1161+
##### `input`
1162+
1163+
Data type: `String[1]`
1164+
1165+
Input string
1166+
1167+
##### `path`
1168+
1169+
Data type: `Boolean`
1170+
1171+
Use path (-p) ornon-path style escaping.
1172+
11151173
## Data types
11161174

11171175
### <a name="systemddropin"></a>`Systemd::Dropin`

functions/escape.pp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# @summary Escape strings as systemd-escape does.
2+
# @param input Input string
3+
# @param path Use path (-p) ornon-path style escaping.
4+
# @return String
5+
# @example Escaping a string
6+
# $result = systemd::escape('foo::bar')
7+
# @example Escaping a path
8+
# $result = systemd::escape('/mnt/foobar',true)
9+
function systemd::escape(String[1] $input, Boolean $path = false) >> String {
10+
# Escape method is defined
11+
# https://www.freedesktop.org/software/systemd/man/systemd.unit.html
12+
13+
# fail path if . on end.
14+
if $path and $input[-1] == '.' {
15+
fail('A path can not end in a \'.\'')
16+
}
17+
18+
# De-duplicate any `/` and prefix,suffix `/` if file
19+
if $path {
20+
$_chomped = $input.regsubst('/+$','').regsubst('^/+','').regsubst('//+','/')
21+
} else {
22+
$_chomped = $input
23+
}
24+
25+
# Docs talk of escaping `:` also but seems not to be the case in reality.
26+
#
27+
$_output = $_chomped.map |$_i, $_token | {
28+
case $_token {
29+
'.': {
30+
$_escaped = $_i ? {
31+
0 => '\x2e',
32+
default => $_token,
33+
}
34+
}
35+
'/': { $_escaped = '-' }
36+
',': { $_escaped = '\x2c' }
37+
default: { $_escaped = $_token }
38+
}
39+
$_escaped
40+
}.join
41+
42+
return $_output
43+
}

spec/functions/escape_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
describe 'systemd::escape' do
3+
context 'with path false' do
4+
it { is_expected.to run.with_params('foo', false).and_return('foo') }
5+
it { is_expected.to run.with_params('foo/bar/.', false).and_return('foo-bar-.') }
6+
it { is_expected.to run.with_params('/foo/bar/', false).and_return('-foo-bar-') }
7+
it { is_expected.to run.with_params('//foo//bar//', false).and_return('--foo--bar--') }
8+
it { is_expected.to run.with_params('//foo:bar,foo_bar.//', false).and_return('--foo:bar\x2cfoo_bar.--') }
9+
it { is_expected.to run.with_params('.foo', false).and_return('\x2efoo') }
10+
end
11+
context 'with path true' do
12+
it { is_expected.to run.with_params('foo', true).and_return('foo') }
13+
it { is_expected.to run.with_params('foo/bar/.', true).and_raise_error(%r{ path can not end}) }
14+
it { is_expected.to run.with_params('/foo/bar/', true).and_return('foo-bar') }
15+
it { is_expected.to run.with_params('//foo//bar//', true).and_return('foo-bar') }
16+
it { is_expected.to run.with_params('//foo:bar,foo_bar.//', true).and_return('foo:bar\x2cfoo_bar.') }
17+
it { is_expected.to run.with_params('.foo', true).and_return('\x2efoo') }
18+
end
19+
end

0 commit comments

Comments
 (0)