xref: /haiku/src/bin/pkgman/command_full_sync.cpp (revision a629567a9001547736cfe892cdf992be16868fed)
1 /*
2  * Copyright 2013-2014, 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%\n"
28 	"    Synchronizes the installed packages with the repositories.\n";
29 
30 static const char* const kLongUsage =
31 	"Usage: %program% %command%\n"
32 	"Synchronizes the installed packages with the repositories. The command\n"
33 	"is similar to the \"update\" command, but more aggressive. It also\n"
34 	"downgrades or removes packages, if necessary.\n"
35 	"\n"
36 	"Options:\n"
37 	"  -H, --home\n"
38 	"    Synchronizes the packages in the user's home directory. Default is\n"
39 	"    to synchronize the packages 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(FullSyncCommand, "full-sync", kShortUsage, kLongUsage,
47 	kCommandCategoryPackages)
48 
49 
50 int
51 FullSyncCommand::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 	// no remaining arguments
89 	if (optind < argc)
90 		PrintUsageAndExit(true);
91 
92 	// perform the sync
93 	PackageManager packageManager(location, interactive);
94 	packageManager.FullSync();
95 
96 	return 0;
97 }
98