Improvements to CLI and backup & restore.

This commit is contained in:
Alexandre Rosenfeld
2011-02-17 20:51:26 +00:00
parent 3f22dba6ee
commit 252881ae2e
3 changed files with 226 additions and 62 deletions

View File

@@ -68,6 +68,10 @@ EOT
);
CLI::taskArg('backup-file', false);
CLI::taskArg('workspace', true);
CLI::taskOpt("overwrite", "If a workspace already exists, overwrite it.", "o", "overwrite");
CLI::taskOpt("info", "Only shows information about a backup archive.", "i");
CLI::taskOpt("workspace", "Select which workspace to restore if multiple workspaces are present in the archive.",
"w", "workspace");
CLI::taskRun(run_workspace_restore);
CLI::taskName('cacheview-repair');
@@ -357,19 +361,53 @@ function run_workspace_backup($args, $opts) {
$workspaces[] = new workspaceTools($arg);
}
} else if (sizeof($args) > 0) {
$workspaces[] = new workspaceTools($args[0]);
$workspace = new workspaceTools($args[0]);
$workspaces[] = $workspace;
if (sizeof($args) == 2)
$filename = $args[1];
else
$filename = $workspace->name . ".tar.gz";
$filename = "{$workspace->name}.tar";
} else {
throw new Exception("No workspace specified for backup");
}
foreach ($workspaces as $workspace)
$workspace->backup($filename);
if (!$workspace->workspaceExists())
throw new Exception("Workspace '{$workspace->name}' not found");
//If this is a relative path, put the file in the backups directory
if (strpos($filename, "/") === false && strpos($filename, '\\') === false)
$filename = PATH_DATA . "backups/$filename";
CLI::logging("Backing up to $filename\n");
$backup = workspaceTools::createBackup($filename);
foreach ($workspaces as $workspace)
$workspace->backup($backup);
CLI::logging("\n");
workspaceTools::printSysInfo();
foreach ($workspaces as $workspace) {
CLI::logging("\n");
$workspace->printMetadata(false);
}
}
function run_workspace_restore($args, $opts) {
//$workspace = new workspaceTools($args[0]);
workspaceTools::restore($args[0], $args[1]);
$filename = $args[0];
if (strpos($filename, "/") === false && strpos($filename, '\\') === false) {
$filename = PATH_DATA . "backups/$filename";
if (substr_compare($filename, ".tar", -4, 4, true) != 0)
$filename .= ".tar";
}
$info = array_key_exists("info", $opts);
if ($info) {
workspaceTools::getBackupInfo($filename);
} else {
CLI::logging("Restoring from $filename\n");
$workspace = array_key_exists("workspace", $opts) ? $opts['workspace'] : NULL;
$overwrite = array_key_exists("overwrite", $opts);
$dstWorkspace = $args[1];
workspaceTools::restore($filename, $workspace, $dstWorkspace, $overwrite);
}
}
?>

View File

@@ -45,7 +45,7 @@ class CLI {
'description' => NULL,
'args' => array(),
'function' => NULL,
'opt' => array('short' => '', 'long' => array())
'opt' => array('short' => '', 'long' => array(), 'descriptions' => array())
);
}
@@ -83,9 +83,16 @@ class CLI {
* @param string $short short options
* @param array $long long options
*/
public static function taskOpt($short, $long = array()) {
public static function taskOpt($name, $description, $short, $long = NULL) {
assert(self::$currentTask !== NULL);
self::$tasks[self::$currentTask]["opt"] = array('short' => $short, 'long' => $long);
$opts = self::$tasks[self::$currentTask]["opt"];
if ($short)
$opts['short'] .= $short;
if ($long)
$opts['long'][] = $long;
$opts['descriptions'][$name] = array('short' => $short, 'long' => $long,
'description' => $description);
self::$tasks[self::$currentTask]["opt"] = $opts;
}
/**
@@ -104,10 +111,13 @@ class CLI {
* @param array $args if defined, the task name should be argument 0
* @param array $opts options as returned by getopt
*/
public static function help($args, $opts) {
public static function help($args, $opts = NULL) {
global $argv;
$scriptName = $argv[0];
$taskName = $args[0];
if (is_array($args))
$taskName = $args[0];
else
$taskName = $args;
if (!$taskName) {
echo "usage: $scriptName <task> [options] [args]\n";
@@ -142,9 +152,21 @@ Usage: $scriptName $taskName $valid_args
$description
EOT;
$valid_options = array();
foreach(self::$tasks[$taskName]['opt']['descriptions'] as $opt => $data) {
$optString = array();
if ($data['short'])
$optString[] = "-{$data['short']}";
if ($data['long'])
$optString[] = "--{$data['long']}";
$valid_options[] = " " . join(", ", $optString) . "\n\t" . wordwrap($data['description'], 70, "\n\t");
}
$valid_options = join("\n", $valid_options);
if ($valid_options) {
$message .= <<< EOT
Options:
$valid_options
EOT;
@@ -185,9 +207,40 @@ EOT;
G::LoadThirdParty('pear/Console', 'Getopt');
$short = "h" . $taskData['opt']['short'];
$long = array_merge(array("help"), $taskData['opt']['long']);
list($options, $arguments) = Console_GetOpt::getopt2($args, $short, $long);
$getopt = Console_GetOpt::getopt2($args, $short, $long);
if (!is_array($getopt)) {
echo self::error("Invalid options (" . $getopt->getMessage() . ")") . "\n\n";
self::help($taskName);
return;
}
list($options, $arguments) = $getopt;
foreach ($taskData['opt']['descriptions'] as $optName => $optDescription) {
$validOpts[$optDescription['short']] = $optName;
$validOpts[$optDescription['long']] = $optName;
}
$taskOpts = array();
try {
call_user_func($taskData['function'], $arguments, $options);
foreach ($options as $opt) {
list($optName, $optArg) = $opt;
if ($optName === "h" || $optName === "--help") {
self::help($taskName);
return;
}
if (strpos($optName, '--') === 0)
$optName = substr($optName, 2);
if (!array_key_exists($optName, $validOpts))
throw new Exception("Invalid option: $optName");
if (array_key_exists($validOpts[$optName], $taskOpts))
throw new Exception("'$optName' specified more then once");
$taskOpts[$validOpts[$optName]] = $optArg;
}
} catch (Exception $e) {
echo self::error("Invalid options: " . $e->getMessage()) . "\n\n";
self::help($taskName);
return;
}
try {
call_user_func($taskData['function'], $arguments, $taskOpts);
} catch (Exception $e) {
echo self::error("\n Error executing '$taskName':\n\n {$e->getMessage()}\n") . "\n";
}

File diff suppressed because it is too large Load Diff