xref: /haiku/src/bin/pkgman/command_uninstall.cpp (revision 4e3137c085bae361922078f123dceb92da700640)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  */
8 
9 
10 #include <getopt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include "Command.h"
15 #include "pkgman.h"
16 #include "PackageManager.h"
17 
18 
19 // TODO: internationalization!
20 
21 
22 using namespace BPackageKit;
23 using namespace BPackageKit::BPrivate;
24 
25 
26 static const char* const kShortUsage =
27 	"  %command% <package> ...\n"
28 	"    Uninstalls one or more packages.\n";
29 
30 static const char* const kLongUsage =
31 	"Usage: %program% %command% <package> ...\n"
32 	"Uninstalls the specified packages.\n"
33 	"\n"
34 	"Options:\n"
35 	"  --debug <level>\n"
36 	"    Print debug output. <level> should be between 0 (no debug output,\n"
37 	"    the default) and 10 (most debug output).\n"
38 	"  -H, --home\n"
39 	"    Uninstall the packages from the user's home directory. Default is to\n"
40 	"    uninstall from the system directory.\n"
41 	"  -y\n"
42 	"    Non-interactive mode. Automatically confirm changes, but fail when\n"
43 	"    encountering problems.\n"
44 	"\n";
45 
46 
47 DEFINE_COMMAND(UninstallCommand, "uninstall", kShortUsage, kLongUsage,
48 	kCommandCategoryPackages)
49 
50 
51 int
52 UninstallCommand::Execute(int argc, const char* const* argv)
53 {
54 	BPackageInstallationLocation location
55 		= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
56 	bool interactive = true;
57 
58 	while (true) {
59 		static struct option sLongOptions[] = {
60 			{ "debug", required_argument, 0, OPTION_DEBUG },
61 			{ "help", no_argument, 0, 'h' },
62 			{ "home", no_argument, 0, 'H' },
63 			{ 0, 0, 0, 0 }
64 		};
65 
66 		opterr = 0; // don't print errors
67 		int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
68 		if (c == -1)
69 			break;
70 
71 		if (fCommonOptions.HandleOption(c))
72 			continue;
73 
74 		switch (c) {
75 			case 'h':
76 				PrintUsageAndExit(false);
77 				break;
78 
79 			case 'H':
80 				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
81 				break;
82 
83 			case 'y':
84 				interactive = false;
85 				break;
86 
87 			default:
88 				PrintUsageAndExit(true);
89 				break;
90 		}
91 	}
92 
93 	// The remaining arguments are the packages to be uninstalled.
94 	if (argc < optind + 1)
95 		PrintUsageAndExit(true);
96 
97 	int packageCount = argc - optind;
98 	const char* const* packages = argv + optind;
99 
100 	// perform the installation
101 	PackageManager packageManager(location, interactive);
102 	packageManager.SetDebugLevel(fCommonOptions.DebugLevel());
103 	packageManager.Uninstall(packages, packageCount);
104 
105 	return 0;
106 }
107