From c65e3293b66cfb24e752db967a56d2a71ccca514 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Wed, 13 Nov 2013 19:07:12 +0000 Subject: [PATCH] Quite big CLI cleanup (and more) model/CliHelp.php: - new class, used for "postfixadmin-cli $module help" - replaces the PostfixAdmin* classes in scripts/shells/*.php model/PFAHandler.php - add public $taskNames with the list of supported CLI commands scripts/postfixadmin-cli.php - dispatch(): - directly set the classes to load instead of using shell->loadTasks() - when "postfixadmin-cli $module" is called, display help instead of error message about "invalid command ''" - make it more readable by moving error checks to the beginning (replaces deeply nested if's with return statements) - remove unused code parts scripts/shells/*.php: - remove PostfixAdmin* classes (obsoleted by CliHelp) - remove $tasks (now in PFAHandler) - delete alias.php and domain.php because nothing is left - mailbox.php still contains PasswordTask scripts/shells/shell.php: - remove $taskNames (moved to PFAHandler) - remove loadTasks() (integrated in postfixadmin-cli.php's dispatch()) git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1575 a1433add-5e2c-0410-b055-b7f2511e0802 --- model/CliHelp.php | 65 ++++++++++++++++++++ model/PFAHandler.php | 3 + scripts/postfixadmin-cli.php | 112 ++++++++++++++++------------------- scripts/shells/alias.php | 54 ----------------- scripts/shells/domain.php | 66 --------------------- scripts/shells/mailbox.php | 50 ---------------- scripts/shells/shell.php | 73 +---------------------- 7 files changed, 120 insertions(+), 303 deletions(-) create mode 100644 model/CliHelp.php delete mode 100644 scripts/shells/alias.php delete mode 100644 scripts/shells/domain.php diff --git a/model/CliHelp.php b/model/CliHelp.php new file mode 100644 index 00000000..17ee8201 --- /dev/null +++ b/model/CliHelp.php @@ -0,0 +1,65 @@ +help(); + } + + public function help() { + + $handler = new $this->handler_to_use; + # TODO: adjust help text according to $handler->taskNames + + $module = preg_replace('/Handler$/', '', $this->handler_to_use); + $module = strtolower($module); + + $this->out( +"Usage: + + postfixadmin-cli $module [
] [--option value] +"); +/* + View $module in interactive mode. + +- or - + + postfixadmin-cli $module view
+ + View $module
in non-interactive mode. +"); */ + + + + $head = "Usage: postfixadmin-cli $module [
] [--option value] [--option value]\n"; + $head .= "-----------------------------------------------\n"; + $head .= "Parameters:\n\n"; + + $commands = array( + 'task' => "\t\n" . + "\t\tAvailable values:\n\n". + "\t\t".sprintf("%-20s %s", "view: ", "View an existing $module.")."\n". + "\t\t".sprintf("%-20s %s", "add: ", "Add a $module.")."\n". + "\t\t".sprintf("%-20s %s", "update: ", "Update a $module.")."\n". + "\t\t".sprintf("%-20s %s", "delete: ", "Delete a $module")."\n". + "\t\t".sprintf("%-20s %s", "password: ", "Changes the password for a $module")."\n", + 'address' => "\t[
]\n" . + "\t\tA address of recipient.\n", + ); + + foreach ($commands as $cmd) { + $this->out("{$cmd}\n\n"); + } + } + + +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/model/PFAHandler.php b/model/PFAHandler.php index a3dd1bd8..500efe32 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -11,6 +11,9 @@ abstract class PFAHandler { # array of info messages (for example success messages) public $infomsg = array(); + # array of tasks available in CLI + public $taskNames = array('Help', 'Add', 'Update', 'Delete', 'View'); + /** * variables that must be defined in all *Handler classes */ diff --git a/scripts/postfixadmin-cli.php b/scripts/postfixadmin-cli.php index 82b0d31c..81d7495e 100644 --- a/scripts/postfixadmin-cli.php +++ b/scripts/postfixadmin-cli.php @@ -227,82 +227,82 @@ class PostfixAdmin { */ function dispatch() { $CONF = Config::read('all'); - if (isset($this->args[0])) { - $plugin = null; - $shell = $this->args[0]; - if (strpos($shell, '.') !== false) { - list($plugin, $shell) = explode('.', $this->args[0]); - } - $this->shell = $shell; + if (!isset($this->args[0])) { + $this->help(); + return; + } + + $this->shell = $this->args[0]; $this->shiftArgs(); $this->shellName = Inflector::camelize($this->shell); - $this->shellClass = 'PostfixAdmin'.$this->shellName; + $this->shellClass = $this->shellName . 'Handler'; if ($this->shell == 'help') { $this->help(); - } else { - $loaded = false; - $paths = array(); - - if ($plugin !== null) { - $pluginPaths = Config::read('pluginPaths'); - $count = count($pluginPaths); - for ($i = 0; $i < $count; $i++) { - $paths[] = $pluginPaths[$i] . $plugin . DS . 'vendors' . DS . 'shells' . DS; - } - } - - - $paths[] = CORE_INCLUDE_PATH . DS . "shells" . DS; - - $this->shellPaths = $paths; - foreach ($this->shellPaths as $path) { - $this->shellPath = $path . $this->shell . ".php"; - if (file_exists($this->shellPath)) { - $loaded = true; - break; - } - } - if ($loaded) { + return; + } +# TODO: move shells/shell.php to model/ to enable autoloading if (!class_exists('Shell')) { require CORE_INCLUDE_PATH . DS . "shells" . DS . 'shell.php'; } - - require $this->shellPath; - if (class_exists($this->shellClass)) { - $command = null; + $command = 'help'; # not the worst default ;-) if (isset($this->args[0])) { $command = $this->args[0]; } + $this->shellCommand = $command; - $shell = new $this->shellClass($this); + + + $this->shellClass = 'Cli' . Inflector::camelize($command); + + if (ucfirst($command) == 'Add' || ucfirst($command) == 'Update') { + $this->shellClass = 'CliEdit'; + } + + if (!class_exists($this->shellClass)) { + $this->stderr('Class '.$this->shellClass.' could not be loaded'); + return; + } + + $shell = new $this->shellClass($this); + + $shell->handler_to_use = ucfirst($this->shell) . 'Handler'; + + if (!class_exists($shell->handler_to_use)) { + $this->stderr('Class '.$shell->handler_to_use.' could not be loaded'); + return; + } + + $task = Inflector::camelize($command); + + $shell->new = 0; + if ($task == 'Add') { + $shell->new = 1; + } + +# TODO: add a way to Cli* to signal if the selected handler is supported (for example, not all *Handler support changing the password) if (strtolower(get_parent_class($shell)) == 'shell') { $shell->initialize(); - $shell->loadTasks(); - foreach ($shell->taskNames as $task) { - if (strtolower(get_parent_class($shell)) == 'shell') { - $shell->{$task}->initialize(); - $shell->{$task}->loadTasks(); - } - } - - $task = Inflector::camelize($command); - if (in_array($task, $shell->taskNames)) { + $handler = new $shell->handler_to_use; + if (in_array($task, $handler->taskNames)) { $this->shiftArgs(); - $shell->{$task}->startup(); + $shell->startup(); + + if (isset($this->args[0]) && $this->args[0] == 'help') { - if (method_exists($shell->{$task}, 'help')) { - $shell->{$task}->help(); + if (method_exists($shell, 'help')) { + $shell->help(); exit(); } else { $this->help(); } } - $shell->{$task}->execute(); + + $shell->execute(); return; } } @@ -340,16 +340,6 @@ class PostfixAdmin { } else { $this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n"); } - } else { - $this->stderr('Class '.$this->shellClass.' could not be loaded'); - } - } else { - $this->help(); - } - } - } else { - $this->help(); - } } /** diff --git a/scripts/shells/alias.php b/scripts/shells/alias.php deleted file mode 100644 index bca603e8..00000000 --- a/scripts/shells/alias.php +++ /dev/null @@ -1,54 +0,0 @@ - [
] [] [-m ]\n"; - $head .= "-----------------------------------------------\n"; - $head .= "Parameters:\n\n"; - - $commands = array( - 'task' => "\t\n" . - "\t\tAvailable values:\n\n". - "\t\t".sprintf("%-20s %s", "view: ", "View an existing alias.")."\n". - "\t\t".sprintf("%-20s %s", "add: ", "Adds an alias.")."\n". - "\t\t".sprintf("%-20s %s", "update: ", "Updates an alias.")."\n". - "\t\t".sprintf("%-20s %s", "delete: ", "Deletes an alias")."\n", - 'address' => "\t[
]\n" . - "\t\tA address of recipient.\n", - ); - - $this->out($head); - if (!isset($this->args[1])) { - foreach ($commands as $cmd) { - $this->out("{$cmd}\n\n"); - } - } elseif (isset($commands[strtolower($this->args[1])])) { - $this->out($commands[strtolower($this->args[1])] . "\n\n"); - } else { - $this->out("Command '" . $this->args[1] . "' not found"); - } - } - - -} - -/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/scripts/shells/domain.php b/scripts/shells/domain.php deleted file mode 100644 index e3f439b9..00000000 --- a/scripts/shells/domain.php +++ /dev/null @@ -1,66 +0,0 @@ - [] [-desc \"\"] [-a ] [-m ] [-q ] [-t ] [-default] [-backup]\n"; - $head .= "-----------------------------------------------\n"; - $head .= "Parameters:\n\n"; - - $commands = array( - 'task' => "\t\n" . - "\t\tAvailable values:\n\n". - "\t\t".sprintf("%-20s %s", "view: ", "View an existing domain.")."\n". - "\t\t".sprintf("%-20s %s", "add: ", "Adds a domain.")."\n". - "\t\t".sprintf("%-20s %s", "update: ", "Updates an domain.")."\n". - "\t\t".sprintf("%-20s %s", "delete: ", "Deletes a domain")."\n", - 'domain' => "\t[]\n" . - "\t\tA address of recipient.\n", - 'a' => "\t[]\n" . - "\t\tNumber of max aliases. -1 = disable | 0 = unlimited\n", - 'm' => "\t[]\n" . - "\t\tNumber of max mailboxes. -1 = disable | 0 = unlimited\n", - 'q' => "\t[]\n" . - "\t\tMax Quota in MB. -1 = disable | 0 = unlimited\n", - 'd' => "\t[]\n" . - "\t\tDomain Quota in MB. -1 = disable | 0 = unlimited\n", - 't' => "\t[]\n" . - "\t\tTransport options from config.inc.php.\n", - 'default' => "\t\tSet to add default Aliases.\n", - 'backup' => "\t\tSet if mailserver is backup MX.\n", - ); - - $this->out($head); - if (!isset($this->args[1])) { - foreach ($commands as $cmd) { - $this->out("{$cmd}\n\n"); - } - } elseif (isset($commands[strtolower($this->args[1])])) { - $this->out($commands[strtolower($this->args[1])] . "\n\n"); - } else { - $this->out("Command '" . $this->args[1] . "' not found"); - } - } - - -} - -/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ diff --git a/scripts/shells/mailbox.php b/scripts/shells/mailbox.php index 0cc431e5..06c9d93a 100644 --- a/scripts/shells/mailbox.php +++ b/scripts/shells/mailbox.php @@ -1,56 +1,6 @@ [
] [] [--