Mercurial > sci
diff scid/main.c @ 54:85c59fbf1407
scid: add commands to retrieve API key
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 17 Aug 2022 11:07:08 +0200 |
parents | 319979427566 |
children | 71cd8447e3a4 |
line wrap: on
line diff
--- a/scid/main.c Wed Aug 17 09:52:00 2022 +0200 +++ b/scid/main.c Wed Aug 17 11:07:08 2022 +0200 @@ -18,19 +18,59 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <time.h> #include <unistd.h> +#include "db.h" #include "http.h" +#include "log.h" #include "scid.h" #include "util.h" static void usage(void) { - fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir]\n"); + fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir] [command]\n"); + exit(1); +} + +static void +help(void) +{ + fprintf(stderr, "usage: scid\n"); + fprintf(stderr, " scid api-get\n"); + fprintf(stderr, " scid api-reset\n"); exit(1); } +static void +cmd_api_get(int argc, char **argv) +{ + char key[SCID_API_KEY_MAX] = {0}; + + if (db_key_get(key, sizeof (key)) == 1) + puts(key); +} + +static void +cmd_api_reset(int argc, char **argv) +{ + char key[SCID_API_KEY_MAX] = {0}; + + if (db_key_init(key, sizeof (key)) == 0) + printf("new key: %s\n", key); +} + +static const struct { + const char *name; + void (*exec)(int, char **); +} commands[] = { + { "api-get", cmd_api_get }, + { "api-reset", cmd_api_reset }, + { NULL, NULL } +}; + int main(int argc, char **argv) { @@ -57,7 +97,30 @@ argc -= optind; argv += optind; - scid_init(); - run(); + /* Always logs first. */ + scid_init(SCID_INIT_LOG); + srand(time(NULL)); + + /* Check if there is a command provided, otherwise run as HTTP. */ + if (argc == 0) { + scid_init(SCID_INIT_DATABASE | SCID_INIT_THEME); + log_info("scid: " VERSION); + run(); + } else { + if (strcmp(argv[0], "help") == 0) + help(); + + for (size_t i = 0; commands[i].name; ++i) { + if (strcmp(commands[i].name, argv[0]) == 0) { + scid_init(SCID_INIT_DATABASE); + commands[i].exec(argc, argv); + goto over; + } + } + + usage(); + } + +over: scid_finish(); }