Completed
Push — master ( 52ead2...fd316a )
by David
12s queued 10s
created

YamlTools   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 11 2
A delete() 0 11 2
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\YamlTools;
4
5
use Symfony\Component\Process\Process;
6
7
class YamlTools
8
{
9
    public const TMP_YAML_FILE = '/tmp/tmp.yml';
10
11
    /**
12
     * Merge the content of $inputFile2 with $inputFile1's one, then write it into $outputFile (or stdout if empty)
13
     */
14
    public static function merge(string $inputFile1, string $inputFile2, string $outputFile = ''): void
15
    {
16
        $command = array('yaml-tools', 'merge', '-i', $inputFile1, $inputFile2);
17
        if (!empty($outputFile)) {
18
            $command[] = '-o';
19
            $command[] = $outputFile;
20
        }
21
        $process = new Process($command);
22
        $process->enableOutput();
23
        $process->setTty(true);
24
        $process->mustRun();
25
    }
26
27
    /**
28
     * Delete one element of the $inputFile (e.g. foo.bar[2].baz), then write it into $outputFile (or stdout if empty)
29
     */
30
    public static function delete(string $elemToDelete, string $inputFile, string $outputFile = ''): void
31
    {
32
        $command = array('yaml-tools', 'delete', $elemToDelete, '-i', $inputFile);
33
        if (!empty($outputFile)) {
34
            $command[] = '-o';
35
            $command[] = $outputFile;
36
        }
37
        $process = new Process($command);
38
        $process->enableOutput();
39
        $process->setTty(true);
40
        $process->mustRun();
41
    }
42
}
43