2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de>
3 * Distributed under the terms of the MIT License.
12 #include <StringList.h>
14 #include <package/Context.h>
15 #include <package/RefreshRepositoryRequest.h>
16 #include <package/PackageRoster.h>
19 #include "DecisionProvider.h"
20 #include "JobStateListener.h"
24 using namespace BPackageKit
;
27 // TODO: internationalization!
30 static const char* const kShortUsage
=
31 " %command% [<repo-name> ...]\n"
32 " Refreshes all or just the given repositories.\n";
34 static const char* const kLongUsage
=
35 "Usage: %program% %command% [<repo-name> ...]\n"
36 "Refreshes all or just the given repositories.\n"
40 DEFINE_COMMAND(RefreshCommand
, "refresh", kShortUsage
, kLongUsage
,
41 kCommandCategoryRepositories
)
45 RefreshCommand::Execute(int argc
, const char* const* argv
)
48 static struct option sLongOptions
[] = {
49 { "help", no_argument
, 0, 'h' },
53 opterr
= 0; // don't print errors
54 int c
= getopt_long(argc
, (char**)argv
, "hu", sLongOptions
, NULL
);
60 PrintUsageAndExit(false);
64 PrintUsageAndExit(true);
69 // The remaining arguments are repo names.
70 const char* const* repoArgs
= argv
+ optind
;
71 int nameCount
= argc
- optind
;
73 BStringList
repositoryNames(20);
75 BPackageRoster roster
;
77 status_t result
= roster
.GetRepositoryNames(repositoryNames
);
79 DIE(result
, "can't collect repository names");
81 for (int i
= 0; i
< nameCount
; ++i
) {
82 if (!repositoryNames
.Add(repoArgs
[i
]))
83 DIE(B_NO_MEMORY
, "can't allocate repository name");
87 DecisionProvider decisionProvider
;
88 JobStateListener listener
;
89 BContext
context(decisionProvider
, listener
);
92 for (int i
= 0; i
< repositoryNames
.CountStrings(); ++i
) {
93 const BString
& repoName
= repositoryNames
.StringAt(i
);
94 BRepositoryConfig repoConfig
;
95 result
= roster
.GetRepositoryConfig(repoName
, &repoConfig
);
98 repoConfig
.Entry().GetPath(&path
);
99 WARN(result
, "skipping repository-config '%s'", path
.Path());
103 BRefreshRepositoryRequest
refreshRequest(context
, repoConfig
);
104 result
= refreshRequest
.Process();
105 if (result
!= B_OK
) {
106 DIE(result
, "request for refreshing repository \"%s\" failed",