diff --git a/lib/CLI.php b/lib/CLI.php index 8e8c1e6..53c5075 100644 --- a/lib/CLI.php +++ b/lib/CLI.php @@ -88,12 +88,12 @@ Commands: user set - 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. user unset - 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 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); } - protected function command(array $options, $args): string { - foreach ($options as $cmd) { - foreach (explode(" ", $cmd) as $part) { - if (!$args[$part]) { - continue 2; - } + protected function command($args): string { + $out = []; + foreach ($args as $k => $v) { + if (preg_match("/^[a-z]/", $k) && $v === true) { + $out[] = $k; } - return $cmd; } - return ""; + return implode(" ", $out); } /** @codeCoverageIgnore */ @@ -248,18 +246,18 @@ USAGE_TEXT; 'help' => false, ]); try { - $cmd = $this->command(["-h", "--help", "--version", "daemon", "feed refresh", "feed refresh-all", "conf save-defaults", "user", "export", "import"], $args); - if ($cmd && !in_array($cmd, ["-h", "--help", "--version", "conf save-defaults"])) { + $cmd = $this->command($args); + if ($cmd && !in_array($cmd, ["", "conf save-defaults"])) { // only certain commands don't require configuration to be loaded $this->loadConf(); } switch ($cmd) { - case "-h": - case "--help": - echo $this->usage($argv0).\PHP_EOL; - return 0; - case "--version": - echo Arsse::VERSION.\PHP_EOL; + case "": + if ($args['--version']) { + echo Arsse::VERSION.\PHP_EOL; + } elseif ($args['--help'] || $args['-h']) { + echo $this->usage($argv0).\PHP_EOL; + } return 0; case "daemon": Arsse::$obj->get(Service::class)->watch(true); @@ -272,8 +270,6 @@ USAGE_TEXT; case "conf save-defaults": $file = $this->resolveFile($args[''], "w"); return (int) !Arsse::$obj->get(Conf::class)->exportFile($file, true); - case "user": - return $this->userManage($args); case "export": $u = $args['']; $file = $this->resolveFile($args[''], "w"); @@ -282,6 +278,43 @@ USAGE_TEXT; $u = $args['']; $file = $this->resolveFile($args[''], "r"); return (int) !Arsse::$obj->get(OPML::class)->importFile($file, $u, ($args['--flat'] || $args['-f']), ($args['--replace'] || $args['-r'])); + case "user add": + $out = $this->userAddOrSetPassword("add", $args[""], $args[""]); + if ($args['--admin']) { + Arsse::$user->propertiesSet($args[""], ['admin' => true]); + } + return $out; + case "user set-pass": + if ($args['--fever']) { + $passwd = Arsse::$obj->get(Fever::class)->register($args[""], $args[""]); + if (is_null($args[""])) { + echo $passwd.\PHP_EOL; + } + return 0; + } else { + return $this->userAddOrSetPassword("passwordSet", $args[""], $args[""], $args["--oldpass"]); + } + // no break + case "user unset-pass": + if ($args['--fever']) { + Arsse::$obj->get(Fever::class)->unregister($args[""]); + } else { + Arsse::$user->passwordUnset($args[""], $args["--oldpass"]); + } + return 0; + case "user remove": + return (int) !Arsse::$user->remove($args[""]); + case "user show": + return $this->userShowProperties($args[""]); + case "user set": + return (int) !Arsse::$user->propertiesSet($args[""], [$args[""] => $args[""]]); + case "user unset": + return (int) !Arsse::$user->propertiesSet($args[""], [$args[""] => null]); + case "user auth": + return $this->userAuthenticate($args[""], $args[""], $args["--fever"]); + case "user list": + case "user": + return $this->userList(); } } catch (AbstractException $e) { $this->logError($e->getMessage()); @@ -294,51 +327,6 @@ USAGE_TEXT; 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[""], $args[""]); - if ($args['--admin']) { - Arsse::$user->propertiesSet($args[""], ['admin' => true]); - } - return $out; - case "set-pass": - if ($args['--fever']) { - $passwd = Arsse::$obj->get(Fever::class)->register($args[""], $args[""]); - if (is_null($args[""])) { - echo $passwd.\PHP_EOL; - } - return 0; - } else { - return $this->userAddOrSetPassword("passwordSet", $args[""], $args[""], $args["--oldpass"]); - } - // no break - case "unset-pass": - if ($args['--fever']) { - Arsse::$obj->get(Fever::class)->unregister($args[""]); - } else { - Arsse::$user->passwordUnset($args[""], $args["--oldpass"]); - } - return 0; - case "remove": - return (int) !Arsse::$user->remove($args[""]); - case "show": - return $this->userShowProperties($args[""]); - case "set": - return (int) !Arsse::$user->propertiesSet($args[""], [$args[""] => $args[""]]); - case "unset": - return (int) !Arsse::$user->propertiesSet($args[""], [$args[""] => null]); - case "auth": - return $this->userAuthenticate($args[""], $args[""], $args["--fever"]); - case "list": - case "": - return $this->userList(); - default: - throw new Exception("constantUnknown", $cmd); // @codeCoverageIgnore - } - } - protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int { $passwd = Arsse::$user->$method(...array_slice(func_get_args(), 1)); if (is_null($password)) {