]>
Commit | Line | Data |
---|---|---|
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> |
e0ad83d5 | 29 | #include <pakfire/root.h> |
fa640ef1 MT |
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" |
3b8789ed | 40 | #include "lib/lint.h" |
ee8a3a64 | 41 | #include "lib/pakfire.h" |
32bbeefc | 42 | #include "lib/progressbar.h" |
d20b1a83 | 43 | #include "lib/provides.h" |
3467dd08 | 44 | #include "lib/repo.h" |
418f36f6 | 45 | #include "lib/repolist.h" |
d20b1a83 | 46 | #include "lib/requires.h" |
beee2bda | 47 | #include "lib/shell.h" |
d20b1a83 | 48 | #include "lib/search.h" |
a693bd2c | 49 | #include "lib/snapshot.h" |
2c80538d MT |
50 | #include "lib/terminal.h" |
51 | #include "lib/version.h" | |
fa640ef1 | 52 | |
fac63399 | 53 | const char* argp_program_version = PACKAGE_FULLVERSION; |
fa640ef1 | 54 | |
732e2f37 MT |
55 | enum { |
56 | OPT_ARCH = 1, | |
57 | OPT_DEBUG = 2, | |
58 | OPT_DISTRO = 3, | |
fa640ef1 | 59 | |
732e2f37 MT |
60 | OPT_ENABLE_REPO = 4, |
61 | OPT_DISABLE_REPO = 5, | |
62 | }; | |
fa640ef1 | 63 | |
732e2f37 | 64 | static struct argp_option options[] = { |
fc5c73fe MT |
65 | { "arch", OPT_ARCH, "ARCH", 0, "Choose an architecture", 0 }, |
66 | { "debug", OPT_DEBUG, NULL, 0, "Run in debug mode", 0 }, | |
67 | { "distro", OPT_DISTRO, "DISTRO", 0, "Build for this distribution", 0 }, | |
68 | { "enable-repo", OPT_ENABLE_REPO, "REPO", 0, "Enable a repository", 0 }, | |
69 | { "disable-repo", OPT_DISABLE_REPO, "REPO", 0, "Disable a repository", 0 }, | |
732e2f37 MT |
70 | { NULL }, |
71 | }; | |
fa640ef1 | 72 | |
732e2f37 | 73 | static const struct command commands[] = { |
8cbf801d MT |
74 | { "build", cli_build, 1, -1, 0 }, |
75 | { "clean", cli_clean, 0, 0, 0 }, | |
76 | { "dist", cli_dist, 1, -1, 0 }, | |
77 | { "image", cli_image, -1, -1, 0 }, | |
78 | { "info", cli_info, 1, -1, 0 }, | |
3b8789ed | 79 | { "lint", cli_lint, 1, -1, 0 }, |
8cbf801d | 80 | { "provides", cli_provides, 1, -1, 0 }, |
c5ad245b | 81 | { "repo", cli_repo, -1, -1, 0 }, |
17e361e1 | 82 | { "repolist", cli_repolist, 0, 0, 0 }, |
1f81cbb2 | 83 | { "requires", cli_requires, 1, -1, 0 }, |
731ea934 | 84 | { "shell", cli_shell, 0, -1, 0 }, |
8cbf801d | 85 | { "search", cli_search, 1, -1, 0 }, |
a693bd2c | 86 | { "snapshot", cli_snapshot, 1, -1, 0 }, |
732e2f37 MT |
87 | { NULL }, |
88 | }; | |
fa640ef1 | 89 | |
50d2ca50 | 90 | const char* args_doc = |
732e2f37 MT |
91 | "build [OPTIONS...] MAKEFILES...\n" |
92 | "clean\n" | |
93 | "dist MAKEFILES...\n" | |
94 | "image ...\n" | |
3b8789ed | 95 | "lint [ARCHIVE...]\n" |
732e2f37 MT |
96 | "provides PATTERN\n" |
97 | "repo compose PATH PACKAGES...\n" | |
98 | "repolist\n" | |
99 | "requires PATTERN\n" | |
731ea934 | 100 | "shell [OPTIONS...] [COMMAND...]\n" |
a693bd2c MT |
101 | "search [OPTIONS...] PATTERN\n" |
102 | "snapshot [COMMAND...]"; | |
732e2f37 | 103 | |
d673ca6c | 104 | static error_t parse(int key, char* arg, struct argp_state* state, void* data) { |
343d9736 | 105 | struct cli_global_args* args = data; |
732e2f37 MT |
106 | |
107 | switch (key) { | |
108 | case OPT_ARCH: | |
5301d9e1 MT |
109 | // Check if the architecture is supported |
110 | if (!pakfire_arch_is_supported_by_host(arg)) | |
111 | argp_failure(state, EXIT_FAILURE, 0, "Unsupported architecture: %s", arg); | |
112 | ||
343d9736 | 113 | args->arch = arg; |
fa640ef1 MT |
114 | break; |
115 | ||
732e2f37 | 116 | case OPT_DEBUG: |
343d9736 | 117 | pakfire_ctx_set_log_level(args->ctx, LOG_DEBUG); |
732e2f37 | 118 | break; |
fa640ef1 | 119 | |
732e2f37 | 120 | case OPT_DISTRO: |
343d9736 | 121 | args->distro = arg; |
732e2f37 | 122 | break; |
fa640ef1 | 123 | |
732e2f37 | 124 | // Enable/Disable Repositories |
fa640ef1 | 125 | |
732e2f37 | 126 | case OPT_ENABLE_REPO: |
343d9736 | 127 | if (args->num_enable_repos >= MAX_REPOS) |
732e2f37 | 128 | return -ENOBUFS; |
fa640ef1 | 129 | |
343d9736 | 130 | args->enable_repos[args->num_enable_repos++] = arg; |
732e2f37 | 131 | break; |
fa640ef1 | 132 | |
732e2f37 | 133 | case OPT_DISABLE_REPO: |
343d9736 | 134 | if (args->num_disable_repos >= MAX_REPOS) |
732e2f37 | 135 | return -ENOBUFS; |
fa640ef1 | 136 | |
343d9736 | 137 | args->disable_repos[args->num_disable_repos++] = arg; |
732e2f37 | 138 | break; |
fa640ef1 | 139 | |
732e2f37 MT |
140 | default: |
141 | return ARGP_ERR_UNKNOWN; | |
fa640ef1 MT |
142 | } |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
c87e1399 | 147 | static int cli_confirm(pakfire_ctx* ctx, pakfire_root* root, |
a6613775 MT |
148 | void* data, const char* message, const char* question) { |
149 | // Just print the message | |
150 | if (message) | |
151 | printf("%s\n", message); | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
fa640ef1 | 156 | int main(int argc, char* argv[]) { |
27886789 | 157 | pakfire_ctx* ctx = NULL; |
bfc9a02a MT |
158 | int r; |
159 | ||
160 | // Setup the context | |
55c00b97 | 161 | r = pakfire_ctx_create(&ctx, NULL); |
bfc9a02a MT |
162 | if (r) |
163 | goto ERROR; | |
164 | ||
1c9373cd MT |
165 | // Write logs to the console |
166 | pakfire_ctx_set_log_callback(ctx, pakfire_log_stderr, NULL); | |
167 | ||
a6613775 MT |
168 | // Setup confirm callback |
169 | pakfire_ctx_set_confirm_callback(ctx, cli_confirm, NULL); | |
170 | ||
10a25449 MT |
171 | // Setup progress callback |
172 | pakfire_ctx_set_progress_callback(ctx, cli_setup_progressbar, NULL); | |
173 | ||
41250642 MT |
174 | // Set the default cache path |
175 | r = pakfire_ctx_set_cache_path(ctx, "~/.cache/pakfire"); | |
176 | if (r) | |
177 | goto ERROR; | |
178 | ||
343d9736 | 179 | struct cli_global_args args = { |
bfc9a02a | 180 | .ctx = ctx, |
961d9b9e | 181 | .distro = cli_get_default_distro(ctx), |
fa640ef1 | 182 | .arch = NULL, |
c87e1399 | 183 | .flags = PAKFIRE_ROOT_FLAGS_BUILD, |
fa640ef1 | 184 | }; |
fa640ef1 | 185 | |
ee8a3a64 | 186 | // Parse the command line and run any commands |
9b8d2c6e | 187 | r = cli_parse(options, commands, args_doc, NULL, |
343d9736 | 188 | parse, CLI_REQUIRE_ROOT, argc, argv, &args); |
bfc9a02a MT |
189 | |
190 | ERROR: | |
d9a624a7 MT |
191 | if (ctx) { |
192 | ctx = pakfire_ctx_unref(ctx); | |
193 | if (ctx) { | |
194 | fprintf(stderr, "Context was not freed\n"); | |
195 | return 1; | |
196 | } | |
197 | } | |
bfc9a02a MT |
198 | |
199 | return r; | |
fa640ef1 | 200 | } |