|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Runner\Docker; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ArgsBuilder |
|
9
|
|
|
* |
|
10
|
|
|
* Utility class to build command line arguments for hashmaps, e.g. |
|
11
|
|
|
* for environment variables (-e, --env) or labels (-l, --label). |
|
12
|
|
|
* |
|
13
|
|
|
* @package Ktomk\Pipelines\Runner\Docker |
|
14
|
|
|
*/ |
|
15
|
|
|
class ArgsBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $option |
|
19
|
|
|
* @param string[] $list |
|
20
|
|
|
* |
|
21
|
|
|
* @return string[] |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public static function optList($option, array $list) |
|
24
|
|
|
{ |
|
25
|
1 |
|
$args = array(); |
|
26
|
1 |
|
foreach ($list as $value) { |
|
27
|
1 |
|
$args[] = $option; |
|
28
|
1 |
|
$args[] = $value; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
return $args; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Multiple options from key/value map |
|
36
|
|
|
* |
|
37
|
|
|
* --option key=value --option key ... |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $option "-e" or "-l" typically for Docker binary |
|
40
|
|
|
* @param array $map |
|
41
|
|
|
* @param bool $dropNullEntries |
|
42
|
|
|
* |
|
43
|
|
|
* @return string[] f options (from $option) and values, ['-e', 'val1', '-e', 'val2', ...] |
|
44
|
|
|
* |
|
45
|
|
|
* @see \Ktomk\Pipelines\Runner\Env::createArgVarDefinitions |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public static function optMap($option, array $map, $dropNullEntries = false) |
|
48
|
|
|
{ |
|
49
|
1 |
|
return self::optList($option, self::mapKeyValues($map, $dropNullEntries)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $map |
|
54
|
|
|
* @param bool $dropNullEntries |
|
55
|
|
|
* |
|
56
|
|
|
* @return string[] |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public static function mapKeyValues(array $map, $dropNullEntries = false) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$array = array(); |
|
61
|
|
|
|
|
62
|
1 |
|
foreach ($map as $key => $value) { |
|
63
|
1 |
|
if (isset($value)) { |
|
64
|
1 |
|
$array[] = sprintf('%s=%s', $key, $value); |
|
65
|
|
|
|
|
66
|
1 |
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$dropNullEntries || $array[] = (string)$key; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
return $array; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|