xref: /haiku/src/bin/pkgman/command_drop_repo.cpp (revision 323b65468e5836bb27a5e373b14027d902349437)
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 "DecisionProvider.h"
18 #include "JobStateListener.h"
19 #include "pkgman.h"
20 
21 
22 using namespace BPackageKit;
23 
24 
25 // TODO: internationalization!
26 
27 
28 static const char* kCommandUsage =
29 	"Usage: %s drop-repo <repo-name>\n"
30 	"Drops (i.e. removes) the repository with the given name.\n"
31 	"\n"
32 ;
33 
34 
35 static void
36 print_command_usage_and_exit(bool error)
37 {
38     fprintf(error ? stderr : stdout, kCommandUsage, kProgramName);
39     exit(error ? 1 : 0);
40 }
41 
42 
43 int
44 command_drop_repo(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 				print_command_usage_and_exit(false);
63 				break;
64 
65 			case 'y':
66 				yesMode = true;
67 				break;
68 
69 			default:
70 				print_command_usage_and_exit(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 		print_command_usage_and_exit(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.InitCheck();
90 	if (result != B_OK)
91 		DIE(result, "unable to create request for dropping repository");
92 	result = dropRequest.CreateInitialJobs();
93 	if (result != B_OK)
94 		DIE(result, "unable to create necessary jobs");
95 
96 	while (BJob* job = dropRequest.PopRunnableJob()) {
97 		result = job->Run();
98 		delete job;
99 		if (result == B_CANCELED)
100 			return 1;
101 	}
102 
103 	return 0;
104 }
105