RemoteDrawingEngine: Reduce RP_READ_BITMAP result timeout.
[haiku.git] / src / bin / pkgman / command_add_repo.cpp
blob6605f3900bb59784c1166bac683fdde13f046b7f
1 /*
2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de>
3 * Distributed under the terms of the MIT License.
4 */
7 #include <getopt.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include <Errors.h>
12 #include <SupportDefs.h>
14 #include <package/AddRepositoryRequest.h>
15 #include <package/Context.h>
16 #include <package/RefreshRepositoryRequest.h>
17 #include <package/PackageRoster.h>
19 #include "Command.h"
20 #include "DecisionProvider.h"
21 #include "JobStateListener.h"
22 #include "pkgman.h"
25 using namespace BPackageKit;
28 // TODO: internationalization!
31 static const char* const kShortUsage =
32 " %command% <repo-base-url>\n"
33 " Adds the repository with the given <repo-base-URL>.\n";
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";
41 DEFINE_COMMAND(AddRepoCommand, "add-repo", kShortUsage, kLongUsage,
42 kCommandCategoryRepositories)
45 int
46 AddRepoCommand::Execute(int argc, const char* const* argv)
48 bool asUserRepository = false;
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 }
57 opterr = 0; // don't print errors
58 int c = getopt_long(argc, (char**)argv, "hu", sLongOptions, NULL);
59 if (c == -1)
60 break;
62 switch (c) {
63 case 'h':
64 PrintUsageAndExit(false);
65 break;
67 case 'u':
68 asUserRepository = true;
69 break;
71 default:
72 PrintUsageAndExit(true);
73 break;
77 // The remaining arguments are repo URLs, i. e. at least one more argument.
78 if (argc < optind + 1)
79 PrintUsageAndExit(true);
81 const char* const* repoURLs = argv + optind;
82 int urlCount = argc - optind;
84 DecisionProvider decisionProvider;
85 JobStateListener listener;
86 BContext context(decisionProvider, listener);
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]);
97 return 1;
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);
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());
113 return 1;
117 return 0;