comparison 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
comparison
equal deleted inserted replaced
53:319979427566 54:85c59fbf1407
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <stdio.h> 19 #include <stdio.h>
20 #include <stdlib.h> 20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
21 #include <unistd.h> 23 #include <unistd.h>
22 24
25 #include "db.h"
23 #include "http.h" 26 #include "http.h"
27 #include "log.h"
24 #include "scid.h" 28 #include "scid.h"
25 #include "util.h" 29 #include "util.h"
26 30
27 static void 31 static void
28 usage(void) 32 usage(void)
29 { 33 {
30 fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir]\n"); 34 fprintf(stderr, "usage: scid [-d database] [-s sock] [-t themedir] [command]\n");
31 exit(1); 35 exit(1);
32 } 36 }
37
38 static void
39 help(void)
40 {
41 fprintf(stderr, "usage: scid\n");
42 fprintf(stderr, " scid api-get\n");
43 fprintf(stderr, " scid api-reset\n");
44 exit(1);
45 }
46
47 static void
48 cmd_api_get(int argc, char **argv)
49 {
50 char key[SCID_API_KEY_MAX] = {0};
51
52 if (db_key_get(key, sizeof (key)) == 1)
53 puts(key);
54 }
55
56 static void
57 cmd_api_reset(int argc, char **argv)
58 {
59 char key[SCID_API_KEY_MAX] = {0};
60
61 if (db_key_init(key, sizeof (key)) == 0)
62 printf("new key: %s\n", key);
63 }
64
65 static const struct {
66 const char *name;
67 void (*exec)(int, char **);
68 } commands[] = {
69 { "api-get", cmd_api_get },
70 { "api-reset", cmd_api_reset },
71 { NULL, NULL }
72 };
33 73
34 int 74 int
35 main(int argc, char **argv) 75 main(int argc, char **argv)
36 { 76 {
37 int ch; 77 int ch;
55 } 95 }
56 96
57 argc -= optind; 97 argc -= optind;
58 argv += optind; 98 argv += optind;
59 99
60 scid_init(); 100 /* Always logs first. */
61 run(); 101 scid_init(SCID_INIT_LOG);
102 srand(time(NULL));
103
104 /* Check if there is a command provided, otherwise run as HTTP. */
105 if (argc == 0) {
106 scid_init(SCID_INIT_DATABASE | SCID_INIT_THEME);
107 log_info("scid: " VERSION);
108 run();
109 } else {
110 if (strcmp(argv[0], "help") == 0)
111 help();
112
113 for (size_t i = 0; commands[i].name; ++i) {
114 if (strcmp(commands[i].name, argv[0]) == 0) {
115 scid_init(SCID_INIT_DATABASE);
116 commands[i].exec(argc, argv);
117 goto over;
118 }
119 }
120
121 usage();
122 }
123
124 over:
62 scid_finish(); 125 scid_finish();
63 } 126 }