Browse Source

Simplify CLI command processing

rpm
J. King 3 years ago
parent
commit
3795b1ccd8
  1. 80
      lib/CLI.php

80
lib/CLI.php

@ -88,12 +88,12 @@ Commands:
user set <username> <property> <value> user set <username> <property> <value>
Sets a user's metadata proprty to the supplied value. See below for Sets a user's metadata property to the supplied value. See below for
details on the various properties available. details on the various properties available.
user unset <username> <property> user unset <username> <property>
Sets a user's metadata proprty to its default value. See below for Sets a user's metadata property to its default value. See below for
details on the various properties available. What the default value details on the various properties available. What the default value
for a property evaluates to depends on which protocol is used. for a property evaluates to depends on which protocol is used.
@ -215,16 +215,14 @@ USAGE_TEXT;
return str_replace("arsse.php", $prog, self::USAGE); return str_replace("arsse.php", $prog, self::USAGE);
} }
protected function command(array $options, $args): string { protected function command($args): string {
foreach ($options as $cmd) { $out = [];
foreach (explode(" ", $cmd) as $part) { foreach ($args as $k => $v) {
if (!$args[$part]) { if (preg_match("/^[a-z]/", $k) && $v === true) {
continue 2; $out[] = $k;
} }
} }
return $cmd; return implode(" ", $out);
}
return "";
} }
/** @codeCoverageIgnore */ /** @codeCoverageIgnore */
@ -248,18 +246,18 @@ USAGE_TEXT;
'help' => false, 'help' => false,
]); ]);
try { try {
$cmd = $this->command(["-h", "--help", "--version", "daemon", "feed refresh", "feed refresh-all", "conf save-defaults", "user", "export", "import"], $args); $cmd = $this->command($args);
if ($cmd && !in_array($cmd, ["-h", "--help", "--version", "conf save-defaults"])) { if ($cmd && !in_array($cmd, ["", "conf save-defaults"])) {
// only certain commands don't require configuration to be loaded // only certain commands don't require configuration to be loaded
$this->loadConf(); $this->loadConf();
} }
switch ($cmd) { switch ($cmd) {
case "-h": case "":
case "--help": if ($args['--version']) {
echo $this->usage($argv0).\PHP_EOL;
return 0;
case "--version":
echo Arsse::VERSION.\PHP_EOL; echo Arsse::VERSION.\PHP_EOL;
} elseif ($args['--help'] || $args['-h']) {
echo $this->usage($argv0).\PHP_EOL;
}
return 0; return 0;
case "daemon": case "daemon":
Arsse::$obj->get(Service::class)->watch(true); Arsse::$obj->get(Service::class)->watch(true);
@ -272,8 +270,6 @@ USAGE_TEXT;
case "conf save-defaults": case "conf save-defaults":
$file = $this->resolveFile($args['<file>'], "w"); $file = $this->resolveFile($args['<file>'], "w");
return (int) !Arsse::$obj->get(Conf::class)->exportFile($file, true); return (int) !Arsse::$obj->get(Conf::class)->exportFile($file, true);
case "user":
return $this->userManage($args);
case "export": case "export":
$u = $args['<username>']; $u = $args['<username>'];
$file = $this->resolveFile($args['<file>'], "w"); $file = $this->resolveFile($args['<file>'], "w");
@ -282,28 +278,13 @@ USAGE_TEXT;
$u = $args['<username>']; $u = $args['<username>'];
$file = $this->resolveFile($args['<file>'], "r"); $file = $this->resolveFile($args['<file>'], "r");
return (int) !Arsse::$obj->get(OPML::class)->importFile($file, $u, ($args['--flat'] || $args['-f']), ($args['--replace'] || $args['-r'])); return (int) !Arsse::$obj->get(OPML::class)->importFile($file, $u, ($args['--flat'] || $args['-f']), ($args['--replace'] || $args['-r']));
} case "user add":
} catch (AbstractException $e) {
$this->logError($e->getMessage());
return $e->getCode();
}
} // @codeCoverageIgnore
/** @codeCoverageIgnore */
protected function logError(string $msg): void {
fwrite(STDERR, $msg.\PHP_EOL);
}
protected function userManage($args): int {
$cmd = $this->command(["add", "remove", "show", "set", "unset", "set-pass", "unset-pass", "list", "auth"], $args);
switch ($cmd) {
case "add":
$out = $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]); $out = $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]);
if ($args['--admin']) { if ($args['--admin']) {
Arsse::$user->propertiesSet($args["<username>"], ['admin' => true]); Arsse::$user->propertiesSet($args["<username>"], ['admin' => true]);
} }
return $out; return $out;
case "set-pass": case "user set-pass":
if ($args['--fever']) { if ($args['--fever']) {
$passwd = Arsse::$obj->get(Fever::class)->register($args["<username>"], $args["<password>"]); $passwd = Arsse::$obj->get(Fever::class)->register($args["<username>"], $args["<password>"]);
if (is_null($args["<password>"])) { if (is_null($args["<password>"])) {
@ -314,29 +295,36 @@ USAGE_TEXT;
return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"], $args["--oldpass"]); return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"], $args["--oldpass"]);
} }
// no break // no break
case "unset-pass": case "user unset-pass":
if ($args['--fever']) { if ($args['--fever']) {
Arsse::$obj->get(Fever::class)->unregister($args["<username>"]); Arsse::$obj->get(Fever::class)->unregister($args["<username>"]);
} else { } else {
Arsse::$user->passwordUnset($args["<username>"], $args["--oldpass"]); Arsse::$user->passwordUnset($args["<username>"], $args["--oldpass"]);
} }
return 0; return 0;
case "remove": case "user remove":
return (int) !Arsse::$user->remove($args["<username>"]); return (int) !Arsse::$user->remove($args["<username>"]);
case "show": case "user show":
return $this->userShowProperties($args["<username>"]); return $this->userShowProperties($args["<username>"]);
case "set": case "user set":
return (int) !Arsse::$user->propertiesSet($args["<username>"], [$args["<property>"] => $args["<value>"]]); return (int) !Arsse::$user->propertiesSet($args["<username>"], [$args["<property>"] => $args["<value>"]]);
case "unset": case "user unset":
return (int) !Arsse::$user->propertiesSet($args["<username>"], [$args["<property>"] => null]); return (int) !Arsse::$user->propertiesSet($args["<username>"], [$args["<property>"] => null]);
case "auth": case "user auth":
return $this->userAuthenticate($args["<username>"], $args["<password>"], $args["--fever"]); return $this->userAuthenticate($args["<username>"], $args["<password>"], $args["--fever"]);
case "list": case "user list":
case "": case "user":
return $this->userList(); return $this->userList();
default:
throw new Exception("constantUnknown", $cmd); // @codeCoverageIgnore
} }
} catch (AbstractException $e) {
$this->logError($e->getMessage());
return $e->getCode();
}
} // @codeCoverageIgnore
/** @codeCoverageIgnore */
protected function logError(string $msg): void {
fwrite(STDERR, $msg.\PHP_EOL);
} }
protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int { protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int {

Loading…
Cancel
Save