From: Jan Kupec Date: Wed, 13 Jun 2007 07:00:24 +0000 (+0000) Subject: command enumeration/mapping class added X-Git-Tag: BASE-SuSE-Linux-10_3-Branch~329 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=59a6fc8b6001e54853be638dc39b5806ba8e8326;p=platform%2Fupstream%2Fzypper.git command enumeration/mapping class added --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 176c08b..af77002 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ ADD_DEFINITIONS ( SET( zypper_SRCS zypper.cc + zypper-command.cc zypper-getopt.cc zypper-sources.cc zypper-misc.cc @@ -17,6 +18,7 @@ SET( zypper_SRCS SET (zypper_HEADERS zypper.h + zypper-command.h zypper-getopt.h zypper-sources.h zypper-misc.h diff --git a/src/zypper-command.cc b/src/zypper-command.cc new file mode 100644 index 0000000..ad9b1e8 --- /dev/null +++ b/src/zypper-command.cc @@ -0,0 +1,57 @@ +#include + +#include + +#include +#include + +#include "zypper-command.h" + +// redefine _ gettext macro defined by ZYpp +#ifdef _ +#undef _ +#endif +#define _(MSG) ::gettext(MSG) + + +static std::map _table; + +const ZypperCommand ZypperCommand::ADD_REPO(ZypperCommand::ADD_REPO_e); +const ZypperCommand ZypperCommand::REMOVE_REPO(ZypperCommand::REMOVE_REPO_e); +const ZypperCommand ZypperCommand::LIST_REPOS(ZypperCommand::LIST_REPOS_e); +const ZypperCommand ZypperCommand::REFRESH(ZypperCommand::REFRESH_e); +const ZypperCommand ZypperCommand::SEARCH(ZypperCommand::SEARCH_e); +const ZypperCommand ZypperCommand::INSTALL(ZypperCommand::INSTALL_e); +const ZypperCommand ZypperCommand::REMOVE(ZypperCommand::REMOVE_e); +const ZypperCommand ZypperCommand::UPDATE(ZypperCommand::UPDATE_e); + +ZypperCommand::ZypperCommand(const std::string & strval_r) + : _command(parse(strval_r)) +{} + +ZypperCommand::Command ZypperCommand::parse(const std::string & strval_r) +{ + if (_table.empty()) + { + // initialize it + _table["addrepo"] = _table["service-add"] = _table["sa"] = ZypperCommand::ADD_REPO_e; + _table["rmrepo"] = _table["service-delete"] = _table["sd"] = ZypperCommand::REMOVE_REPO_e; + _table["listrepos"] = _table["service-list"] = _table["sl"] = ZypperCommand::LIST_REPOS_e; + _table["refresh"] = _table["ref"] = ZypperCommand::REFRESH_e; + _table["search"] = _table["se"] = ZypperCommand::SEARCH_e; + _table["install"] = _table["in"] = ZypperCommand::INSTALL_e; + _table["remove"] = _table["rm"] = ZypperCommand::REMOVE_e; + _table["update"] = _table["up"] = ZypperCommand::UPDATE_e; + _table["NONE"] = _table["none"] = ZypperCommand::NONE_e; + } + + std::map::const_iterator it + = _table.find(strval_r); + if (it == _table.end()) + { + std::string message = + boost::str( boost::format(_("Unknown command '%s'")) % strval_r ); + ZYPP_THROW(zypp::Exception(message)); + } + return it->second; +} diff --git a/src/zypper-command.h b/src/zypper-command.h new file mode 100644 index 0000000..76d485f --- /dev/null +++ b/src/zypper-command.h @@ -0,0 +1,56 @@ +#ifndef ZYPPERCOMMAND_H_ +#define ZYPPERCOMMAND_H_ + +#include +#include + +/** + * Enumeration of zypper commands with mapping of command aliases. + * The mapping includes rug equivalents as well. + */ +struct ZypperCommand +{ + static const ZypperCommand ADD_REPO; + static const ZypperCommand REMOVE_REPO; + static const ZypperCommand LIST_REPOS; + static const ZypperCommand REFRESH; + static const ZypperCommand SEARCH; + static const ZypperCommand INSTALL; + static const ZypperCommand REMOVE; + static const ZypperCommand UPDATE; + + enum Command + { + NONE_e, + ADD_REPO_e, + REMOVE_REPO_e, + LIST_REPOS_e, + REFRESH_e, + SEARCH_e, + INSTALL_e, + REMOVE_e, + UPDATE_e, + }; + + ZypperCommand(Command command) : _command(command) {} + + explicit ZypperCommand(const std::string & strval_r); + + const Command toEnum() const { return _command; } + + ZypperCommand::Command parse(const std::string & strval_r); + + const std::string & asString() const; + + + Command _command; +}; + +inline std::ostream & operator<<( std::ostream & str, const ZypperCommand & obj ) +{ return str << obj.asString(); } + +inline bool operator==(const ZypperCommand & obj1, const ZypperCommand & obj2) +{ return obj1._command == obj2._command; } + + +#endif /*ZYPPERCOMMAND_H_*/ diff --git a/src/zypper.cc b/src/zypper.cc index 6eb2f55..e1e184a 100644 --- a/src/zypper.cc +++ b/src/zypper.cc @@ -32,6 +32,7 @@ #include "zypper-search.h" #include "zypper-info.h" #include "zypper-getopt.h" +#include "zypper-command.h" using namespace std; //using namespace boost; @@ -207,10 +208,12 @@ string process_globals(int argc, char **argv) } /// process one command from the OS shell or the zypper shell -int one_command(const string& command, int argc, char **argv) +int one_command(const string& command_str, int argc, char **argv) { // === command-specific options === + ZypperCommand command(command_str); + struct option no_options = {0, 0, 0, 0}; struct option *specific_options = &no_options; string specific_help; @@ -225,7 +228,7 @@ int one_command(const string& command, int argc, char **argv) "\t--disable-system-resolvables, -T\t\tDo not read system installed resolvables\n" ); - if (command == "install" || command == "in") { + if (command.toEnum() == ZypperCommand::INSTALL_e) { static struct option install_options[] = { {"catalog", required_argument, 0, 'c'}, {"type", required_argument, 0, 't'}, @@ -241,7 +244,7 @@ int one_command(const string& command, int argc, char **argv) "\t--no-confirm,-y\tDo not require user confirmation\n" ); } - else if (command == "remove" || command == "rm") { + else if (command.toEnum() == ZypperCommand::REMOVE_e) { static struct option remove_options[] = { {"type", required_argument, 0, 't'}, {"no-confirm", no_argument, 0, 'y'}, @@ -255,7 +258,7 @@ int one_command(const string& command, int argc, char **argv) "\t--no-confirm,-y\tDo not require user confirmation\n" ); } - else if (command == "service-add" || command == "sa") { + else if (command.toEnum() == ZypperCommand::ADD_REPO_e) { static struct option service_add_options[] = { {"type", required_argument, 0, 't'}, {"disabled", no_argument, 0, 'd'}, @@ -278,7 +281,7 @@ int one_command(const string& command, int argc, char **argv) "\t--no-refresh,-n\t\tDo not automatically refresh the metadata\n" ); } - else if (command == "service-list" || command == "sl") { + else if (command.toEnum() == ZypperCommand::LIST_REPOS_e) { static struct option service_list_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -292,7 +295,7 @@ int one_command(const string& command, int argc, char **argv) "This command has no options.\n" ); } - else if (command == "service-delete" || command == "sd") { + else if (command.toEnum() == ZypperCommand::REMOVE_REPO_e) { static struct option service_delete_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -306,7 +309,7 @@ int one_command(const string& command, int argc, char **argv) "This command has no options.\n" ); } - else if (command == "service-rename" || command == "sr") { + else if (command_str == "service-rename" || command_str == "sr") { static struct option service_rename_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -320,7 +323,7 @@ int one_command(const string& command, int argc, char **argv) "This command has no options.\n" ); } - else if (command == "refresh" || command == "ref") { + else if (command.toEnum() == ZypperCommand::REFRESH_e) { static struct option refresh_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -332,7 +335,7 @@ int one_command(const string& command, int argc, char **argv) "Refresh all installation sources found in the system.\n" ); } - else if (command == "list-updates" || command == "lu") { + else if (command_str == "list-updates" || command_str == "lu") { static struct option list_updates_options[] = { {"type", required_argument, 0, 't'}, {"help", no_argument, 0, 'h'}, @@ -348,7 +351,7 @@ int one_command(const string& command, int argc, char **argv) "\t--type,-t\t\tType of resolvable (default: patch)\n" ); } - else if (command == "update" || command == "up") { + else if (command_str == "update" || command_str == "up") { static struct option update_options[] = { {"type", required_argument, 0, 't'}, {"no-confirm", no_argument, 0, 'y'}, @@ -364,7 +367,7 @@ int one_command(const string& command, int argc, char **argv) "\t--skip-interactive\t\tSkip interactive updates\n" ); } - else if (command == "search" || command == "se") { + else if (command_str == "search" || command_str == "se") { static struct option search_options[] = { {"installed-only", no_argument, 0, 'i'}, {"uninstalled-only", no_argument, 0, 'u'}, @@ -404,7 +407,7 @@ int one_command(const string& command, int argc, char **argv) "* and ? wildcards can also be used within search strings.\n" ); } - else if (command == "patch-check" || command == "pchk") { + else if (command_str == "patch-check" || command_str == "pchk") { static struct option patch_check_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -418,7 +421,7 @@ int one_command(const string& command, int argc, char **argv) "This command has no options.\n" ); } - else if (command == "patches" || command == "pch") { + else if (command_str == "patches" || command_str == "pch") { static struct option patches_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -432,7 +435,7 @@ int one_command(const string& command, int argc, char **argv) "This command has no options.\n" ); } - else if (command == "info" || command == "if") { + else if (command_str == "info" || command_str == "if") { static struct option info_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -444,7 +447,7 @@ int one_command(const string& command, int argc, char **argv) "'info' -- Show full information for packages\n" ); } - else if (command == "patch-info") { + else if (command_str == "patch-info") { static struct option patch_info_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -456,7 +459,7 @@ int one_command(const string& command, int argc, char **argv) "'patch-info' -- Show detailed information for patches\n" ); } - else if (command == "moo") { + else if (command_str == "moo") { static struct option moo_options[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} @@ -468,9 +471,9 @@ int one_command(const string& command, int argc, char **argv) "'moo' - Show an animal\n" ); } - else if (!command.empty()) { // empty command is treated earlier - if (command != "help") // #235709 - cerr << _("Unknown command") << " '" << command << "'." << endl << endl; + else if (!command_str.empty()) { // empty command is treated earlier + if (command_str != "help") // #235709 + cerr << _("Unknown command") << " '" << command_str << "'." << endl << endl; cerr << help_commands; return ZYPPER_EXIT_ERR_SYNTAX; } @@ -530,7 +533,7 @@ int one_command(const string& command, int argc, char **argv) // here come commands that need the lock try { - if (command == "service-list" || command == "sl") + if (command_str == "service-list" || command_str == "sl") zypp_readonly_hack::IWantIt (); // #247001 God = zypp::getZYpp(); @@ -549,14 +552,14 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( moo )---------------------------------------- - if (command == "moo") { + if (command_str == "moo") { cout << " \\\\\\\\\\\n \\\\\\\\\\\\\\__o\n__\\\\\\\\\\\\\\'/_" << endl; return ZYPPER_EXIT_OK; } // --------------------------( service list )------------------------------- - else if (command == "service-list" || command == "sl") + else if (command.toEnum() == ZypperCommand::LIST_REPOS_e) { if (ghelp) { cerr << specific_help << endl; @@ -575,7 +578,7 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( service add )-------------------------------- - else if (command == "service-add" || command == "sa") + else if (command.toEnum() == ZypperCommand::ADD_REPO_e) { // TODO: repect values in .repo, have these as overrides bool enabled = ! copts.count("disabled"); @@ -633,7 +636,7 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( service delete )----------------------------- - else if (command == "service-delete" || command == "sd") + else if (command.toEnum() == ZypperCommand::REMOVE_REPO_e) { if (ghelp || arguments.size() < 1) { cerr << specific_help; @@ -657,7 +660,7 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( service rename )----------------------------- - else if (command == "service-rename" || command == "sr") + else if (command_str == "service-rename" || command_str == "sr") { if (ghelp || arguments.size() < 2) { cerr << specific_help; @@ -681,21 +684,19 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( refresh )------------------------------------ - else if (command == "refresh" || command == "ref") { - if (ghelp) { - cerr << specific_help; - return !ghelp; - } - + else if (command.toEnum() == ZypperCommand::REFRESH_e) + { + if (ghelp) { cout << specific_help; return !ghelp; } + refresh_sources(); } // --------------------------( remove/install )----------------------------- - else if (command == "install" || command == "in" || - command == "remove" || command == "rm") { - - if (command == "install" || command == "in") { + else if (command.toEnum() == ZypperCommand::INSTALL_e || + command.toEnum() == ZypperCommand::REMOVE_e ) + { + if (command.toEnum() == ZypperCommand::INSTALL_e) { if (ghelp || arguments.size() < 1) { cerr << "install [options] name...\n" << specific_help; return !ghelp; @@ -704,7 +705,7 @@ int one_command(const string& command, int argc, char **argv) gData.packages_to_install = arguments; } - if (command == "remove" || command == "rm") { + if (command.toEnum() == ZypperCommand::REMOVE_e) { if (ghelp || arguments.size() < 1) { cerr << "remove [options] name...\n" << specific_help @@ -744,7 +745,7 @@ int one_command(const string& command, int argc, char **argv) cout_v << "DONE" << endl; for ( vector::const_iterator it = arguments.begin(); it != arguments.end(); ++it ) { - if (command == "install" || command == "in") { + if (command.toEnum() == ZypperCommand::INSTALL_e) { mark_for_install(kind, *it); } else { @@ -760,7 +761,7 @@ int one_command(const string& command, int argc, char **argv) // TODO -c, --catalog option - else if (command == "search" || command == "se") { + else if (command_str == "search" || command_str == "se") { ZyppSearchOptions options; if (ghelp) { @@ -814,7 +815,7 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( patch check )-------------------------------- // TODO: rug summary - else if (command == "patch-check" || command == "pchk") { + else if (command_str == "patch-check" || command_str == "pchk") { if (ghelp) { cerr << specific_help; return !ghelp; @@ -841,7 +842,7 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( patches )------------------------------------ - else if (command == "patches" || command == "pch") { + else if (command_str == "patches" || command_str == "pch") { if (ghelp) { cerr << specific_help; return !ghelp; @@ -857,7 +858,7 @@ int one_command(const string& command, int argc, char **argv) // --------------------------( list updates )------------------------------- - else if (command == "list-updates" || command == "lu") { + else if (command_str == "list-updates" || command_str == "lu") { if (ghelp) { // FIXME catalog... cerr << specific_help; @@ -884,7 +885,7 @@ int one_command(const string& command, int argc, char **argv) // -----------------------------( update )---------------------------------- - else if (command == "update" || command == "up") { + else if (command_str == "update" || command_str == "up") { if (ghelp) { cerr << "update [options]\n" << specific_help @@ -916,7 +917,7 @@ int one_command(const string& command, int argc, char **argv) // -----------------------------( info )------------------------------------ - else if (command == "info" || command == "if" || command == "patch-info") { + else if (command_str == "info" || command_str == "if" || command_str == "patch-info") { if (ghelp || arguments.size() == 0) { cerr << specific_help; return !ghelp; @@ -927,7 +928,7 @@ int one_command(const string& command, int argc, char **argv) cond_load_resolvables (); establish (); - printInfo(command,arguments); + printInfo(command_str,arguments); return ZYPPER_EXIT_OK; }