]> git.ipfire.org Git - pakfire.git/blame - src/cli/pakfire.c
cli: Check for root privileges when needed
[pakfire.git] / src / cli / pakfire.c
CommitLineData
c9abb903
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2023 Pakfire development team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
a0262a9d 21#include <argp.h>
bfc9a02a 22#include <syslog.h>
c9abb903 23
2c80538d
MT
24#include "lib/clean.h"
25#include "lib/command.h"
26#include "lib/info.h"
27#include "lib/install.h"
a0262a9d 28#include "lib/pakfire.h"
10a25449 29#include "lib/progressbar.h"
2c80538d
MT
30#include "lib/provides.h"
31#include "lib/remove.h"
32#include "lib/repolist.h"
33#include "lib/requires.h"
34#include "lib/search.h"
35#include "lib/sync.h"
19bcc976 36#include "lib/terminal.h"
2c80538d
MT
37#include "lib/update.h"
38#include "lib/version.h"
b82d0b7f 39
8aac21f1
MT
40#define MAX_REPOS 16
41
a0262a9d
MT
42const char* argp_program_version = PACKAGE_VERSION;
43
44static const char* args_doc =
45 "install PACKAGES...\n"
46 "remove PACKAGES...\n"
47 "update [PACKAGES...]\n"
48 "sync\n"
49 "search PATTERNS...\n"
50 "provides PATTERNS...\n"
51 "requires PATTERNS...\n"
52 "repolist\n"
53 "clean";
54
55enum {
56 OPT_CONFIG = 1,
57 OPT_DEBUG = 2,
58 OPT_OFFLINE = 3,
59 OPT_ROOT = 4,
19bcc976 60 OPT_YES = 5,
a0262a9d 61
19bcc976
MT
62 OPT_ENABLE_REPO = 6,
63 OPT_DISABLE_REPO = 7,
c9abb903
MT
64};
65
a0262a9d
MT
66static struct argp_option options[] = {
67 { "config", OPT_CONFIG, "FILE", 0, "Use this configuration file", 0 },
68 { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 },
69 { "offline", OPT_OFFLINE, NULL, 0, "Run in offline mode", 0 },
70 { "root", OPT_ROOT, "PATH", 0, "The path to operate in", 0 },
71 { "enable-repo", OPT_ENABLE_REPO, "REPO", 0, "Enable a repository", 0 },
72 { "disable-repo", OPT_DISABLE_REPO, "REPO", 0, "Disable a repository", 0 },
19bcc976 73 { "yes", OPT_YES, NULL, 0, "Answer all questions with yes", 0 },
a0262a9d
MT
74 { NULL },
75};
c9abb903 76
a0262a9d
MT
77static const struct command commands[] = {
78 { "clean", cli_clean, 0, 0, 0 },
79 { "info", cli_info, 1, -1, 0 },
8b881a9b 80 { "install", cli_install, 1, -1, 0 },
a0262a9d 81 { "provides", cli_provides, 1, -1, 0 },
38d531d3 82 { "remove", cli_remove, 1, -1, 0 },
a0262a9d
MT
83 { "repolist", cli_repolist, 0, 0, 0 },
84 { "requires", cli_requires, 1, -1, 0 },
85 { "search", cli_search, 1, -1, 0 },
9d72126e 86 { "sync", cli_sync, 0, 0, 0 },
bbbc51c6 87 { "update", cli_update, 0, -1, 0 },
a0262a9d
MT
88 { NULL },
89};
c9abb903 90
d673ca6c 91static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
a0262a9d 92 struct cli_config* config = data;
c9abb903 93
a0262a9d
MT
94 switch (key) {
95 case OPT_CONFIG:
96 config->config = arg;
c9abb903
MT
97 break;
98
a0262a9d 99 case OPT_DEBUG:
bfc9a02a 100 pakfire_ctx_set_log_level(config->ctx, LOG_DEBUG);
a0262a9d 101 break;
8aac21f1 102
a0262a9d 103 case OPT_OFFLINE:
d11380dd 104 pakfire_ctx_set_flag(config->ctx, PAKFIRE_CTX_OFFLINE);
a0262a9d 105 break;
8aac21f1 106
a0262a9d
MT
107 case OPT_ROOT:
108 config->root = arg;
109 break;
8aac21f1 110
19bcc976
MT
111 case OPT_YES:
112 pakfire_ctx_set_confirm_callback(config->ctx, cli_term_confirm_yes, NULL);
113 break;
114
a0262a9d 115 // Enable/Disable Repositories
c9abb903 116
a0262a9d
MT
117 case OPT_ENABLE_REPO:
118 if (config->num_enable_repos >= MAX_REPOS)
119 return -ENOBUFS;
c9abb903 120
a0262a9d
MT
121 config->enable_repos[config->num_enable_repos++] = arg;
122 break;
c9abb903 123
a0262a9d
MT
124 case OPT_DISABLE_REPO:
125 if (config->num_disable_repos >= MAX_REPOS)
126 return -ENOBUFS;
c9abb903 127
a0262a9d
MT
128 config->disable_repos[config->num_disable_repos++] = arg;
129 break;
dd34b744 130
a0262a9d
MT
131 default:
132 return ARGP_ERR_UNKNOWN;
dd34b744
MT
133 }
134
dd34b744
MT
135 return 0;
136}
137
8aac21f1 138
c9abb903 139int main(int argc, char* argv[]) {
bfc9a02a
MT
140 struct pakfire_ctx* ctx = NULL;
141 int r;
142
143 // Setup the context
55c00b97 144 r = pakfire_ctx_create(&ctx, NULL);
bfc9a02a
MT
145 if (r)
146 goto ERROR;
147
19bcc976
MT
148 // Make this all interactive
149 pakfire_ctx_set_confirm_callback(ctx, cli_term_confirm, NULL);
150
10a25449
MT
151 // Setup progress callback
152 pakfire_ctx_set_progress_callback(ctx, cli_setup_progressbar, NULL);
153
8cfa28c8
MT
154 // Setup pick solution callback
155 pakfire_ctx_set_pick_solution_callback(ctx, cli_term_pick_solution, NULL);
156
a0262a9d 157 struct cli_config config = {
bfc9a02a 158 .ctx = ctx,
c9abb903 159 // XXX hard-coded path
a0262a9d 160 .config = "/etc/pakfire/general.conf",
c9abb903
MT
161 .arch = NULL,
162 .root = "/",
163 .flags = 0,
c9abb903
MT
164 .yes = 0,
165 };
166
a0262a9d 167 // Parse the command line and run any commands
9b8d2c6e
MT
168 r = cli_parse(options, commands, args_doc, NULL, parse,
169 CLI_REQUIRE_ROOT, argc, argv, &config);
bfc9a02a
MT
170
171ERROR:
172 if (ctx)
173 pakfire_ctx_unref(ctx);
174
175 return r;
c9abb903 176}