|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Console\DockerCompose; |
|
4
|
|
|
|
|
5
|
|
|
use App\PorterLibrary; |
|
6
|
|
|
use App\Support\Contracts\Cli; |
|
7
|
|
|
|
|
8
|
|
|
class CliCommand |
|
9
|
|
|
{ |
|
10
|
|
|
protected $command = ''; |
|
11
|
|
|
protected $interactive = false; |
|
12
|
|
|
protected $realTime = false; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Cli |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $cli; |
|
18
|
|
|
|
|
19
|
28 |
|
public function __construct(Cli $cli, $command) |
|
20
|
|
|
{ |
|
21
|
28 |
|
$this->command = trim($command); |
|
22
|
28 |
|
$this->cli = $cli; |
|
23
|
28 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Append a bash command, optionally with a further call. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string|null $command |
|
29
|
|
|
* |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function bash($command = null) |
|
33
|
|
|
{ |
|
34
|
3 |
|
$this->interactive(); |
|
35
|
3 |
|
$this->append('bash'); |
|
36
|
|
|
|
|
37
|
3 |
|
if ($command) { |
|
38
|
1 |
|
$this->append("-c \"$command\""); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Append to a command. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string|null $string |
|
48
|
|
|
* |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function append($string = null) |
|
52
|
|
|
{ |
|
53
|
4 |
|
$this->command = trim($this->command." {$string}"); |
|
54
|
|
|
|
|
55
|
4 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set a command as being interactive (i.e. passthru() in php). |
|
60
|
|
|
* |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
6 |
|
public function interactive() |
|
64
|
|
|
{ |
|
65
|
6 |
|
$this->interactive = true; |
|
66
|
|
|
|
|
67
|
6 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set a command as not being interactive. |
|
72
|
|
|
* |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function notInteractive() |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->interactive = false; |
|
78
|
|
|
|
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check if the command is expected to be interactive. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
16 |
|
public function isInteractive() |
|
88
|
|
|
{ |
|
89
|
16 |
|
return $this->interactive; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set our expectation to see real-time output. |
|
94
|
|
|
* |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
9 |
|
public function realTime() |
|
98
|
|
|
{ |
|
99
|
9 |
|
$this->realTime = true; |
|
100
|
|
|
|
|
101
|
9 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set our expectation NOT to see real-time output. |
|
106
|
|
|
* |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function notRealTime() |
|
110
|
|
|
{ |
|
111
|
2 |
|
$this->realTime = false; |
|
112
|
|
|
|
|
113
|
2 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Check if we're expecting real0time output. |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
15 |
|
public function isRealTime() |
|
122
|
|
|
{ |
|
123
|
15 |
|
return $this->realTime; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Prepare the full command string. |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
21 |
|
public function prepare() |
|
132
|
|
|
{ |
|
133
|
21 |
|
return trim( |
|
134
|
|
|
'docker-compose -f ' |
|
135
|
21 |
|
.app(PorterLibrary::class)->dockerComposeFile() |
|
136
|
21 |
|
.' -p porter ' |
|
137
|
21 |
|
.$this->command |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Execute the command. |
|
143
|
|
|
* |
|
144
|
|
|
* @return string|null |
|
145
|
|
|
*/ |
|
146
|
13 |
|
public function perform() |
|
147
|
|
|
{ |
|
148
|
13 |
|
if ($this->isInteractive()) { |
|
149
|
1 |
|
$this->cli->passthru($this->prepare()); |
|
150
|
|
|
|
|
151
|
1 |
|
return; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
13 |
|
if ($this->isRealTime()) { |
|
155
|
7 |
|
return $this->cli->execRealTime($this->prepare()); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
11 |
|
return $this->cli->exec($this->prepare()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Return the Cli instance for this CliCommand. |
|
163
|
|
|
* |
|
164
|
|
|
* @return Cli |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function getCli() |
|
167
|
|
|
{ |
|
168
|
1 |
|
return $this->cli; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set the timeout for the Cli instance. |
|
173
|
|
|
* |
|
174
|
|
|
* @param int $seconds |
|
175
|
|
|
* |
|
176
|
|
|
* @return CliCommand |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function setTimeout(int $seconds) |
|
179
|
|
|
{ |
|
180
|
1 |
|
$this->cli->setTimeout($seconds); |
|
181
|
|
|
|
|
182
|
1 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Remove the timeout for the Cli instance. (Just a nicer way to write it). |
|
187
|
|
|
* |
|
188
|
|
|
* @return CliCommand |
|
189
|
|
|
*/ |
|
190
|
2 |
|
public function doNotTimeout() |
|
191
|
|
|
{ |
|
192
|
2 |
|
$this->cli->doNotTimeout(); |
|
193
|
|
|
|
|
194
|
2 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|