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