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