]> git.ipfire.org Git - pakfire.git/blame - src/cli/pakfire-builder.c
cli: Check for root privileges when needed
[pakfire.git] / src / cli / pakfire-builder.c
CommitLineData
fa640ef1
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
732e2f37 21#include <argp.h>
fa640ef1
MT
22#include <limits.h>
23#include <stdio.h>
24#include <stdlib.h>
bfc9a02a 25#include <syslog.h>
fa640ef1
MT
26#include <unistd.h>
27
5301d9e1 28#include <pakfire/arch.h>
fa640ef1
MT
29#include <pakfire/pakfire.h>
30#include <pakfire/string.h>
31
817f7557 32#include "lib/assert.h"
060a1182 33#include "lib/build.h"
d20b1a83 34#include "lib/clean.h"
2c80538d 35#include "lib/command.h"
961d9b9e 36#include "lib/config.h"
5f9bc3d2 37#include "lib/dist.h"
0a333c90 38#include "lib/image.h"
ecdd76bd 39#include "lib/info.h"
ee8a3a64 40#include "lib/pakfire.h"
32bbeefc 41#include "lib/progressbar.h"
d20b1a83 42#include "lib/provides.h"
3467dd08 43#include "lib/repo.h"
418f36f6 44#include "lib/repolist.h"
d20b1a83 45#include "lib/requires.h"
beee2bda 46#include "lib/shell.h"
d20b1a83 47#include "lib/search.h"
2c80538d
MT
48#include "lib/terminal.h"
49#include "lib/version.h"
fa640ef1 50
732e2f37 51const char* argp_program_version = PACKAGE_VERSION;
fa640ef1 52
732e2f37
MT
53enum {
54 OPT_ARCH = 1,
55 OPT_DEBUG = 2,
56 OPT_DISTRO = 3,
fa640ef1 57
732e2f37
MT
58 OPT_ENABLE_REPO = 4,
59 OPT_DISABLE_REPO = 5,
60};
fa640ef1 61
732e2f37 62static struct argp_option options[] = {
fc5c73fe
MT
63 { "arch", OPT_ARCH, "ARCH", 0, "Choose an architecture", 0 },
64 { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 },
65 { "distro", OPT_DISTRO, "DISTRO", 0, "Build for this distribution", 0 },
66 { "enable-repo", OPT_ENABLE_REPO, "REPO", 0, "Enable a repository", 0 },
67 { "disable-repo", OPT_DISABLE_REPO, "REPO", 0, "Disable a repository", 0 },
732e2f37
MT
68 { NULL },
69};
fa640ef1 70
732e2f37 71static const struct command commands[] = {
8cbf801d
MT
72 { "build", cli_build, 1, -1, 0 },
73 { "clean", cli_clean, 0, 0, 0 },
74 { "dist", cli_dist, 1, -1, 0 },
75 { "image", cli_image, -1, -1, 0 },
76 { "info", cli_info, 1, -1, 0 },
77 { "provides", cli_provides, 1, -1, 0 },
c5ad245b 78 { "repo", cli_repo, -1, -1, 0 },
17e361e1 79 { "repolist", cli_repolist, 0, 0, 0 },
1f81cbb2 80 { "requires", cli_requires, 1, -1, 0 },
15b7db71 81 { "shell", cli_shell, 0, 0, 0 },
8cbf801d 82 { "search", cli_search, 1, -1, 0 },
732e2f37
MT
83 { NULL },
84};
fa640ef1 85
50d2ca50 86const char* args_doc =
732e2f37
MT
87 "build [OPTIONS...] MAKEFILES...\n"
88 "clean\n"
89 "dist MAKEFILES...\n"
90 "image ...\n"
91 "provides PATTERN\n"
92 "repo compose PATH PACKAGES...\n"
93 "repolist\n"
94 "requires PATTERN\n"
95 "shell [OPTIONS...]\n"
96 "search [OPTIONS...] PATTERN";
97
d673ca6c 98static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
ee8a3a64 99 struct cli_config* config = data;
732e2f37
MT
100
101 switch (key) {
102 case OPT_ARCH:
5301d9e1
MT
103 // Check if the architecture is supported
104 if (!pakfire_arch_is_supported_by_host(arg))
105 argp_failure(state, EXIT_FAILURE, 0, "Unsupported architecture: %s", arg);
106
732e2f37 107 config->arch = arg;
fa640ef1
MT
108 break;
109
732e2f37 110 case OPT_DEBUG:
bfc9a02a 111 pakfire_ctx_set_log_level(config->ctx, LOG_DEBUG);
732e2f37 112 break;
fa640ef1 113
732e2f37
MT
114 case OPT_DISTRO:
115 config->distro = arg;
116 break;
fa640ef1 117
732e2f37 118 // Enable/Disable Repositories
fa640ef1 119
732e2f37
MT
120 case OPT_ENABLE_REPO:
121 if (config->num_enable_repos >= MAX_REPOS)
122 return -ENOBUFS;
fa640ef1 123
732e2f37
MT
124 config->enable_repos[config->num_enable_repos++] = arg;
125 break;
fa640ef1 126
732e2f37
MT
127 case OPT_DISABLE_REPO:
128 if (config->num_disable_repos >= MAX_REPOS)
129 return -ENOBUFS;
fa640ef1 130
732e2f37
MT
131 config->disable_repos[config->num_disable_repos++] = arg;
132 break;
fa640ef1 133
732e2f37
MT
134 default:
135 return ARGP_ERR_UNKNOWN;
fa640ef1
MT
136 }
137
138 return 0;
139}
140
a6613775
MT
141static int cli_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
142 void* data, const char* message, const char* question) {
143 // Just print the message
144 if (message)
145 printf("%s\n", message);
146
147 return 0;
148}
149
fa640ef1 150int main(int argc, char* argv[]) {
bfc9a02a
MT
151 struct pakfire_ctx* ctx = NULL;
152 int r;
153
154 // Setup the context
55c00b97 155 r = pakfire_ctx_create(&ctx, NULL);
bfc9a02a
MT
156 if (r)
157 goto ERROR;
158
a6613775
MT
159 // Setup confirm callback
160 pakfire_ctx_set_confirm_callback(ctx, cli_confirm, NULL);
161
10a25449
MT
162 // Setup progress callback
163 pakfire_ctx_set_progress_callback(ctx, cli_setup_progressbar, NULL);
164
41250642
MT
165 // Set the default cache path
166 r = pakfire_ctx_set_cache_path(ctx, "~/.cache/pakfire");
167 if (r)
168 goto ERROR;
169
ee8a3a64 170 struct cli_config config = {
bfc9a02a 171 .ctx = ctx,
961d9b9e 172 .distro = cli_get_default_distro(ctx),
fa640ef1 173 .arch = NULL,
0d54159f 174 .flags = PAKFIRE_FLAGS_BUILD,
fa640ef1 175 };
fa640ef1 176
ee8a3a64 177 // Parse the command line and run any commands
9b8d2c6e
MT
178 r = cli_parse(options, commands, args_doc, NULL,
179 parse, CLI_REQUIRE_ROOT, argc, argv, &config);
bfc9a02a
MT
180
181ERROR:
182 if (ctx)
183 pakfire_ctx_unref(ctx);
184
185 return r;
fa640ef1 186}