Skip to content

Commit 2a18fbb

Browse files
committed
Add config quote filter
1 parent b760baa commit 2a18fbb

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.github/warp-logo@2x.png

-78.6 KB
Binary file not shown.

src/Configuration.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ public function parse(mixed $value): mixed
115115
{
116116
if (is_string($value)) {
117117
$normalizedValue = normalize_line_endings($value);
118-
return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*\}\}/', function (array $matches) {
119-
return $this->get($matches[1]);
118+
return preg_replace_callback('/\{\{\s*([\w\.\/-]+)\s*(?:\|\s*(\w+)\s*)?\}\}/', function (array $matches) {
119+
$value = $this->get($matches[1]);
120+
if (isset($matches[2])) {
121+
$value = match ($matches[2]) {
122+
'quote' => quote((string) $value),
123+
default => throw new \InvalidArgumentException("Unknown filter: {$matches[2]}"),
124+
};
125+
}
126+
return $value;
120127
}, $normalizedValue);
121128
}
122129

src/functions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ function within(string $path, callable $callback): mixed
376376
* run("echo $path");
377377
* ```
378378
*
379+
* Use `| quote` filter to safely quote config values as shell arguments:
380+
* ```php
381+
* run('echo {{ message | quote }}');
382+
* run('grep -r {{ pattern | quote }} {{release_path}}');
383+
* ```
384+
*
379385
* @param string $command Command to run on remote host.
380386
* @param string|null $cwd Sets the process working directory. If not set {{working_path}} will be used.
381387
* @param int|null $timeout Sets the process timeout (max. runtime). The timeout in seconds (default: 300 sec; see {{default_timeout}}, `null` to disable).

tests/src/Configuration/ConfigurationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,32 @@ public function testAddToParentCallback()
159159
self::assertEquals(['a', 'b', 'c'], $alpha->get('files'));
160160
}
161161

162+
public function testParseQuoteFilter()
163+
{
164+
$config = new Configuration();
165+
$config->set('name', "it's a test");
166+
167+
self::assertEquals("\$'it\\'s a test'", $config->parse('{{ name | quote }}'));
168+
}
169+
170+
public function testParseQuoteFilterSafeValue()
171+
{
172+
$config = new Configuration();
173+
$config->set('path', '/usr/local/bin');
174+
175+
self::assertEquals('/usr/local/bin', $config->parse('{{ path | quote }}'));
176+
}
177+
178+
public function testParseUnknownFilter()
179+
{
180+
$this->expectException(\InvalidArgumentException::class);
181+
$this->expectExceptionMessage('Unknown filter: upper');
182+
183+
$config = new Configuration();
184+
$config->set('name', 'test');
185+
$config->parse('{{ name | upper }}');
186+
}
187+
162188
public function testPersist()
163189
{
164190
$parent = new Configuration();

0 commit comments

Comments
 (0)