2017-07-21 21:15:43 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse;
|
|
|
|
|
|
|
|
class CLI {
|
|
|
|
protected $args = [];
|
|
|
|
|
|
|
|
protected function usage(): string {
|
|
|
|
$prog = basename($_SERVER['argv'][0]);
|
|
|
|
return <<<USAGE_TEXT
|
|
|
|
Usage:
|
|
|
|
$prog daemon
|
|
|
|
$prog feed refresh <n>
|
2017-08-20 03:56:32 +00:00
|
|
|
$prog conf save-defaults <file>
|
2017-08-28 23:38:58 +00:00
|
|
|
$prog user add <username> [<password>]
|
2017-07-21 21:15:43 +00:00
|
|
|
$prog --version
|
|
|
|
$prog --help | -h
|
|
|
|
|
|
|
|
The Arsse command-line interface currently allows you to start the refresh
|
2017-08-28 23:38:58 +00:00
|
|
|
daemon, refresh a specific feed by numeric ID, add a user, or save default
|
|
|
|
configuration to a sample file.
|
2017-07-21 21:15:43 +00:00
|
|
|
USAGE_TEXT;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function __construct(array $argv = null) {
|
2017-09-05 23:35:14 +00:00
|
|
|
$argv = $argv ?? array_slice($_SERVER['argv'], 1);
|
2017-07-21 21:15:43 +00:00
|
|
|
$this->args = \Docopt::handle($this->usage(), [
|
|
|
|
'argv' => $argv,
|
|
|
|
'help' => true,
|
2017-10-01 13:33:49 +00:00
|
|
|
'version' => Arsse::VERSION,
|
2017-07-21 21:15:43 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:56:32 +00:00
|
|
|
protected function loadConf(): bool {
|
|
|
|
// FIXME: this should be a method of the Conf class
|
|
|
|
Arsse::load(new Conf());
|
2017-08-29 14:50:31 +00:00
|
|
|
if (file_exists(BASE."config.php")) {
|
2017-08-20 03:56:32 +00:00
|
|
|
Arsse::$conf->importFile(BASE."config.php");
|
|
|
|
}
|
2017-08-28 23:38:58 +00:00
|
|
|
// command-line operations will never respect authorization
|
|
|
|
Arsse::$user->authorizationEnabled(false);
|
2017-08-20 03:56:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function dispatch(array $args = null): int {
|
2017-07-21 21:15:43 +00:00
|
|
|
// act on command line
|
2017-09-05 23:35:14 +00:00
|
|
|
$args = $args ?? $this->args;
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($this->command("daemon", $args)) {
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->loadConf();
|
2017-07-21 21:15:43 +00:00
|
|
|
return $this->daemon();
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif ($this->command("feed refresh", $args)) {
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->loadConf();
|
2017-07-21 21:15:43 +00:00
|
|
|
return $this->feedRefresh((int) $args['<n>']);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif ($this->command("conf save-defaults", $args)) {
|
2017-08-20 03:56:32 +00:00
|
|
|
return $this->confSaveDefaults($args['<file>']);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif ($this->command("user add", $args)) {
|
2017-08-28 23:38:58 +00:00
|
|
|
$this->loadConf();
|
|
|
|
return $this->userAdd($args['<username>'], $args['<password>']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function command($cmd, $args): bool {
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach (explode(" ", $cmd) as $part) {
|
|
|
|
if (!$args[$part]) {
|
2017-08-28 23:38:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
2017-08-28 23:38:58 +00:00
|
|
|
return true;
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function daemon(bool $loop = true): int {
|
2017-07-21 21:15:43 +00:00
|
|
|
(new Service)->watch($loop);
|
|
|
|
return 0; // FIXME: should return the exception code of thrown exceptions
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function feedRefresh(int $id): int {
|
2017-08-18 03:05:08 +00:00
|
|
|
return (int) !Arsse::$db->feedUpdate($id); // FIXME: exception error codes should be returned here
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
2017-08-20 03:56:32 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function confSaveDefaults(string $file): int {
|
2017-08-20 03:56:32 +00:00
|
|
|
return (int) !(new Conf)->exportFile($file, true);
|
|
|
|
}
|
2017-08-28 23:38:58 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function userAdd(string $user, string $password = null): int {
|
2017-08-28 23:38:58 +00:00
|
|
|
$passwd = Arsse::$user->add($user, $password);
|
2017-08-29 14:50:31 +00:00
|
|
|
if (is_null($password)) {
|
2017-09-28 23:25:31 +00:00
|
|
|
echo $passwd.\PHP_EOL;
|
2017-08-28 23:38:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|