#include "zmart-sources.h"
#include <boost/format.hpp>
+#include <boost/lexical_cast.hpp>
#include <zypp/target/store/PersistentStorage.h>
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 {
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*/ );
+}
if (optind < argc) {
command = argv[optind++];
}
+ else {
+ command = "";
+ }
}
if (command.empty()) {
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"
if (copts.count("_unknown"))
return 1;
- list<string> arguments;
+ vector<string> arguments;
if (optind < argc) {
cerr_v << "non-option ARGV-elements: ";
while (optind < argc) {
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
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 )
{
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) {
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()) {