1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console\Command; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp; |
6
|
|
|
use InfluxDB; |
7
|
|
|
use Martial\Transmission\API; |
8
|
|
|
use Popstas\Transmission\Console; |
9
|
|
|
use Popstas\Transmission\Console\Application; |
10
|
|
|
use Popstas\Transmission\Console\Config; |
11
|
|
|
use Popstas\Transmission\Console\TransmissionClient; |
12
|
|
|
use Psr\Log\LogLevel; |
13
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class Command extends BaseCommand |
20
|
|
|
{ |
21
|
|
|
private $rawHelp; |
22
|
|
|
|
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Dry run, don\'t change any data'); |
26
|
|
|
$this->addOption('config', null, InputOption::VALUE_OPTIONAL, 'Configuration file'); |
27
|
|
|
$this->addOption('transmission-host', null, InputOption::VALUE_OPTIONAL, 'Transmission host'); |
28
|
|
|
$this->addOption('transmission-port', null, InputOption::VALUE_OPTIONAL, 'Transmission port'); |
29
|
|
|
$this->addOption('transmission-user', null, InputOption::VALUE_OPTIONAL, 'Transmission user'); |
30
|
|
|
$this->addOption('transmission-password', null, InputOption::VALUE_OPTIONAL, 'Transmission password'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Application |
35
|
|
|
*/ |
36
|
|
|
public function getApplication() |
37
|
|
|
{ |
38
|
|
|
return parent::getApplication(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
// logger |
44
|
|
|
$logger = $this->getApplication()->getLogger(); |
45
|
|
|
if (!isset($logger)) { |
46
|
|
|
$verbosityLevelMap = [ |
47
|
|
|
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL, |
48
|
|
|
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, |
49
|
|
|
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG, |
50
|
|
|
]; |
51
|
|
|
$logger = new ConsoleLogger($output, $verbosityLevelMap); |
52
|
|
|
$this->getApplication()->setLogger($logger); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// config |
56
|
|
|
$config = $this->getApplication()->getConfig(); |
57
|
|
|
if (!isset($config)) { |
58
|
|
|
$config = new Config($input->getOption('config')); |
59
|
|
|
try { |
60
|
|
|
$config->loadConfigFile(); |
61
|
|
|
} catch (\RuntimeException $e) { |
62
|
|
|
$logger->critical($e->getMessage()); |
63
|
|
|
$this->setCode(function () { |
64
|
|
|
return 1; |
65
|
|
|
}); |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$this->getApplication()->setConfig($config); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// compatibility: first client available at old config names |
72
|
|
|
// this overrides config values to options from command line arguments |
73
|
|
|
$firstTransmission = $config->get('transmission')[0]; |
74
|
|
|
$vars = ['host', 'port', 'user', 'password']; |
75
|
|
|
foreach ($vars as $var) { |
76
|
|
|
$configName = 'transmission-' . $var; |
77
|
|
|
$config->set($configName, $this->getInputOption($input, $configName, $firstTransmission[$var])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// client |
81
|
|
|
$client = $this->getApplication()->getClient(); |
82
|
|
|
if (!isset($client)) { |
83
|
|
|
$client = $this->createTransmissionClient( |
84
|
|
|
$config->get('transmission-host'), |
85
|
|
|
$config->get('transmission-port'), |
86
|
|
|
$config->get('transmission-user'), |
87
|
|
|
$config->get('transmission-password') |
88
|
|
|
); |
89
|
|
|
$this->getApplication()->setClient($client); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$logger->info('[{date}] command: {args}', [ |
93
|
|
|
'date' => date('Y-m-d H:i:s'), |
94
|
|
|
'args' => implode(' ', array_slice($_SERVER['argv'], 1)), |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
parent::initialize($input, $output); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function createTransmissionClient($host, $port, $user, $password) |
101
|
|
|
{ |
102
|
|
|
$logger = $this->getApplication()->getLogger(); |
103
|
|
|
|
104
|
|
|
$connect = ['host' => $host, 'port' => $port, 'user' => $user, 'password' => $password]; |
105
|
|
|
|
106
|
|
|
$baseUri = 'http://' . $connect['host'] . ':' . $connect['port'] . '/transmission/rpc'; |
107
|
|
|
$httpClient = new GuzzleHttp\Client(['base_uri' => $baseUri]); |
108
|
|
|
|
109
|
|
|
$api = new API\RpcClient($httpClient, $connect['user'], $connect['password']); |
110
|
|
|
$api->setLogger($logger); |
111
|
|
|
|
112
|
|
|
$logger->debug('Connect Transmission using: {user}:{password}@{host}:{port}', $connect); |
113
|
|
|
|
114
|
|
|
return new TransmissionClient($api); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function getInputOption(InputInterface $input, $optionName, $defaultValue = null) |
118
|
|
|
{ |
119
|
|
|
$value = $defaultValue; |
120
|
|
|
$optionValue = $input->hasOption($optionName) ? $input->getOption($optionName) : null; |
121
|
|
|
if (isset($optionValue)) { |
122
|
|
|
$value = $optionValue; |
123
|
|
|
} |
124
|
|
|
return $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function dryRun( |
128
|
|
|
InputInterface $input, |
129
|
|
|
OutputInterface $output, |
130
|
|
|
\Closure $callback, |
131
|
|
|
$message = 'dry-run, don\'t doing anything' |
132
|
|
|
) { |
133
|
|
|
if (!$input->getOption('dry-run')) { |
134
|
|
|
return $callback(); |
135
|
|
|
} else { |
136
|
|
|
$output->writeln($message); |
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setHelp($help) |
142
|
|
|
{ |
143
|
|
|
$this->rawHelp = $help; |
144
|
|
|
$help = preg_replace('/```\n(.*?)\n```/mis', '<info>$1</info>', $help); |
145
|
|
|
$help = preg_replace('/(\n|^)#+ (.*)/', '$1<question>$2</question>', $help); |
146
|
|
|
$help = preg_replace('/`([^`]*?)`/', '<comment>$1</comment>', $help); |
147
|
|
|
$help = preg_replace('/\*\*(.*?)\*\*/', '<comment>$1</comment>', $help); |
148
|
|
|
return parent::setHelp($help); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getRawHelp() |
152
|
|
|
{ |
153
|
|
|
return $this->rawHelp; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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: