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 <new>
12
13#include <Entry.h>
14#include <Errors.h>
15#include <ObjectList.h>
16#include <Path.h>
17
18#include <package/RepositoryCache.h>
19#include <package/RepositoryConfig.h>
20#include <package/PackageInfo.h>
21#include <package/PackageRoster.h>
22#include <package/RepositoryInfo.h>
23
24#include "Command.h"
25#include "pkgman.h"
26
27
28// TODO: internationalization!
29
30
31using namespace BPackageKit;
32
33
34static const char* const kShortUsage =
35	"  %command%\n"
36	"    Lists all repositories.\n";
37
38static const char* const kLongUsage =
39	"Usage:\n"
40	"    %program% %command% [options]\n"
41	"Lists all configured package repositories.\n"
42	"\n";
43
44
45DEFINE_COMMAND(ListReposCommand, "list-repos", kShortUsage, kLongUsage,
46	COMMAND_CATEGORY_REPOSITORIES)
47
48
49int
50ListReposCommand::Execute(int argc, const char* const* argv)
51{
52	bool verbose = false;
53
54	while (true) {
55		static struct option sLongOptions[] = {
56			{ "help", no_argument, 0, 'h' },
57			{ "verbose", no_argument, 0, 'v' },
58			{ 0, 0, 0, 0 }
59		};
60
61		opterr = 0; // don't print errors
62		int c = getopt_long(argc, (char**)argv, "hv", sLongOptions, NULL);
63		if (c == -1)
64			break;
65
66		switch (c) {
67			case 'h':
68				PrintUsageAndExit(false);
69				break;
70
71			case 'v':
72				verbose = true;
73				break;
74
75			default:
76				PrintUsageAndExit(true);
77				break;
78		}
79	}
80
81	// No remaining arguments.
82	if (argc != optind)
83		PrintUsageAndExit(true);
84
85	BStringList repositoryNames(20);
86	BPackageRoster roster;
87	status_t result = roster.GetRepositoryNames(repositoryNames);
88	if (result != B_OK)
89		DIE(result, "can't collect repository names");
90
91	for (int i = 0; i < repositoryNames.CountStrings(); ++i) {
92		const BString& repoName = repositoryNames.StringAt(i);
93		BRepositoryConfig repoConfig;
94		result = roster.GetRepositoryConfig(repoName, &repoConfig);
95		if (result != B_OK) {
96			BPath path;
97			repoConfig.Entry().GetPath(&path);
98			WARN(result, "skipping repository-config '%s'", path.Path());
99			continue;
100		}
101		if (verbose && i > 0)
102			printf("\n");
103		printf(" %s %s\n",
104			repoConfig.IsUserSpecific() ? "[User]" : "      ",
105			repoConfig.Name().String());
106		printf("\t\tbase-url:  %s\n", repoConfig.BaseURL().String());
107		printf("\t\tidentifier: %s\n", repoConfig.Identifier().String());
108		printf("\t\tpriority:  %u\n", repoConfig.Priority());
109
110		if (verbose) {
111			BRepositoryCache repoCache;
112			result = roster.GetRepositoryCache(repoName, &repoCache);
113			if (result == B_OK) {
114				printf("\t\tvendor:    %s\n",
115					repoCache.Info().Vendor().String());
116				printf("\t\tsummary:   %s\n",
117					repoCache.Info().Summary().String());
118				printf("\t\tarch:      %s\n", BPackageInfo::kArchitectureNames[
119						repoCache.Info().Architecture()]);
120				printf("\t\tpkg-count: %" B_PRIu32 "\n",
121					repoCache.CountPackages());
122				printf("\t\tbase-url:  %s\n",
123					repoCache.Info().BaseURL().String());
124				printf("\t\tidentifier:  %s\n",
125					repoCache.Info().Identifier().String());
126				printf("\t\torig-prio: %u\n", repoCache.Info().Priority());
127			} else
128				printf("\t\t<no repository cache found>\n");
129		}
130	}
131
132	return 0;
133}
134