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 <Errors.h> 12 #include <SupportDefs.h> 13 14 #include <package/DropRepositoryRequest.h> 15 #include <package/Context.h> 16 17 #include "Command.h" 18 #include "DecisionProvider.h" 19 #include "JobStateListener.h" 20 #include "pkgman.h" 21 22 23 using namespace BPackageKit; 24 25 26 // TODO: internationalization! 27 28 29 static const char* const kShortUsage = 30 " %command% <repo-name>\n" 31 " Drops the repository with the given <repo-name>.\n"; 32 33 static const char* const kLongUsage = 34 "Usage: %program% %command% <repo-name>\n" 35 "Drops (i.e. removes) the repository with the given name.\n" 36 "\n"; 37 38 39 DEFINE_COMMAND(DropRepoCommand, "drop-repo", kShortUsage, kLongUsage, 40 kCommandCategoryRepositories) 41 42 43 int 44 DropRepoCommand::Execute(int argc, const char* const* argv) 45 { 46 bool yesMode = false; 47 48 while (true) { 49 static struct option sLongOptions[] = { 50 { "help", no_argument, 0, 'h' }, 51 { "yes", no_argument, 0, 'y' }, 52 { 0, 0, 0, 0 } 53 }; 54 55 opterr = 0; // don't print errors 56 int c = getopt_long(argc, (char**)argv, "hu", sLongOptions, NULL); 57 if (c == -1) 58 break; 59 60 switch (c) { 61 case 'h': 62 PrintUsageAndExit(false); 63 break; 64 65 case 'y': 66 yesMode = true; 67 break; 68 69 default: 70 PrintUsageAndExit(true); 71 break; 72 } 73 } 74 75 // The remaining argument is a repo name, i. e. one more argument. 76 if (argc != optind + 1) 77 PrintUsageAndExit(true); 78 79 const char* repoName = argv[optind]; 80 81 DecisionProvider decisionProvider; 82 // if (yesMode) 83 // decisionProvider.SetAcceptEverything(true); 84 JobStateListener listener; 85 BContext context(decisionProvider, listener); 86 87 status_t result; 88 DropRepositoryRequest dropRequest(context, repoName); 89 result = dropRequest.Process(true); 90 if (result != B_OK) { 91 if (result != B_CANCELED) { 92 DIE(result, "request for dropping repository \"%s\" failed", 93 repoName); 94 } 95 return 1; 96 } 97 98 return 0; 99 } 100