1500bb630SOliver Tappe /*
2500bb630SOliver Tappe * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de>
3500bb630SOliver Tappe * Distributed under the terms of the MIT License.
4500bb630SOliver Tappe */
5500bb630SOliver Tappe
6500bb630SOliver Tappe
7500bb630SOliver Tappe #include <getopt.h>
8500bb630SOliver Tappe #include <stdio.h>
9500bb630SOliver Tappe #include <stdlib.h>
10500bb630SOliver Tappe
11500bb630SOliver Tappe #include <new>
12500bb630SOliver Tappe
13500bb630SOliver Tappe #include <Entry.h>
14500bb630SOliver Tappe #include <Errors.h>
15500bb630SOliver Tappe #include <ObjectList.h>
16500bb630SOliver Tappe #include <Path.h>
17500bb630SOliver Tappe
18ead4d20cSOliver Tappe #include <package/RepositoryCache.h>
19500bb630SOliver Tappe #include <package/RepositoryConfig.h>
20ead4d20cSOliver Tappe #include <package/PackageInfo.h>
217d7ed9bfSOliver Tappe #include <package/PackageRoster.h>
22ead4d20cSOliver Tappe #include <package/RepositoryInfo.h>
23500bb630SOliver Tappe
2438e528bbSIngo Weinhold #include "Command.h"
25500bb630SOliver Tappe #include "pkgman.h"
26500bb630SOliver Tappe
27500bb630SOliver Tappe
28500bb630SOliver Tappe // TODO: internationalization!
29500bb630SOliver Tappe
30500bb630SOliver Tappe
317d7ed9bfSOliver Tappe using namespace BPackageKit;
32500bb630SOliver Tappe
33500bb630SOliver Tappe
3438e528bbSIngo Weinhold static const char* const kShortUsage =
3538e528bbSIngo Weinhold " %command%\n"
3638e528bbSIngo Weinhold " Lists all repositories.\n";
3738e528bbSIngo Weinhold
3838e528bbSIngo Weinhold static const char* const kLongUsage =
39500bb630SOliver Tappe "Usage:\n"
4038e528bbSIngo Weinhold " %program% %command% [options]\n"
41500bb630SOliver Tappe "Lists all configured package repositories.\n"
4238e528bbSIngo Weinhold "\n";
43500bb630SOliver Tappe
44500bb630SOliver Tappe
45458a86aaSIngo Weinhold DEFINE_COMMAND(ListReposCommand, "list-repos", kShortUsage, kLongUsage,
4652a61976SAndrew Lindesay COMMAND_CATEGORY_REPOSITORIES)
47500bb630SOliver Tappe
48500bb630SOliver Tappe
49500bb630SOliver Tappe int
Execute(int argc,const char * const * argv)5038e528bbSIngo Weinhold ListReposCommand::Execute(int argc, const char* const* argv)
51500bb630SOliver Tappe {
52500bb630SOliver Tappe bool verbose = false;
53500bb630SOliver Tappe
54500bb630SOliver Tappe while (true) {
55500bb630SOliver Tappe static struct option sLongOptions[] = {
56500bb630SOliver Tappe { "help", no_argument, 0, 'h' },
57500bb630SOliver Tappe { "verbose", no_argument, 0, 'v' },
58500bb630SOliver Tappe { 0, 0, 0, 0 }
59500bb630SOliver Tappe };
60500bb630SOliver Tappe
61500bb630SOliver Tappe opterr = 0; // don't print errors
62500bb630SOliver Tappe int c = getopt_long(argc, (char**)argv, "hv", sLongOptions, NULL);
63500bb630SOliver Tappe if (c == -1)
64500bb630SOliver Tappe break;
65500bb630SOliver Tappe
66500bb630SOliver Tappe switch (c) {
67500bb630SOliver Tappe case 'h':
6838e528bbSIngo Weinhold PrintUsageAndExit(false);
69500bb630SOliver Tappe break;
70500bb630SOliver Tappe
71500bb630SOliver Tappe case 'v':
72500bb630SOliver Tappe verbose = true;
73500bb630SOliver Tappe break;
74500bb630SOliver Tappe
75500bb630SOliver Tappe default:
7638e528bbSIngo Weinhold PrintUsageAndExit(true);
77500bb630SOliver Tappe break;
78500bb630SOliver Tappe }
79500bb630SOliver Tappe }
80500bb630SOliver Tappe
81500bb630SOliver Tappe // No remaining arguments.
82500bb630SOliver Tappe if (argc != optind)
8338e528bbSIngo Weinhold PrintUsageAndExit(true);
84500bb630SOliver Tappe
852021c984SIngo Weinhold BStringList repositoryNames(20);
867d7ed9bfSOliver Tappe BPackageRoster roster;
8735edda8fSOliver Tappe status_t result = roster.GetRepositoryNames(repositoryNames);
8835edda8fSOliver Tappe if (result != B_OK)
8935edda8fSOliver Tappe DIE(result, "can't collect repository names");
90500bb630SOliver Tappe
912021c984SIngo Weinhold for (int i = 0; i < repositoryNames.CountStrings(); ++i) {
922021c984SIngo Weinhold const BString& repoName = repositoryNames.StringAt(i);
937d7ed9bfSOliver Tappe BRepositoryConfig repoConfig;
9435edda8fSOliver Tappe result = roster.GetRepositoryConfig(repoName, &repoConfig);
9535edda8fSOliver Tappe if (result != B_OK) {
9635edda8fSOliver Tappe BPath path;
9735edda8fSOliver Tappe repoConfig.Entry().GetPath(&path);
9835edda8fSOliver Tappe WARN(result, "skipping repository-config '%s'", path.Path());
9935edda8fSOliver Tappe continue;
10035edda8fSOliver Tappe }
101ead4d20cSOliver Tappe if (verbose && i > 0)
102ead4d20cSOliver Tappe printf("\n");
103500bb630SOliver Tappe printf(" %s %s\n",
10435edda8fSOliver Tappe repoConfig.IsUserSpecific() ? "[User]" : " ",
10535edda8fSOliver Tappe repoConfig.Name().String());
106ead4d20cSOliver Tappe printf("\t\tbase-url: %s\n", repoConfig.BaseURL().String());
107*aa272ca3SAdrien Destugues printf("\t\tidentifier: %s\n", repoConfig.Identifier().String());
108ead4d20cSOliver Tappe printf("\t\tpriority: %u\n", repoConfig.Priority());
109ead4d20cSOliver Tappe
110ead4d20cSOliver Tappe if (verbose) {
111ead4d20cSOliver Tappe BRepositoryCache repoCache;
112ead4d20cSOliver Tappe result = roster.GetRepositoryCache(repoName, &repoCache);
113ead4d20cSOliver Tappe if (result == B_OK) {
114ead4d20cSOliver Tappe printf("\t\tvendor: %s\n",
115ead4d20cSOliver Tappe repoCache.Info().Vendor().String());
116ead4d20cSOliver Tappe printf("\t\tsummary: %s\n",
117ead4d20cSOliver Tappe repoCache.Info().Summary().String());
118ead4d20cSOliver Tappe printf("\t\tarch: %s\n", BPackageInfo::kArchitectureNames[
119ead4d20cSOliver Tappe repoCache.Info().Architecture()]);
12071046951SOliver Tappe printf("\t\tpkg-count: %" B_PRIu32 "\n",
12171046951SOliver Tappe repoCache.CountPackages());
1223b17d8ddSAndrew Lindesay printf("\t\tbase-url: %s\n",
1233b17d8ddSAndrew Lindesay repoCache.Info().BaseURL().String());
124*aa272ca3SAdrien Destugues printf("\t\tidentifier: %s\n",
125*aa272ca3SAdrien Destugues repoCache.Info().Identifier().String());
126ead4d20cSOliver Tappe printf("\t\torig-prio: %u\n", repoCache.Info().Priority());
127ead4d20cSOliver Tappe } else
128ead4d20cSOliver Tappe printf("\t\t<no repository cache found>\n");
129ead4d20cSOliver Tappe }
130500bb630SOliver Tappe }
131500bb630SOliver Tappe
132500bb630SOliver Tappe return 0;
133500bb630SOliver Tappe }
134