1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2023 Pakfire development team #
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. #
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. #
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/>. #
19 #############################################################################*/
28 #include <pakfire/pakfire.h>
29 #include <pakfire/string.h>
31 #include "lib/build.h"
32 #include "lib/clean.h"
33 #include "lib/command.h"
36 #include "lib/provides.h"
38 #include "lib/repolist.h"
39 #include "lib/requires.h"
40 #include "lib/shell.h"
41 #include "lib/search.h"
42 #include "lib/terminal.h"
43 #include "lib/version.h"
53 const char* enable_repos
[MAX_REPOS
];
54 unsigned int num_enable_repos
;
55 const char* disable_repos
[MAX_REPOS
];
56 unsigned int num_disable_repos
;
59 static int cli_main(struct pakfire
* pakfire
, int argc
, char* argv
[]) {
60 static const struct command commands
[] = {
61 { "build", 0, cli_build
},
62 { "clean", 0, cli_clean
},
63 { "dist", 0, cli_dist
},
64 { "info", 0, cli_info
},
65 { "provides", 0, cli_provides
},
66 { "repo", 0, cli_repo
},
67 { "repolist", 0, cli_repolist
},
68 { "requires", 0, cli_requires
},
69 { "shell", 0, cli_shell
},
70 { "search", 0, cli_search
},
74 return command_dispatch(pakfire
, commands
, argc
, argv
);
77 static void help(void) {
79 "%s [OPTIONS...] COMMAND\n\n"
81 " -a --arch Run in different architecture\n"
82 " --distro Selects the distribution to compile for\n"
83 " --debug Run in debug mode\n"
84 " -h --help Show help\n"
85 " --version Show version\n"
88 " search Search for packages\n"
89 " provides Shows which package provides a dependency\n"
90 " requires Shows which package requries a dependency\n"
91 " repolist Show all configured repositories\n"
92 " clean Cleans up no longer needed resources\n",
93 program_invocation_short_name
99 static int parse_argv(struct config
* config
, int argc
, char* argv
[]) {
110 static const struct option options
[] = {
111 { "arch", required_argument
, NULL
, 'a' },
112 { "debug", no_argument
, NULL
, ARG_DEBUG
},
113 { "distro", required_argument
, NULL
, ARG_DISTRO
},
114 { "help", no_argument
, NULL
, 'h' },
115 { "version", no_argument
, NULL
, ARG_VERSION
},
118 { "enable-repo", required_argument
, NULL
, ARG_ENABLE_REPO
},
119 { "disable-repo", required_argument
, NULL
, ARG_DISABLE_REPO
},
125 c
= getopt_long(argc
, argv
, "ah", options
, NULL
);
131 config
->arch
= optarg
;
138 config
->flags
|= PAKFIRE_FLAGS_DEBUG
;
142 config
->distro
= optarg
;
149 case ARG_ENABLE_REPO
:
150 if (config
->num_enable_repos
>= MAX_REPOS
)
153 config
->enable_repos
[config
->num_enable_repos
++] = optarg
;
156 case ARG_DISABLE_REPO
:
157 if (config
->num_disable_repos
>= MAX_REPOS
)
160 config
->disable_repos
[config
->num_disable_repos
++] = optarg
;
174 static void cli_set_repo_enabled(struct pakfire
* pakfire
, const char* name
, int enabled
) {
175 struct pakfire_repo
* repo
= NULL
;
177 // Find the repository
178 repo
= pakfire_get_repo(pakfire
, name
);
180 // Ignore if the repository could not be found
185 pakfire_repo_set_enabled(repo
, enabled
);
186 pakfire_repo_unref(repo
);
189 static FILE* open_distro_config(const char* distro
) {
194 // XXX hard-coded path
197 r
= snprintf(path
, sizeof(path
), "/etc/pakfire/distros/%s.conf", distro
);
201 // Open the configuration file
202 f
= fopen(path
, "r");
206 fprintf(stderr
, "Could not find distro '%s': %m\n", distro
);
217 static int setup_pakfire(const struct config
* config
, struct pakfire
** pakfire
) {
218 struct pakfire
* p
= NULL
;
222 // Open the distro configuration
223 f
= open_distro_config(config
->distro
);
229 // Initialize Pakfire
230 r
= pakfire_create(&p
, NULL
, config
->arch
, f
, config
->flags
, NULL
, NULL
);
234 // Enable repositories
235 for (unsigned int i
= 0; i
< config
->num_enable_repos
; i
++)
236 cli_set_repo_enabled(p
, config
->enable_repos
[i
], 1);
238 // Disable repositories
239 for (unsigned int i
= 0; i
< config
->num_disable_repos
; i
++)
240 cli_set_repo_enabled(p
, config
->disable_repos
[i
], 0);
252 int main(int argc
, char* argv
[]) {
253 struct pakfire
* pakfire
= NULL
;
256 struct config config
= {
257 // XXX hard-coded distro
260 .flags
= PAKFIRE_FLAGS_BUILD
,
263 // Parse command line arguments
264 r
= parse_argv(&config
, argc
, argv
);
269 r
= setup_pakfire(&config
, &pakfire
);
274 r
= cli_main(pakfire
, argc
- optind
+ 1, argv
+ optind
- 1);
278 pakfire_unref(pakfire
);