1/*
2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefere.de>
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <getopt.h>
8#include <stdio.h>
9#include <stdlib.h>
10
11#include <Errors.h>
12#include <SupportDefs.h>
13
14#include <package/DropRepositoryRequest.h>
15#include <package/Context.h>
16
17#include "Command.h"
18#include "DecisionProvider.h"
19#include "JobStateListener.h"
20#include "pkgman.h"
21
22
23using namespace BPackageKit;
24
25
26// TODO: internationalization!
27
28
29static const char* const kShortUsage =
30	"  %command% <repo-name>\n"
31	"    Drops the repository with the given <repo-name>.\n";
32
33static const char* const kLongUsage =
34	"Usage: %program% %command% <repo-name>\n"
35	"Drops (i.e. removes) the repository with the given name.\n"
36	"\n";
37
38
39DEFINE_COMMAND(DropRepoCommand, "drop-repo", kShortUsage, kLongUsage,
40	COMMAND_CATEGORY_REPOSITORIES)
41
42
43int
44DropRepoCommand::Execute(int argc, const char* const* argv)
45{
46	bool interactive = true;
47
48	while (true) {
49		static struct option sLongOptions[] = {
50			{ "help", no_argument, 0, 'h' },
51			{ "yes", no_argument, 0, 'y' },
52			{ 0, 0, 0, 0 }
53		};
54
55		opterr = 0; // don't print errors
56		int c = getopt_long(argc, (char**)argv, "hu", sLongOptions, NULL);
57		if (c == -1)
58			break;
59
60		switch (c) {
61			case 'h':
62				PrintUsageAndExit(false);
63				break;
64
65			case 'y':
66				interactive = false;
67				break;
68
69			default:
70				PrintUsageAndExit(true);
71				break;
72		}
73	}
74
75	// The remaining argument is a repo name, i. e. one more argument.
76	if (argc != optind + 1)
77		PrintUsageAndExit(true);
78
79	const char* repoName = argv[optind];
80
81	DecisionProvider decisionProvider;
82	decisionProvider.SetInteractive(interactive);
83	JobStateListener listener;
84	BContext context(decisionProvider, listener);
85
86	status_t result;
87	DropRepositoryRequest dropRequest(context, repoName);
88	result = dropRequest.Process(true);
89	if (result != B_OK) {
90		if (result != B_CANCELED) {
91			DIE(result, "request for dropping repository \"%s\" failed",
92				repoName);
93		}
94		return 1;
95	}
96
97	return 0;
98}
99