1 /* 2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de> 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <getopt.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 #include <Errors.h> 12 #include <SupportDefs.h> 13 14 #include <package/AddRepositoryRequest.h> 15 #include <package/Context.h> 16 #include <package/RefreshRepositoryRequest.h> 17 #include <package/PackageRoster.h> 18 19 #include "Command.h" 20 #include "DecisionProvider.h" 21 #include "JobStateListener.h" 22 #include "pkgman.h" 23 24 25 using namespace BPackageKit; 26 27 28 // TODO: internationalization! 29 30 31 static const char* const kShortUsage = 32 " %command% <repo-base-url>\n" 33 " Adds the repository with the given <repo-base-URL>.\n"; 34 35 static const char* const kLongUsage = 36 "Usage: %program% %command% <repo-URL> [<repo-URL> ...]\n" 37 "Adds one or more repositories by downloading them from the given URL(s).\n" 38 "\n"; 39 40 41 DEFINE_COMMAND(AddRepoCommand, "add-repo", kShortUsage, kLongUsage, 42 kCommandCategoryRepositories) 43 44 45 int 46 AddRepoCommand::Execute(int argc, const char* const* argv) 47 { 48 bool asUserRepository = false; 49 50 while (true) { 51 static struct option sLongOptions[] = { 52 { "help", no_argument, 0, 'h' }, 53 { "user", no_argument, 0, 'u' }, 54 { 0, 0, 0, 0 } 55 }; 56 57 opterr = 0; // don't print errors 58 int c = getopt_long(argc, (char**)argv, "hu", sLongOptions, NULL); 59 if (c == -1) 60 break; 61 62 switch (c) { 63 case 'h': 64 PrintUsageAndExit(false); 65 break; 66 67 case 'u': 68 asUserRepository = true; 69 break; 70 71 default: 72 PrintUsageAndExit(true); 73 break; 74 } 75 } 76 77 // The remaining arguments are repo URLs, i. e. at least one more argument. 78 if (argc < optind + 1) 79 PrintUsageAndExit(true); 80 81 const char* const* repoURLs = argv + optind; 82 int urlCount = argc - optind; 83 84 DecisionProvider decisionProvider; 85 JobStateListener listener; 86 BContext context(decisionProvider, listener); 87 88 status_t result; 89 for (int i = 0; i < urlCount; ++i) { 90 AddRepositoryRequest addRequest(context, repoURLs[i], asUserRepository); 91 result = addRequest.Process(true); 92 if (result != B_OK) { 93 if (result != B_CANCELED) { 94 DIE(result, "request for adding repository \"%s\" failed", 95 repoURLs[i]); 96 } 97 return 1; 98 } 99 100 // now refresh the repo-cache of the new repository 101 BString repoName = addRequest.RepositoryName(); 102 BPackageRoster roster; 103 BRepositoryConfig repoConfig; 104 roster.GetRepositoryConfig(repoName, &repoConfig); 105 106 BRefreshRepositoryRequest refreshRequest(context, repoConfig); 107 result = refreshRequest.Process(true); 108 if (result != B_OK) { 109 if (result != B_CANCELED) { 110 DIE(result, "request for refreshing repository \"%s\" failed", 111 repoName.String()); 112 } 113 return 1; 114 } 115 } 116 117 return 0; 118 } 119