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