|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the slack-cli package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
|
13
|
|
|
|
|
14
|
|
|
use CL\SlackCli\Config\Config; |
|
15
|
|
|
use CL\SlackCli\Config\ConfigFactory; |
|
16
|
|
|
use CL\SlackCli\Console\Application; |
|
17
|
|
|
use Composer\Config\JsonConfigSource; |
|
18
|
|
|
use Composer\Json\JsonFile; |
|
19
|
|
|
use Symfony\Component\Console\Command\Command; |
|
20
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @author Cas Leentfaar <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class AbstractCommand extends Command |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var InputInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $input; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var OutputInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $output; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Config |
|
42
|
|
|
*/ |
|
43
|
|
|
private $config; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var JsonConfigSource |
|
47
|
|
|
*/ |
|
48
|
|
|
private $configSource; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $configPath; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private $configPathFromInput; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
* |
|
63
|
|
|
* @return Application |
|
64
|
|
|
*/ |
|
65
|
24 |
|
public function getApplication() |
|
66
|
1 |
|
{ |
|
67
|
24 |
|
return parent::getApplication(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
24 |
|
protected function configure() |
|
74
|
|
|
{ |
|
75
|
24 |
|
$this->addOption( |
|
76
|
24 |
|
'configuration-path', |
|
77
|
24 |
|
null, |
|
78
|
24 |
|
InputOption::VALUE_REQUIRED, |
|
79
|
|
|
'Configuration file to use during this command, defaults to %YOUR_HOME_DIR%/slack-cli/config.json' |
|
80
|
24 |
|
); |
|
81
|
24 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
*/ |
|
86
|
24 |
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
87
|
|
|
{ |
|
88
|
24 |
|
$this->input = $input; |
|
89
|
24 |
|
$this->output = $output; |
|
90
|
24 |
|
$this->configPathFromInput = $this->input->getOption('configuration-path'); |
|
91
|
24 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
1 |
|
protected function getConfigPath() |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->getConfig(); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this->configPath; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return JsonConfigSource |
|
105
|
|
|
*/ |
|
106
|
2 |
|
protected function getConfigSource() |
|
107
|
|
|
{ |
|
108
|
2 |
|
$this->getConfig(); |
|
109
|
|
|
|
|
110
|
2 |
|
return $this->configSource; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return Config |
|
115
|
|
|
* |
|
116
|
|
|
* @throws \Exception |
|
117
|
|
|
*/ |
|
118
|
5 |
|
protected function getConfig() |
|
119
|
|
|
{ |
|
120
|
5 |
|
if (!isset($this->config)) { |
|
121
|
5 |
|
$configFilePath = $this->configPathFromInput ?: (ConfigFactory::getHomeDir() . '/config.json'); |
|
122
|
5 |
|
$this->config = ConfigFactory::createConfig($configFilePath); |
|
123
|
5 |
|
$configFile = new JsonFile($configFilePath); |
|
124
|
5 |
|
$this->configPath = $configFile->getPath(); |
|
125
|
5 |
|
$this->configSource = new JsonConfigSource($configFile); |
|
126
|
|
|
|
|
127
|
|
|
// initialize the global file if it's not there |
|
128
|
5 |
|
if (!$configFile->exists()) { |
|
129
|
1 |
|
$path = $configFile->getPath(); |
|
130
|
1 |
|
$dir = dirname($path); |
|
131
|
1 |
|
if (!is_dir($dir)) { |
|
132
|
|
|
mkdir($dir, 0777, true); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
touch($configFile->getPath()); |
|
136
|
1 |
|
$configFile->write(['config' => new \ArrayObject()]); |
|
137
|
1 |
|
@chmod($configFile->getPath(), 0600); |
|
|
|
|
|
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
5 |
|
if (!$configFile->exists()) { |
|
141
|
|
|
throw new \RuntimeException('No config.json found in the current directory'); |
|
142
|
|
|
} |
|
143
|
5 |
|
} |
|
144
|
|
|
|
|
145
|
5 |
|
return $this->config; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $message |
|
150
|
|
|
*/ |
|
151
|
18 |
|
protected function writeOk($message) |
|
152
|
|
|
{ |
|
153
|
18 |
|
$this->output->writeln(sprintf('<fg=green>✔</fg=green> %s', $message)); |
|
154
|
18 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $message |
|
158
|
|
|
*/ |
|
159
|
1 |
|
protected function writeComment($message) |
|
160
|
|
|
{ |
|
161
|
1 |
|
$this->output->writeln(sprintf('<comment>%s</comment>', $message)); |
|
162
|
1 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $message |
|
166
|
|
|
*/ |
|
167
|
21 |
|
protected function writeError($message) |
|
168
|
|
|
{ |
|
169
|
21 |
|
$this->output->writeln(sprintf('<fg=red>✘</fg=red> %s', $message)); |
|
170
|
21 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param array $headers |
|
174
|
|
|
* |
|
175
|
|
|
* @return Table |
|
176
|
|
|
*/ |
|
177
|
1 |
|
protected function createTable(array $headers = []) |
|
178
|
|
|
{ |
|
179
|
1 |
|
$table = new Table($this->output); |
|
180
|
|
|
|
|
181
|
1 |
|
if (!empty($headers)) { |
|
182
|
1 |
|
$table->setHeaders($headers); |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
return $table; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param array $keysValues |
|
190
|
|
|
* |
|
191
|
|
|
* @return Table |
|
192
|
|
|
*/ |
|
193
|
1 |
|
protected function createKeyValueTable(array $keysValues) |
|
194
|
|
|
{ |
|
195
|
1 |
|
$table = $this->createTable(['Key', 'Value']); |
|
196
|
1 |
|
foreach ($keysValues as $key => $value) { |
|
197
|
1 |
|
$table->addRow([$key, $value]); |
|
198
|
1 |
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
return $table; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return bool |
|
205
|
|
|
*/ |
|
206
|
20 |
|
protected function isTest() |
|
207
|
|
|
{ |
|
208
|
20 |
|
$env = $this->input->getOption('env'); |
|
209
|
|
|
|
|
210
|
20 |
|
if ($env === 'test-success' || $env === 'test-failure') { |
|
211
|
20 |
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return false; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return bool |
|
219
|
|
|
*/ |
|
220
|
19 |
|
protected function isTestSuccess() |
|
221
|
|
|
{ |
|
222
|
19 |
|
return $this->input->getOption('env') === 'test-success'; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: