From c199fde849ece144d3dbf996f7092464ab3dc59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Tue, 23 Jul 2024 12:36:43 +0200 Subject: [PATCH] feat: ci generation --- .builds/alpine.yml | 14 ++++++++++ src/cli/cli.c | 9 ++++-- src/cli/command/ci.c | 66 ++++++++++++++++++++++++++++++++++++++++++++ src/cli/command/ci.h | 1 + src/config.c | 1 + 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 .builds/alpine.yml create mode 100644 src/cli/command/ci.c create mode 100644 src/cli/command/ci.h diff --git a/.builds/alpine.yml b/.builds/alpine.yml new file mode 100644 index 0000000..1839391 --- /dev/null +++ b/.builds/alpine.yml @@ -0,0 +1,14 @@ +image: alpine/latest +packages: + - git +sources: + - "https://git.sr.ht/~hyro/gimi" +secrets: + - 55691174-b52f-477c-81ea-dd32deff19b8now +tasks: + - sync: | + set +x + git remote add gimi-github git@github.com:xhyrom/gimi.git + git push -f --all gimi-github + git push -f --tags gimi-github + set -x diff --git a/src/cli/cli.c b/src/cli/cli.c index b46fee1..46c730c 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -1,4 +1,5 @@ #include "../gimi_constants.h" +#include "command/ci.h" #include "command/config.h" #include "command/init.h" #include "command/provider.h" @@ -20,14 +21,18 @@ void cli_print_version() { int cli_handle(int argc, char **argv) { char *sub_command = argv[0]; - if (strcmp(sub_command, "init") == 0) { - return cli_command_init(argc, argv); + if (strcmp(sub_command, "ci") == 0) { + return cli_command_ci(argc, argv); } if (strcmp(sub_command, "config") == 0) { return cli_command_config(argc, argv); } + if (strcmp(sub_command, "init") == 0) { + return cli_command_init(argc, argv); + } + if (strcmp(sub_command, "provider") == 0) { return cli_command_provider(argc, argv); } diff --git a/src/cli/command/ci.c b/src/cli/command/ci.c new file mode 100644 index 0000000..46f4c53 --- /dev/null +++ b/src/cli/command/ci.c @@ -0,0 +1,66 @@ +#include "../../config.h" +#include +#include +#include + +#define SOURCEHUT \ + "image: alpine/latest\n" \ + "packages:\n" \ + " - git\n" \ + "sources:\n" \ + " - %s\n" \ + "secrets:\n" \ + " - \n" \ + "tasks:\n" \ + " - sync: |\n" \ + " %s\n" \ + " %s\n" \ + " %s\n" + +int generate(int argc, char **argv) { + struct gimi_config *cfg = config_read(); + struct gimi_config_provider *provider; + + if (argc == 2) { + provider = config_find_provider(cfg, argv[1]); + + if (provider == NULL) { + printf("error: no such provider '%s'", argv[1]); + return 1; + } + } else { + for (int i = 0; i < cfg->providers_size; i++) { + if (cfg->providers[i]->primary) { + provider = cfg->providers[i]; + break; + } + } + + if (provider == NULL) { + printf("error: can't find primary provider"); + return 1; + } + }; + + config_free(cfg); + return 0; +} + +int cli_command_ci(int argc, char **argv) { + if (argc == 1) { + printf("usage: gimi ci generate [provider]"); + return 1; + } + + // remove "ci" from args + argc -= 1; + argv += 1; + + char *subcommand = argv[0]; + + if (strcmp(subcommand, "generate") == 0) { + return generate(argc, argv); + } + + return 0; +} diff --git a/src/cli/command/ci.h b/src/cli/command/ci.h new file mode 100644 index 0000000..0f7da66 --- /dev/null +++ b/src/cli/command/ci.h @@ -0,0 +1 @@ +int cli_command_ci(int argc, char **argv); diff --git a/src/config.c b/src/config.c index e66db50..9a83c39 100644 --- a/src/config.c +++ b/src/config.c @@ -74,6 +74,7 @@ struct gimi_config_provider *config_find_provider(struct gimi_config *cfg, for (int i = 0; i < cfg->providers_size; i++) { if (strcmp(cfg->providers[i]->name, name) == 0) { provider = cfg->providers[i]; + break; } }