SF.net SVN: postfixadmin:[1575] trunk
Brought to you by:
christian_boltz,
gingerdog
From: <chr...@us...> - 2013-11-13 19:07:15
|
Revision: 1575 http://sourceforge.net/p/postfixadmin/code/1575 Author: christian_boltz Date: 2013-11-13 19:07:12 +0000 (Wed, 13 Nov 2013) Log Message: ----------- 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()) Modified Paths: -------------- trunk/model/PFAHandler.php trunk/scripts/postfixadmin-cli.php trunk/scripts/shells/mailbox.php trunk/scripts/shells/shell.php Added Paths: ----------- trunk/model/CliHelp.php Removed Paths: ------------- trunk/scripts/shells/alias.php trunk/scripts/shells/domain.php Added: trunk/model/CliHelp.php =================================================================== --- trunk/model/CliHelp.php (rev 0) +++ trunk/model/CliHelp.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -0,0 +1,65 @@ +<?php + +class CliHelp extends Shell { + + public $handler_to_use = "__not_set__"; + +/** + * Show help for this shell. + * + * @access public + */ + public function execute() { + $this->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 <task> [<address>] [--option value] +"); +/* + View $module in interactive mode. + +- or - + + postfixadmin-cli $module view <address> + + View $module <address> in non-interactive mode. +"); */ + + + + $head = "Usage: postfixadmin-cli $module <task> [<address>] [--option value] [--option value]\n"; + $head .= "-----------------------------------------------\n"; + $head .= "Parameters:\n\n"; + + $commands = array( + 'task' => "\t<task>\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[<address>]\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: */ Property changes on: trunk/model/CliHelp.php ___________________________________________________________________ Added: svn:keywords ## -0,0 +1 ## +Id \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/model/PFAHandler.php =================================================================== --- trunk/model/PFAHandler.php 2013-11-13 00:40:08 UTC (rev 1574) +++ trunk/model/PFAHandler.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -11,6 +11,9 @@ # 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 */ Modified: trunk/scripts/postfixadmin-cli.php =================================================================== --- trunk/scripts/postfixadmin-cli.php 2013-11-13 00:40:08 UTC (rev 1574) +++ trunk/scripts/postfixadmin-cli.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -227,82 +227,82 @@ */ 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(); - } - } + $handler = new $shell->handler_to_use; + if (in_array($task, $handler->taskNames)) { + $this->shiftArgs(); + $shell->startup(); - $task = Inflector::camelize($command); - if (in_array($task, $shell->taskNames)) { - $this->shiftArgs(); - $shell->{$task}->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 @@ } 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(); - } } /** Deleted: trunk/scripts/shells/alias.php =================================================================== --- trunk/scripts/shells/alias.php 2013-11-13 00:40:08 UTC (rev 1574) +++ trunk/scripts/shells/alias.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -1,54 +0,0 @@ -<?php - - -class PostfixAdminAlias extends Shell { - -/** - * Contains tasks to load and instantiate - * - * @var array - * @access public - */ - var $tasks = array('Add', 'Update', 'Delete', 'View'); - - - - - -/** - * Show help for this shell. - * - * @access public - */ - function help() { - $head = "Usage: postfixadmin-cli alias <task> [<address>] [] [-m <method>]\n"; - $head .= "-----------------------------------------------\n"; - $head .= "Parameters:\n\n"; - - $commands = array( - 'task' => "\t<task>\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[<address>]\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: */ Deleted: trunk/scripts/shells/domain.php =================================================================== --- trunk/scripts/shells/domain.php 2013-11-13 00:40:08 UTC (rev 1574) +++ trunk/scripts/shells/domain.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -1,66 +0,0 @@ -<?php - - -class PostfixAdminDomain extends Shell { - -/** - * Contains tasks to load and instantiate - * - * @var array - * @access public - */ - var $tasks = array('Add', 'Update', 'Delete', 'View'); - - - - - -/** - * Show help for this shell. - * - * @access public - */ - function help() { - $head = "Usage: postfixadmin-cli domain <task> [<domain>] [-desc \"<description>\"] [-a <aliases>] [-m <mailboxes>] [-q <quota in MB>] [-t <transport>] [-default] [-backup]\n"; - $head .= "-----------------------------------------------\n"; - $head .= "Parameters:\n\n"; - - $commands = array( - 'task' => "\t<task>\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[<domain>]\n" . - "\t\tA address of recipient.\n", - 'a' => "\t[<aliaes>]\n" . - "\t\tNumber of max aliases. -1 = disable | 0 = unlimited\n", - 'm' => "\t[<mailboxes>]\n" . - "\t\tNumber of max mailboxes. -1 = disable | 0 = unlimited\n", - 'q' => "\t[<quota in MB>]\n" . - "\t\tMax Quota in MB. -1 = disable | 0 = unlimited\n", - 'd' => "\t[<domain quota in MB>]\n" . - "\t\tDomain Quota in MB. -1 = disable | 0 = unlimited\n", - 't' => "\t[<transport>]\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: */ Modified: trunk/scripts/shells/mailbox.php =================================================================== --- trunk/scripts/shells/mailbox.php 2013-11-13 00:40:08 UTC (rev 1574) +++ trunk/scripts/shells/mailbox.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -1,56 +1,6 @@ <?php -class PostfixAdminMailbox extends Shell { - -/** - * Contains tasks to load and instantiate - * - * @var array - * @access public - */ - var $tasks = array('Add', 'Update', 'Delete', 'Password', 'View'); - - - - -/** - * Show help for this shell. - * - * @access public - */ - function help() { - $head = "Usage: postfixadmin-cli mailbox <task> [<address>] [] [--<option> <value>]\n"; - $head .= "-----------------------------------------------\n"; - $head .= "Parameters:\n\n"; - - $commands = array( - 'task' => "\t<task>\n" . - "\t\tAvailable values:\n\n". - "\t\t".sprintf("%-20s %s", "view: ", "View an existing mailbox.")."\n". - "\t\t".sprintf("%-20s %s", "add: ", "Adds a new mailbox.")."\n". - "\t\t".sprintf("%-20s %s", "update: ", "Updates a mailbox.")."\n". - "\t\t".sprintf("%-20s %s", "delete: ", "Deletes a mailbox")."\n". - "\t\t".sprintf("%-20s %s", "password: ", "Changes the PW for a mailbox.")."\n", - 'address' => "\t[<address>]\n" . - "\t\tMail address\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"); - } - } - - -} - class PasswordTask extends Shell { /** * Execution method always used for tasks Modified: trunk/scripts/shells/shell.php =================================================================== --- trunk/scripts/shells/shell.php 2013-11-13 00:40:08 UTC (rev 1574) +++ trunk/scripts/shells/shell.php 2013-11-13 19:07:12 UTC (rev 1575) @@ -102,13 +102,6 @@ */ var $tasks = array(); /** - * Contains the loaded tasks - * - * @var array - * @access public - */ - var $taskNames = array(); -/** * Contains models to load and instantiate * * @var array @@ -184,71 +177,7 @@ $this->hr(); } - /** - * Loads tasks defined in var $tasks - * - * @return bool - * @access public - */ - function loadTasks() { - if ($this->tasks === null || $this->tasks === false) { - return; - } - - if ($this->tasks !== true && !empty($this->tasks)) { - - $tasks = $this->tasks; - if (!is_array($tasks)) { - $tasks = array($tasks); - } - - foreach ($tasks as $taskName) { - $task = Inflector::underscore($taskName); - $taskClass = Inflector::camelize($taskName.'Task'); - $taskKey = Inflector::underscore($taskClass); - - if ($taskName == 'Add' || $taskName == 'Update') { - $taskClass = 'CliEdit'; - } elseif ($taskName == 'Delete') { - $taskClass = 'CliDelete'; - } elseif ($taskName == 'View') { - $taskClass = 'CliView'; - } - -# elseif (!class_exists($taskClass)) { -# foreach ($this->Dispatch->shellPaths as $path) { -# $taskPath = $path . 'tasks' . DS . $task.'.php'; -# if (file_exists($taskPath)) { -# require_once $taskPath; -# break; -# } -# } -# } - - $this->taskNames[] = $taskName; - $this->{$taskName} = new $taskClass($this->Dispatch); - - if ($taskName == 'Add') { - $this->{$taskName}->handler_to_use = ucfirst($this->shell) . 'Handler'; - $this->{$taskName}->new = 1; - } elseif ($taskName == 'Update') { - $this->{$taskName}->handler_to_use = ucfirst($this->shell) . 'Handler'; - $this->{$taskName}->new = 0; - } elseif ($taskName == 'Delete' || $taskName == 'View') { - $this->{$taskName}->handler_to_use = ucfirst($this->shell) . 'Handler'; - $this->{$taskName}->new = 0; - } - - if (!isset($this->{$taskName})) { - $this->err("Task '".$taskName."' could not be loaded"); - $this->_stop(); - } - } - } - - return false; - } - /** +/** * Prompts the user for input, and returns it. * * @param string $prompt Prompt text. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |