2023-11-24 18:47:23 +00:00
|
|
|
#include <ctype.h>
|
2023-11-22 21:39:27 +00:00
|
|
|
#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
|
|
|
|
2023-11-23 20:31:54 +00:00
|
|
|
#include "cJSON.h"
|
2023-11-22 21:39:27 +00:00
|
|
|
#include "config.h"
|
2023-11-25 15:55:02 +00:00
|
|
|
#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
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
static int run(const char *root_dir, const char *target) {
|
|
|
|
int retval = EXIT_FAILURE;
|
2023-11-25 16:15:30 +00:00
|
|
|
|
2023-11-23 20:31:54 +00:00
|
|
|
char *cwd = getcwd(0, 0);
|
2023-11-24 18:47:23 +00:00
|
|
|
if (!root_dir) {
|
|
|
|
root_dir = getenv("BOOTSTRAP_ROOT_DIR");
|
|
|
|
}
|
2023-11-23 20:31:54 +00:00
|
|
|
|
2023-11-25 15:55:02 +00:00
|
|
|
struct Error *error = 0;
|
2023-11-23 14:40:17 +00:00
|
|
|
struct Config *config = 0;
|
2023-11-25 15:55:02 +00:00
|
|
|
|
2023-11-25 18:51:42 +00:00
|
|
|
if ((error = config_new(cwd, root_dir, target, &config))) {
|
2023-11-25 15:55:02 +00:00
|
|
|
fprintf(stderr, "%s", error->message);
|
2023-11-24 18:47:23 +00:00
|
|
|
goto cleanup_cwd;
|
2023-11-22 21:39:27 +00:00
|
|
|
}
|
2023-11-22 18:15:12 +00:00
|
|
|
|
2023-11-23 20:31:54 +00:00
|
|
|
cJSON *parsed = 0;
|
2023-11-25 16:15:30 +00:00
|
|
|
if ((error = parse_spec_json(config, &parsed))) {
|
|
|
|
fprintf(stderr, "%s", error->message);
|
2023-11-24 18:47:23 +00:00
|
|
|
goto cleanup_config;
|
2023-11-23 20:31:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 17:27:44 +00:00
|
|
|
struct DynArray *prompts = 0;
|
2023-11-25 16:15:30 +00:00
|
|
|
if ((error = validate_spec_json(parsed, &prompts))) {
|
|
|
|
fprintf(stderr, "%s", error->message);
|
2023-11-24 18:47:23 +00:00
|
|
|
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))) {
|
2023-11-25 16:33:58 +00:00
|
|
|
if (error) {
|
|
|
|
fprintf(stderr, "%s", error->message);
|
|
|
|
}
|
2023-11-25 03:29:24 +00:00
|
|
|
goto cleanup_parsed;
|
|
|
|
}
|
2023-11-23 20:31:54 +00:00
|
|
|
|
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
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
cleanup_parsed:
|
2023-11-25 17:37:41 +00:00
|
|
|
cJSON_Delete(parsed);
|
2023-11-24 17:27:44 +00:00
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
cleanup_config:
|
2023-11-23 14:40:17 +00:00
|
|
|
config_free(config);
|
2023-11-23 20:31:54 +00:00
|
|
|
|
2023-11-24 18:47:23 +00:00
|
|
|
cleanup_cwd:
|
2023-11-23 20:31:54 +00:00
|
|
|
free(cwd);
|
2023-11-25 15:55:02 +00:00
|
|
|
error_free(error);
|
2023-11-25 16:33:58 +00:00
|
|
|
|
2023-11-23 20:31:54 +00:00
|
|
|
return retval;
|
2023-11-22 18:15:12 +00:00
|
|
|
}
|
2023-11-24 18:47:23 +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);
|
|
|
|
}
|