bootstrap/main.c

93 lines
1.8 KiB
C
Raw Normal View History

#include <ctype.h>
#include <stdio.h>
2023-11-22 20:43:34 +00:00
#include <stdlib.h>
2023-11-23 15:07:22 +00:00
#include <unistd.h>
2023-11-22 20:43:34 +00:00
#include "cJSON.h"
#include "config.h"
#include "error.h"
2023-11-25 03:29:24 +00:00
#include "evaluator.h"
2023-11-24 16:09:18 +00:00
#include "parser.h"
2023-11-24 17:27:44 +00:00
#include "validator.h"
2023-11-22 20:43:34 +00:00
static int run(const char *root_dir, const char *target) {
int retval = EXIT_FAILURE;
char *cwd = getcwd(0, 0);
if (!root_dir) {
root_dir = getenv("BOOTSTRAP_ROOT_DIR");
}
struct Error *error = 0;
2023-11-23 14:40:17 +00:00
struct Config *config = 0;
if ((error = config_new(cwd, root_dir, target, &config))) {
fprintf(stderr, "%s", error->message);
goto cleanup_cwd;
}
2023-11-22 18:15:12 +00:00
cJSON *parsed = 0;
if ((error = parse_spec_json(config, &parsed))) {
fprintf(stderr, "%s", error->message);
goto cleanup_config;
}
2023-11-24 17:27:44 +00:00
struct DynArray *prompts = 0;
2023-11-26 13:30:44 +00:00
if ((error = validate_spec_json(config, parsed, &prompts))) {
fprintf(stderr, "%s", error->message);
goto cleanup_parsed;
2023-11-24 17:27:44 +00:00
}
2023-11-25 22:25:01 +00:00
if ((retval = evaluate_runner(config, prompts, &error))) {
if (error) {
fprintf(stderr, "%s", error->message);
}
2023-11-25 03:29:24 +00:00
goto cleanup_parsed;
}
2023-11-24 19:23:52 +00:00
cleanup_prompts:
2023-11-25 17:37:41 +00:00
dyn_array_free(prompts);
2023-11-24 19:23:52 +00:00
cleanup_parsed:
2023-11-25 17:37:41 +00:00
cJSON_Delete(parsed);
2023-11-24 17:27:44 +00:00
cleanup_config:
2023-11-23 14:40:17 +00:00
config_free(config);
cleanup_cwd:
free(cwd);
error_free(error);
return retval;
2023-11-22 18:15:12 +00:00
}
int main(int argc, char **argv) {
char *root_dir = 0;
char *target = 0;
int opt;
while ((opt = getopt(argc, argv, "d:")) != -1) {
switch (opt) {
case 'd':
root_dir = optarg;
break;
}
}
for (int index = optind; index < argc; index++) {
if (target == 0) {
target = argv[index];
} else {
fprintf(stderr, "Usage: bootstrap [-d <ROOT_DIR>] <spec>\n");
return EXIT_FAILURE;
}
}
if (!target) {
fprintf(stderr, "Usage: bootstrap [-d <ROOT_DIR>] <spec>\n");
return EXIT_FAILURE;
}
return run(root_dir, target);
}