bootstrap/main.c

67 lines
1.6 KiB
C
Raw Normal View History

#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 "loader.h"
2023-11-22 20:43:34 +00:00
2023-11-23 18:02:40 +00:00
const char *ENV_BOOTSTRAP_ROOT_DIR = "BOOTSTRAP_ROOT_DIR";
2023-11-23 11:10:04 +00:00
2023-11-22 18:15:12 +00:00
int main(int argc, char **argv) {
2023-11-22 20:43:34 +00:00
int num = 0;
if (argc != 2) {
2023-11-23 18:02:40 +00:00
fprintf(stderr, "Usage: bootstrap <spec>\n");
2023-11-22 20:43:34 +00:00
exit(EXIT_FAILURE);
}
int retval = EXIT_SUCCESS;
char *cwd = getcwd(0, 0);
2023-11-23 18:02:40 +00:00
const char *root_dir = getenv(ENV_BOOTSTRAP_ROOT_DIR);
2023-11-23 15:07:22 +00:00
const char *target = argv[1];
2023-11-23 11:10:04 +00:00
// `cwd` must be free'd.
2023-11-23 14:40:17 +00:00
struct Config *config = 0;
2023-11-23 15:07:22 +00:00
switch (config_load(cwd, root_dir, target, &config)) {
case CE_ENV_CWD_INVALID:
fprintf(stderr, "Could not retrieve the $CWD value.");
retval = EXIT_FAILURE;
goto cwd_cleanup;
2023-11-23 18:02:40 +00:00
case CE_ENV_ROOT_DIR_INVALID:
fprintf(stderr, "Must specify $BOOTSTRAP_ROOT_DIR environment variable.");
retval = EXIT_FAILURE;
goto cwd_cleanup;
2023-11-23 15:07:22 +00:00
case CE_TARGET_INVALID:
fprintf(stderr, "Target spec `%s` is invalid.", argv[1]);
retval = EXIT_FAILURE;
goto cwd_cleanup;
}
2023-11-22 18:15:12 +00:00
// `config` must be free'd.
cJSON *parsed = 0;
switch (read_spec_json(config, &parsed)) {
case SJE_JSON_CANNOT_OPEN:
fprintf(stderr, "Found `spec.json` but could not open.");
retval = EXIT_FAILURE;
goto config_cleanup;
case SJE_JSON_INVALID:
fprintf(stderr, "`spec.json` does not conform to bootstrap format.");
retval = EXIT_FAILURE;
goto config_cleanup;
}
// TODO: Extract the prompts out of the `spec.json` file.
// TODO: Load in the curses interface.
// TODO: Run `run.sh`.
config_cleanup:
2023-11-23 14:40:17 +00:00
config_free(config);
cwd_cleanup:
free(cwd);
return retval;
2023-11-22 18:15:12 +00:00
}