1 | <?php declare(strict_types=1); |
||
8 | class BashScriptParser implements ScriptParser |
||
9 | { |
||
10 | const TYPE_DIRECT_EXECUTE = '<PSH_EXECUTE_THROUGH_CMD>'; |
||
11 | |||
12 | const SHOULD_BE_PRESENT = "set -euo pipefail"; |
||
13 | |||
14 | /** |
||
15 | * {@inheritdoc} |
||
16 | */ |
||
17 | public function parseContent(string $content, Script $script, ScriptLoader $loader): array |
||
18 | { |
||
19 | $this->testContentContainsMarker($content); |
||
20 | $this->testScriptFileFitsRequirements($script); |
||
21 | |||
22 | $shouldWarn = $this->shouldWarnAboutBestPractice($content); |
||
23 | $warning = null; |
||
24 | |||
25 | if ($shouldWarn) { |
||
26 | $warning = 'Execution of this script is not secure, please consider adding <bold>set -euo pipefail</bold> in the beginning'; |
||
27 | } |
||
28 | |||
29 | return [new BashCommand($script, $warning)]; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param Script $script |
||
34 | */ |
||
35 | private function testScriptFileFitsRequirements(Script $script) |
||
45 | |||
46 | /** |
||
47 | * @param string $content |
||
48 | */ |
||
49 | private function testContentContainsMarker(string $content) |
||
55 | |||
56 | /** |
||
57 | * @param string $content |
||
58 | * @return bool |
||
59 | */ |
||
60 | private function shouldWarnAboutBestPractice(string $content): bool |
||
71 | } |
||
72 |