2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de>
3 * Distributed under the terms of the MIT License.
12 #include <SupportDefs.h>
14 #include <package/AddRepositoryRequest.h>
15 #include <package/Context.h>
16 #include <package/RefreshRepositoryRequest.h>
17 #include <package/PackageRoster.h>
20 #include "DecisionProvider.h"
21 #include "JobStateListener.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"
41 DEFINE_COMMAND(AddRepoCommand
, "add-repo", kShortUsage
, kLongUsage
,
42 kCommandCategoryRepositories
)
46 AddRepoCommand::Execute(int argc
, const char* const* argv
)
48 bool asUserRepository
= false;
51 static struct option sLongOptions
[] = {
52 { "help", no_argument
, 0, 'h' },
53 { "user", no_argument
, 0, 'u' },
57 opterr
= 0; // don't print errors
58 int c
= getopt_long(argc
, (char**)argv
, "hu", sLongOptions
, NULL
);
64 PrintUsageAndExit(false);
68 asUserRepository
= true;
72 PrintUsageAndExit(true);
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
);
89 for (int i
= 0; i
< urlCount
; ++i
) {
90 AddRepositoryRequest
addRequest(context
, repoURLs
[i
], asUserRepository
);
91 result
= addRequest
.Process(true);
93 if (result
!= B_CANCELED
) {
94 DIE(result
, "request for adding repository \"%s\" failed",
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",