From: Michael Tremer Date: Sun, 6 Oct 2024 17:38:56 +0000 (+0000) Subject: cli: shell: Implement running commands via CLI X-Git-Tag: 0.9.30~1127 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=731ea9342e1a467c8446f3f0187a6f7cb9743d49;p=pakfire.git cli: shell: Implement running commands via CLI Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/shell.c b/src/cli/lib/shell.c index d5e7fe427..c9e6f3f4b 100644 --- a/src/cli/lib/shell.c +++ b/src/cli/lib/shell.c @@ -27,11 +27,16 @@ #include #include +#define MAX_ARGS 128 #define MAX_PACKAGES 128 struct config { int flags; + // Arguments + const char* argv[MAX_ARGS + 1]; + unsigned int argc; + // Packages const char* packages[MAX_PACKAGES + 1]; unsigned int num_packages; @@ -66,6 +71,13 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { config->packages[config->num_packages++] = arg; break; + case ARGP_KEY_ARG: + if (config->argc >= MAX_ARGS) + return -ENOBUFS; + + config->argv[config->argc++] = arg; + break; + default: return ARGP_ERR_UNKNOWN; } @@ -82,6 +94,8 @@ int cli_shell(void* data, int argc, char* argv[]) { struct config config = { .flags = PAKFIRE_BUILD_INTERACTIVE, + .argv = {}, + .argc = 0, .packages = {}, .num_packages = 0, }; @@ -110,8 +124,8 @@ int cli_shell(void* data, int argc, char* argv[]) { goto ERROR; } - // Run shell - r = pakfire_build_shell(build); + // Run the command + r = pakfire_build_shell(build, (config.argc) ? config.argv : NULL); ERROR: if (build) diff --git a/src/cli/pakfire-builder.c b/src/cli/pakfire-builder.c index adcabfde4..d1e40e945 100644 --- a/src/cli/pakfire-builder.c +++ b/src/cli/pakfire-builder.c @@ -78,7 +78,7 @@ static const struct command commands[] = { { "repo", cli_repo, -1, -1, 0 }, { "repolist", cli_repolist, 0, 0, 0 }, { "requires", cli_requires, 1, -1, 0 }, - { "shell", cli_shell, 0, 0, 0 }, + { "shell", cli_shell, 0, -1, 0 }, { "search", cli_search, 1, -1, 0 }, { NULL }, }; @@ -92,7 +92,7 @@ const char* args_doc = "repo compose PATH PACKAGES...\n" "repolist\n" "requires PATTERN\n" - "shell [OPTIONS...]\n" + "shell [OPTIONS...] [COMMAND...]\n" "search [OPTIONS...] PATTERN"; static error_t parse(int key, char* arg, struct argp_state* state, void* data) { diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 68330efc5..9d6dfc26c 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -1818,7 +1818,7 @@ static int pakfire_build_perform(struct pakfire_build* build, ERROR: // Drop to a shell for debugging if (pakfire_build_has_flag(build, PAKFIRE_BUILD_INTERACTIVE)) - pakfire_build_shell(build); + pakfire_build_shell(build, NULL); return r; } @@ -2453,7 +2453,7 @@ ERROR: /* Drops the user into a shell */ -PAKFIRE_EXPORT int pakfire_build_shell(struct pakfire_build* build) { +PAKFIRE_EXPORT int pakfire_build_shell(struct pakfire_build* build, const char* argv[]) { int r; // Initialize the build environment @@ -2466,6 +2466,10 @@ PAKFIRE_EXPORT int pakfire_build_shell(struct pakfire_build* build) { if (r) return r; - // Run shell + // Run the command (if given) + if (argv) + return pakfire_jail_exec(build->jail, argv, 0); + + // Otherwise run the shell return pakfire_jail_shell(build->jail); } diff --git a/src/libpakfire/include/pakfire/build.h b/src/libpakfire/include/pakfire/build.h index 5e4430594..ef4f6473e 100644 --- a/src/libpakfire/include/pakfire/build.h +++ b/src/libpakfire/include/pakfire/build.h @@ -48,7 +48,7 @@ void pakfire_build_set_log_callback(struct pakfire_build* build, int pakfire_build_set_ccache_path(struct pakfire_build* build, const char* path); int pakfire_build_set_target(struct pakfire_build* build, const char* target); -int pakfire_build_shell(struct pakfire_build* build); +int pakfire_build_shell(struct pakfire_build* build, const char* argv[]); int pakfire_build_exec(struct pakfire_build* build, const char* path);