-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseCommand.php
78 lines (55 loc) · 1.89 KB
/
BaseCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace Sigmie\Cli;
use Sigmie\Base\Contracts\API;
use Sigmie\Base\Http\Connection;
use Sigmie\Cli\Contracts\OutputFormat;
use Sigmie\Cli\Outputs\ClientInfo;
use Sigmie\Http\JSONClient;
use Sigmie\Http\JSONResponse;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
abstract class BaseCommand extends Command
{
use API;
protected InputInterface $input;
protected OutputInterface $output;
protected JSONClient $client;
protected Config $config;
public function handleRaw(JSONResponse $response)
{
$this->output->writeln(json_encode($response->json(), JSON_PRETTY_PRINT));
}
abstract protected function executeCommand(): int;
protected function configure()
{
parent::configure();
$this->addOption('raw', 'w', InputOption::VALUE_NONE, 'Render the raw json response returned from Elasticsearch');
$this->config = new Config();
$this->setHttpConnection($this->createHttpConnection());
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->renderInfo();
return $this->executeCommand();
}
protected function output(OutputFormat $outputFormat)
{
$outputFormat->output($this->output);
}
private function createHttpConnection()
{
$cluster = $this->config->getActiveCluster();
return new Connection(JSONClient::create($cluster['host'].':'.$cluster['port']));
}
private function renderInfo()
{
$cluster = $this->config->getActiveCluster();
$info = new ClientInfo($cluster['host'], $cluster['port'], '7.8.0');
$info->output($this->output);
}
}