xref: /haiku/src/bin/pkgman/command_update.cpp (revision 37fedaf8494b34aad811abcc49e79aa32943f880)
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 	"    Updates the specified or all packages.\n";
29 
30 static const char* const kLongUsage =
31 	"Usage: %program% %command% [ <package> ... ]\n"
32 	"Updates the specified packages. If no packages are given, all packages\n"
33 	"in the installation location are updated.\n"
34 	"\n"
35 	"Options:\n"
36 	"  -H, --home\n"
37 	"    Update the packages in the user's home directory. Default is to\n"
38 	"    update in the system directory.\n"
39 	"  -y\n"
40 	"    Non-interactive mode. Automatically confirm changes, but fail when\n"
41 	"    encountering problems.\n"
42 	"\n";
43 
44 
45 DEFINE_COMMAND(UpdateCommand, "update", kShortUsage, kLongUsage,
46 	kCommandCategoryPackages)
47 
48 
49 int
50 UpdateCommand::Execute(int argc, const char* const* argv)
51 {
52 	BPackageInstallationLocation location
53 		= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
54 	bool interactive = true;
55 
56 	while (true) {
57 		static struct option sLongOptions[] = {
58 			{ "help", no_argument, 0, 'h' },
59 			{ "home", no_argument, 0, 'H' },
60 			{ 0, 0, 0, 0 }
61 		};
62 
63 		opterr = 0; // don't print errors
64 		int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
65 		if (c == -1)
66 			break;
67 
68 		switch (c) {
69 			case 'h':
70 				PrintUsageAndExit(false);
71 				break;
72 
73 			case 'H':
74 				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
75 				break;
76 
77 			case 'y':
78 				interactive = false;
79 				break;
80 
81 			default:
82 				PrintUsageAndExit(true);
83 				break;
84 		}
85 	}
86 
87 	// The remaining arguments, if any, are the packages to be installed.
88 	int packageCount = argc - optind;
89 	const char* const* packages = argv + optind;
90 
91 	// perform the update
92 	PackageManager packageManager(location, interactive);
93 	packageManager.Update(packages, packageCount);
94 
95 	return 0;
96 }
97