Added service-rename
authorMartin Vidner <mvidner@suse.cz>
Fri, 29 Sep 2006 13:18:05 +0000 (13:18 +0000)
committerMartin Vidner <mvidner@suse.cz>
Fri, 29 Sep 2006 13:18:05 +0000 (13:18 +0000)
(bug: the db file for old name must be removed manually)
Fixed aliases that had a numeric prefix

tools/zmart/zmart-sources.cc
tools/zmart/zmart-sources.h
tools/zmart/zypper.cc

index b433122..46cd582 100644 (file)
@@ -3,6 +3,7 @@
 #include "zmart-sources.h"
 
 #include <boost/format.hpp>
+#include <boost/lexical_cast.hpp>
 
 #include <zypp/target/store/PersistentStorage.h>
 
@@ -191,17 +192,26 @@ ostream& operator << (ostream& s, const vector<T>& v) {
   return s;
 }
 
+template <typename Target, typename Source>
+void safe_lexical_cast (Source s, Target &tr) {
+  try {
+    tr = boost::lexical_cast<Target> (s);
+  }
+  catch (boost::bad_lexical_cast &) {
+  }
+}
+
 //! remove a source, identified in any way: alias, url, id
 // may throw:
-void remove_source( const std::string anystring )
+void remove_source( const std::string& anystring )
 {
   cerr_vv << "Constructing SourceManager" << endl;
   SourceManager_Ptr manager = SourceManager::sourceManager();
   cerr_vv << "Restoring SourceManager" << endl;
   manager->restore ("/", true /*use_cache*/);
 
-  SourceManager::SourceId sid;
-  zypp::str::strtonum (anystring, sid);
+  SourceManager::SourceId sid = 0;
+  safe_lexical_cast (anystring, sid);
   if (sid > 0) {
     cerr_v << "removing source " << sid << endl;
     try {
@@ -261,3 +271,75 @@ void remove_source( const std::string anystring )
   cerr_vv << "Storing source data" << endl;
   manager->store( "/", true /*metadata_cache*/ );
 }
+
+//! rename a source, identified in any way: alias, url, id
+void rename_source( const std::string& anystring, const std::string& newalias )
+{
+  cerr_vv << "Constructing SourceManager" << endl;
+  SourceManager_Ptr manager = SourceManager::sourceManager();
+  cerr_vv << "Restoring SourceManager" << endl;
+  manager->restore ("/", true /*use_cache*/);
+
+  Source_Ref src;
+
+  SourceManager::SourceId sid = 0;
+  safe_lexical_cast (anystring, sid);
+  if (sid > 0) {
+    try {
+      src = manager->findSource (sid);
+    }
+    catch (const Exception & ex) {
+      ZYPP_CAUGHT (ex);
+      // boost::format: %s is fine regardless of the actual type :-)
+      cerr << format ("Source %s not found.") % sid << endl;
+    }
+  }
+  else {
+    bool is_url = false;
+    // could it be a URL?
+    cerr_vv << "Registered schemes: " << Url::getRegisteredSchemes () << endl;
+    string::size_type pos = anystring.find (':');
+    if (pos != string::npos) {
+      string scheme (anystring, 0, pos);
+      if (Url::isRegisteredScheme (scheme)) {
+       is_url = true;
+       cerr_vv << "Looks like a URL" << endl;
+
+       Url url;
+       try {
+         url = Url (anystring);
+       }
+       catch ( const Exception & excpt_r ) {
+         ZYPP_CAUGHT( excpt_r );
+         cerr << "URL is invalid: " << excpt_r.asUserString() << endl;
+       }
+       if (url.isValid ()) {
+         try {
+           src = manager->findSourceByUrl (url);
+         }
+         catch (const Exception & ex) {
+           ZYPP_CAUGHT (ex);
+           cerr << format ("Source %s not found.") % url.asString() << endl;
+         }
+       }
+      }
+    }
+
+    if (!is_url) {
+      try {
+       src = manager->findSource (anystring);
+      }
+      catch (const Exception & ex) {
+       ZYPP_CAUGHT (ex);
+       cerr << format ("Source %s not found.") % anystring << endl;
+      }
+    }
+  }
+
+  if (src) {
+    src.setAlias (newalias);
+  }
+
+  cerr_vv << "Storing source data" << endl;
+  manager->store( "/", true /*metadata_cache*/ );
+}
index f87bea7..b933717 100644 (file)
@@ -15,7 +15,8 @@
 void init_system_sources();
 void include_source_by_url( const zypp::Url &url );
 void add_source_by_url( const zypp::Url &url, std::string alias );
-void remove_source( const std::string anystring );
+void remove_source( const std::string& anystring );
+void rename_source( const std::string& anystring, const std::string& newalias );
 void list_system_sources();
 
 #endif
index a701c5e..c156454 100644 (file)
@@ -205,6 +205,9 @@ int main(int argc, char **argv)
     if (optind < argc) {
       command = argv[optind++];
     }
+    else {
+      command = "";
+    }
   }
 
   if (command.empty()) {
@@ -220,11 +223,13 @@ int main(int argc, char **argv)
   string specific_help;
 
   string help_commands = "  Commands:\n"
+      "\thelp\t\t\tHelp\n"
       "\tinstall, in\t\tInstall packages or resolvables\n"
       "\tremove, rm\t\tRemove packages or resolvables\n"
       "\tservice-list, sl\tList services aka installation sources\n"
       "\tservice-add, sa\t\tAdd a new service\n"
-      "\tservice-delete, sd\t\tDelete a service\n"
+      "\tservice-delete, sd\tDelete a service\n"
+      "\tservice-rename, sr\tRename a service\n"
       ;
 
   string help_global_source_options = "  Source options:\n"
@@ -289,7 +294,7 @@ int main(int argc, char **argv)
   if (copts.count("_unknown"))
     return 1;
 
-  list<string> arguments;
+  vector<string> arguments;
   if (optind < argc) {
     cerr_v << "non-option ARGV-elements: ";
     while (optind < argc) {
@@ -392,13 +397,12 @@ int main(int argc, char **argv)
       return !help;
     }
       
-    Url url = make_url (arguments.front());
-    arguments.pop_front();
+    Url url = make_url (arguments[0]);
     if (!url.isValid())
       return 1;
     string alias = url.asString();
-    if (!arguments.empty())
-      alias = arguments.front();
+    if (arguments.size() > 1)
+      alias = arguments[1];
 
     // load gpg keys
     // FIXME only once
@@ -429,7 +433,29 @@ int main(int argc, char **argv)
 
     try {
       // also stores it
-      remove_source(arguments.front());
+      remove_source(arguments[0]);
+    }
+    catch ( const Exception & excpt_r )
+    {
+      cerr << excpt_r.asUserString() << endl;
+      return 1;
+    }
+
+    return 0;
+  }
+
+  if (command == "service-rename" || command == "sr")
+  {
+    if (help || arguments.size() < 2) {
+      cerr << "service-rename [options] <URI|alias> <new-alias>\n"
+          << specific_help
+       ;
+      return !help;
+    }
+    
+    try {
+      // also stores it
+      rename_source (arguments[0], arguments[1]);
     }
     catch ( const Exception & excpt_r )
     {
@@ -449,7 +475,7 @@ int main(int argc, char **argv)
       return !help;
     }
       
-    gData.packages_to_install = vector<string>(arguments.begin(), arguments.end());
+    gData.packages_to_install = arguments;
   }
   if (command == "remove" || command == "rm") {
     if (help || arguments.size() < 1) {
@@ -459,7 +485,7 @@ int main(int argc, char **argv)
       return !help;
     }
 
-    gData.packages_to_uninstall = vector<string>(arguments.begin(), arguments.end());
+    gData.packages_to_uninstall = arguments;
   }
 
   if (!gData.packages_to_install.empty() || !gData.packages_to_uninstall.empty()) {