xref: /haiku/src/bin/pkgman/command_update.cpp (revision ab4411e89a079bc0a40d901995f3418d998c51b3)
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 	"A <package> argument can be a search string by which the package is\n"
35 	"looked up locally and in a remote repository or a path to a local\n"
36 	"package file. In the latter case the file is copied.\n"
37 	"\n"
38 	"Options:\n"
39 	"  -H, --home\n"
40 	"    Update the packages in the user's home directory. Default is to\n"
41 	"    update in the system directory.\n"
42 	"  -y\n"
43 	"    Non-interactive mode. Automatically confirm changes, but fail when\n"
44 	"    encountering problems.\n"
45 	"\n";
46 
47 
48 DEFINE_COMMAND(UpdateCommand, "update", kShortUsage, kLongUsage,
49 	kCommandCategoryPackages)
50 
51 
52 int
53 UpdateCommand::Execute(int argc, const char* const* argv)
54 {
55 	BPackageInstallationLocation location
56 		= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
57 	bool interactive = true;
58 
59 	while (true) {
60 		static struct option sLongOptions[] = {
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 		switch (c) {
72 			case 'h':
73 				PrintUsageAndExit(false);
74 				break;
75 
76 			case 'H':
77 				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
78 				break;
79 
80 			case 'y':
81 				interactive = false;
82 				break;
83 
84 			default:
85 				PrintUsageAndExit(true);
86 				break;
87 		}
88 	}
89 
90 	// The remaining arguments, if any, are the packages to be installed.
91 	int packageCount = argc - optind;
92 	const char* const* packages = argv + optind;
93 
94 	// perform the update
95 	PackageManager packageManager(location, interactive);
96 	packageManager.Update(packages, packageCount);
97 
98 	return 0;
99 }
100