|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Conveyor package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jeroen Fiege <[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 Webcreate\Conveyor\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
|
|
18
|
|
|
class StatusCommand extends AbstractCommand |
|
19
|
|
|
{ |
|
20
|
2 |
|
protected function configure() |
|
21
|
|
|
{ |
|
22
|
2 |
|
$this |
|
23
|
2 |
|
->setName('status') |
|
24
|
2 |
|
->setDescription('Show status for each target') |
|
25
|
2 |
|
->addArgument('target', InputArgument::OPTIONAL, 'Show the status for a specific target') |
|
26
|
|
|
; |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$deploy = $this->getConveyor($input, $output, $this->getHelperSet()); |
|
32
|
|
|
|
|
33
|
|
|
$target = $input->getArgument('target'); |
|
34
|
|
|
|
|
35
|
|
|
$status = $deploy->status($target); |
|
36
|
|
|
|
|
37
|
|
|
if (count($status) > 0) { |
|
38
|
|
|
$output->writeln('<comment>Available targets:</comment>'); |
|
39
|
|
|
|
|
40
|
|
|
// calculate max string length of targets |
|
41
|
|
|
$maxStrlen = max(array_map(function ($item) { return strlen($item); }, array_keys($status))); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
foreach ($status as $target => $info) { |
|
44
|
|
|
$abortMessage = null; |
|
45
|
|
|
|
|
46
|
|
|
if (isset($info['error'])) { |
|
47
|
|
|
$abortMessage = sprintf('<error>Error: %s</error>', $info['error']->getMessage()); |
|
48
|
|
|
} elseif (false === $info) { |
|
49
|
|
|
$abortMessage = 'Not deployed'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (null !== $abortMessage) { |
|
53
|
|
|
$output->writeln( |
|
54
|
|
|
sprintf( |
|
55
|
|
|
' <info>%-'.($maxStrlen + 2).'s</info> %s', |
|
56
|
|
|
$target, |
|
57
|
|
|
$abortMessage |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$version = $info['remoteVersion']; |
|
65
|
|
|
$localVersion = $info['localVersion']; |
|
66
|
|
|
$versionCompare = $info['compare']; |
|
67
|
|
|
|
|
68
|
|
|
$statusTxt = ''; |
|
69
|
|
|
if (0 === $versionCompare) { |
|
70
|
|
|
$statusTxt = 'Up-to-date'; |
|
71
|
|
|
} elseif (1 === $versionCompare) { |
|
72
|
|
|
$statusTxt = sprintf('Behind by %d commit(s) on %s', count($info['changelog']), $localVersion->getName()); |
|
73
|
|
|
} elseif (-1 === $versionCompare) { |
|
74
|
|
|
$statusTxt = sprintf('Ahead by %d commit(s) on %s', count($info['changelog']), $localVersion->getName()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$output->writeln( |
|
78
|
|
|
sprintf( |
|
79
|
|
|
' <info>%-'.($maxStrlen + 2).'s</info> <info>%s</info> (<comment>%s</comment>) %s', |
|
80
|
|
|
$target, |
|
81
|
|
|
$version->getName(), |
|
82
|
|
|
$version->getBuild(), |
|
83
|
|
|
$statusTxt |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$baseVersion = 'dev-' . $deploy->getRepository()->getMasterBranch(); |
|
89
|
|
|
|
|
90
|
|
|
$output->writeln(''); |
|
91
|
|
|
$output->writeln(sprintf('Run `%s diff <target> %s` to view changed files', basename($_SERVER['argv'][0]), $baseVersion)); |
|
92
|
|
|
} else { |
|
93
|
|
|
$output->writeln('No targets defined'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: