]> git.ipfire.org Git - pakfire.git/commitdiff
cli: shell: Implement running commands via CLI
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Oct 2024 17:38:56 +0000 (17:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 6 Oct 2024 17:38:56 +0000 (17:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/shell.c
src/cli/pakfire-builder.c
src/libpakfire/build.c
src/libpakfire/include/pakfire/build.h

index d5e7fe42747cb6de0f83fd0a0f95971645a08a64..c9e6f3f4b8a3478964231962cbc70ef0944d94de 100644 (file)
 #include <pakfire/build.h>
 #include <pakfire/pakfire.h>
 
+#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)
index adcabfde4034bc83f50e8cfceb78b0c012d6ec86..d1e40e945a88c364d16a179912d5adb69fb278b5 100644 (file)
@@ -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) {
index 68330efc5d698af77693dcb7f566b37b41b91ba0..9d6dfc26c17a3cba67a32bbc91baaca29aea6ca6 100644 (file)
@@ -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);
 }
index 5e44305948edb966c4c2b994eb16c485d0f17291..ef4f6473e4d6cedd220796dc07f99c08be05916a 100644 (file)
@@ -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);