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 18*ead4d20cSOliver Tappe #include <package/RepositoryCache.h> 19500bb630SOliver Tappe #include <package/RepositoryConfig.h> 20*ead4d20cSOliver Tappe #include <package/PackageInfo.h> 217d7ed9bfSOliver Tappe #include <package/PackageRoster.h> 22*ead4d20cSOliver Tappe #include <package/RepositoryInfo.h> 23500bb630SOliver Tappe 24500bb630SOliver Tappe #include "pkgman.h" 25500bb630SOliver Tappe 26500bb630SOliver Tappe 27500bb630SOliver Tappe // TODO: internationalization! 28500bb630SOliver Tappe 29500bb630SOliver Tappe 307d7ed9bfSOliver Tappe using namespace BPackageKit; 31500bb630SOliver Tappe 32500bb630SOliver Tappe 33500bb630SOliver Tappe static const char* kCommandUsage = 34500bb630SOliver Tappe "Usage:\n" 35500bb630SOliver Tappe " %s list-repos [options]\n" 36500bb630SOliver Tappe "Lists all configured package repositories.\n" 37500bb630SOliver Tappe "\n" 38500bb630SOliver Tappe ; 39500bb630SOliver Tappe 40500bb630SOliver Tappe 41500bb630SOliver Tappe static void 42500bb630SOliver Tappe print_command_usage_and_exit(bool error) 43500bb630SOliver Tappe { 44500bb630SOliver Tappe fprintf(error ? stderr : stdout, kCommandUsage, kProgramName); 45500bb630SOliver Tappe exit(error ? 1 : 0); 46500bb630SOliver Tappe } 47500bb630SOliver Tappe 48500bb630SOliver Tappe 49500bb630SOliver Tappe int 50500bb630SOliver Tappe command_list_repos(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': 68500bb630SOliver Tappe print_command_usage_and_exit(false); 69500bb630SOliver Tappe break; 70500bb630SOliver Tappe 71500bb630SOliver Tappe case 'v': 72500bb630SOliver Tappe verbose = true; 73500bb630SOliver Tappe break; 74500bb630SOliver Tappe 75500bb630SOliver Tappe default: 76500bb630SOliver Tappe print_command_usage_and_exit(true); 77500bb630SOliver Tappe break; 78500bb630SOliver Tappe } 79500bb630SOliver Tappe } 80500bb630SOliver Tappe 81500bb630SOliver Tappe // No remaining arguments. 82500bb630SOliver Tappe if (argc != optind) 83500bb630SOliver Tappe print_command_usage_and_exit(true); 84500bb630SOliver Tappe 8535edda8fSOliver Tappe BObjectList<BString> repositoryNames(20, true); 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 9135edda8fSOliver Tappe for (int i = 0; i < repositoryNames.CountItems(); ++i) { 9235edda8fSOliver Tappe const BString& repoName = *(repositoryNames.ItemAt(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 } 101*ead4d20cSOliver Tappe if (verbose && i > 0) 102*ead4d20cSOliver Tappe printf("\n"); 103500bb630SOliver Tappe printf(" %s %s\n", 10435edda8fSOliver Tappe repoConfig.IsUserSpecific() ? "[User]" : " ", 10535edda8fSOliver Tappe repoConfig.Name().String()); 106*ead4d20cSOliver Tappe printf("\t\tbase-url: %s\n", repoConfig.BaseURL().String()); 107*ead4d20cSOliver Tappe printf("\t\tpriority: %u\n", repoConfig.Priority()); 108*ead4d20cSOliver Tappe 109*ead4d20cSOliver Tappe if (verbose) { 110*ead4d20cSOliver Tappe BRepositoryCache repoCache; 111*ead4d20cSOliver Tappe result = roster.GetRepositoryCache(repoName, &repoCache); 112*ead4d20cSOliver Tappe if (result == B_OK) { 113*ead4d20cSOliver Tappe printf("\t\tvendor: %s\n", 114*ead4d20cSOliver Tappe repoCache.Info().Vendor().String()); 115*ead4d20cSOliver Tappe printf("\t\tsummary: %s\n", 116*ead4d20cSOliver Tappe repoCache.Info().Summary().String()); 117*ead4d20cSOliver Tappe printf("\t\tarch: %s\n", BPackageInfo::kArchitectureNames[ 118*ead4d20cSOliver Tappe repoCache.Info().Architecture()]); 119*ead4d20cSOliver Tappe printf("\t\tpkg-count: %lu\n", repoCache.PackageCount()); 120*ead4d20cSOliver Tappe printf("\t\torig-url: %s\n", 121*ead4d20cSOliver Tappe repoCache.Info().OriginalBaseURL().String()); 122*ead4d20cSOliver Tappe printf("\t\torig-prio: %u\n", repoCache.Info().Priority()); 123*ead4d20cSOliver Tappe 124*ead4d20cSOliver Tappe } else 125*ead4d20cSOliver Tappe printf("\t\t<no repository cache found>\n"); 126*ead4d20cSOliver Tappe } 127500bb630SOliver Tappe } 128500bb630SOliver Tappe 129500bb630SOliver Tappe return 0; 130500bb630SOliver Tappe } 131