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 "pkgman.h"
25
26
27// TODO: internationalization!
28
29
30using namespace BPackageKit;
31
32
33static const char* kCommandUsage =
34	"Usage:\n"
35	"    %s list-repos [options]\n"
36	"Lists all configured package repositories.\n"
37	"\n"
38;
39
40
41static void
42print_command_usage_and_exit(bool error)
43{
44    fprintf(error ? stderr : stdout, kCommandUsage, kProgramName);
45    exit(error ? 1 : 0);
46}
47
48
49int
50command_list_repos(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				print_command_usage_and_exit(false);
69				break;
70
71			case 'v':
72				verbose = true;
73				break;
74
75			default:
76				print_command_usage_and_exit(true);
77				break;
78		}
79	}
80
81	// No remaining arguments.
82	if (argc != optind)
83		print_command_usage_and_exit(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\tpriority:  %u\n", repoConfig.Priority());
108
109		if (verbose) {
110			BRepositoryCache repoCache;
111			result = roster.GetRepositoryCache(repoName, &repoCache);
112			if (result == B_OK) {
113				printf("\t\tvendor:    %s\n",
114					repoCache.Info().Vendor().String());
115				printf("\t\tsummary:   %s\n",
116					repoCache.Info().Summary().String());
117				printf("\t\tarch:      %s\n", BPackageInfo::kArchitectureNames[
118						repoCache.Info().Architecture()]);
119				printf("\t\tpkg-count: %lu\n", repoCache.CountPackages());
120				printf("\t\torig-url:  %s\n",
121					repoCache.Info().OriginalBaseURL().String());
122				printf("\t\torig-prio: %u\n", repoCache.Info().Priority());
123
124			} else
125				printf("\t\t<no repository cache found>\n");
126		}
127	}
128
129	return 0;
130}
131