]> git.ipfire.org Git - pakfire.git/blob - src/cli/pakfire-client.c
cli: Check for root privileges when needed
[pakfire.git] / src / cli / pakfire-client.c
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
21 #include <argp.h>
22 #include <syslog.h>
23
24 #include <pakfire/ctx.h>
25
26 #include "lib/client-build.h"
27 #include "lib/command.h"
28 #include "lib/config.h"
29 #include "lib/pakfire.h"
30 #include "lib/progressbar.h"
31 #include "lib/repo.h"
32 #include "lib/upload.h"
33
34 const char* argp_program_version = PACKAGE_VERSION;
35
36 static const struct command commands[] = {
37 { "build", cli_client_build, 1, -1, 0 },
38 { "repo", cli_repo_client, -1, -1, 0 },
39 { "upload", cli_upload, -1, -1, 0 },
40 { NULL },
41 };
42
43 static const char* args_doc =
44 "repo ...\n"
45 "upload ...";
46
47 static const char* doc = "The Pakfire Build Service Client Tool";
48
49 enum {
50 OPT_DEBUG = 1,
51 OPT_DISTRO = 2,
52 };
53
54 static struct argp_option options[] = {
55 { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 },
56 { "distro", OPT_DISTRO, "DISTRO", 0, "Choose the distribution", 0 },
57 { NULL },
58 };
59
60 static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
61 struct cli_config* config = data;
62
63 switch (key) {
64 case OPT_DEBUG:
65 pakfire_ctx_set_log_level(config->ctx, LOG_DEBUG);
66 break;
67
68 case OPT_DISTRO:
69 config->distro = arg;
70 break;
71
72 default:
73 return ARGP_ERR_UNKNOWN;
74 }
75
76 return 0;
77 }
78
79 int main(int argc, char* argv[]) {
80 struct pakfire_ctx* ctx = NULL;
81 int r;
82
83 // Setup the context
84 r = pakfire_ctx_create(&ctx, NULL);
85 if (r)
86 goto ERROR;
87
88 // Setup progress callback
89 pakfire_ctx_set_progress_callback(ctx, cli_setup_progressbar, NULL);
90
91 struct cli_config config = {
92 .ctx = ctx,
93 .distro = cli_get_default_distro(ctx),
94 };
95
96 // Parse the command line and run any commands
97 r = cli_parse(options, commands, args_doc, doc,
98 parse, 0, argc, argv, &config);
99
100 ERROR:
101 if (ctx)
102 pakfire_ctx_unref(ctx);
103
104 return r;
105 }