xref: /haiku/src/bin/pkgman/command_update.cpp (revision f2762b2d5658b0b6c9ff62997a1485194cc77e7c)
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 	"  --debug <level>\n"
40 	"    Print debug output. <level> should be between 0 (no debug output,\n"
41 	"    the default) and 10 (most debug output).\n"
42 	"  -H, --home\n"
43 	"    Update the packages in the user's home directory. Default is to\n"
44 	"    update in the system directory.\n"
45 	"  -y\n"
46 	"    Non-interactive mode. Automatically confirm changes, but fail when\n"
47 	"    encountering problems.\n"
48 	"\n";
49 
50 
51 DEFINE_COMMAND(UpdateCommand, "update", kShortUsage, kLongUsage,
52 	COMMAND_CATEGORY_PACKAGES)
53 
54 
55 int
56 UpdateCommand::Execute(int argc, const char* const* argv)
57 {
58 	BPackageInstallationLocation location
59 		= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
60 	bool interactive = true;
61 
62 	while (true) {
63 		static struct option sLongOptions[] = {
64 			{ "debug", required_argument, 0, OPTION_DEBUG },
65 			{ "help", no_argument, 0, 'h' },
66 			{ "home", no_argument, 0, 'H' },
67 			{ 0, 0, 0, 0 }
68 		};
69 
70 		opterr = 0; // don't print errors
71 		int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
72 		if (c == -1)
73 			break;
74 
75 		if (fCommonOptions.HandleOption(c))
76 			continue;
77 
78 		switch (c) {
79 			case 'h':
80 				PrintUsageAndExit(false);
81 				break;
82 
83 			case 'H':
84 				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
85 				break;
86 
87 			case 'y':
88 				interactive = false;
89 				break;
90 
91 			default:
92 				PrintUsageAndExit(true);
93 				break;
94 		}
95 	}
96 
97 	// The remaining arguments, if any, are the packages to be installed.
98 	int packageCount = argc - optind;
99 	const char* const* packages = argv + optind;
100 
101 	// perform the update
102 	PackageManager packageManager(location, interactive);
103 	packageManager.SetDebugLevel(fCommonOptions.DebugLevel());
104 	packageManager.Update(packages, packageCount);
105 
106 	return 0;
107 }
108