#include "config.h" #include "log.h" #include "run.h" #include "util/array.h" #include "util/fs.h" #include "util/string.h" #include #include #include #include #if HAVE_STDBOOL_H #include #endif #if HAVE_STDINT_H #include #endif struct sk_options { const char *conf_root; bool dry_run; const char *init_manifest; unsigned int jobs; uint8_t verbosity; const char *target_host; }; static const char *EXECNAME = PACKAGE; /* default */ /* help */ void sk_print_usage(FILE *outstream) { fprintf( outstream, "usage: %s [-h] [-n] [-v] [-V] [-j JOBS] [-i MANIFEST] target_host\n", EXECNAME); } void sk_print_version(FILE *outstream) { fprintf(outstream, "%s %s\n", PACKAGE, VERSION); } void sk_print_help(FILE *outstream) { /* usage */ sk_print_usage(outstream); fprintf(outstream, "\n"); /* version */ sk_print_version(outstream); fprintf(outstream, "\n"); /* positional arguments */ fprintf( outstream, "positional arguments:\n" " target_host host to configure\n" "\n"); /* optional arguments */ fprintf( outstream, "optional arguments:\n" " -i path to the manifest to be used or '-' to read from stdin\n" " -j number of jobs to execute objects in parallel\n" " -h show this help and exit\n" " -n dry run: do not execute generated code\n" " -v increase verbosity (repeat for more verbosity)\n" " -V output version information and exit\n" "\n"); } /* argument parsing */ static int sk_get_global_opts( int argc, char *argv[], struct sk_options *options) { /** * Parse skonfig options. * Will update the `options` struct. * * Options defined to print some information and exit the program will * already do so in this function, in which case the function will never * return. * * @returns true if no errors occurred during parsing, false otherwise. */ extern char *optarg; extern int opterr, optind, optopt; opterr = 0; int c; unsigned int posind = 1, errors = 0; bool eoo = false; /* end of options */ while (optind < argc) { while (!eoo && (c = getopt(argc, argv, ":i:j:hnvV")) != -1) { switch (c) { case 'i': options->init_manifest = optarg; break; case 'j': { unsigned int j; if ('-' == optarg[0] || 1 > (j = atoi(optarg))) { fprintf(stderr, "-j must be at least 1. Assuming -j 1.\n"); j = 1; } options->jobs = j; break; } case 'h': sk_print_help(stdout); exit(EXIT_SUCCESS); case 'n': options->dry_run = true; break; case 'v': options->verbosity++; break; case 'V': sk_print_version(stdout); exit(EXIT_SUCCESS); case ':': fprintf(stderr, "%s: option requires an argument -- %c\n", EXECNAME, optopt); errors++; break; case '?': default: fprintf(stderr, "%s: illegal option -- %c\n", EXECNAME, optopt); errors++; break; } } if (0 == strcmp("--", argv[optind-1])) { eoo = true; } /* handle positional argument */ switch (posind) { case 1: /* target host */ options->target_host = argv[optind]; /* XXX: strcpy() needed? */ break; default: fprintf(stderr, "%s: unrecognized positional argument: %s\n", EXECNAME, argv[optind]); errors++; break; } posind++; optind++; }; if (errors) { sk_print_usage(stderr); return false; } return true; } /* config */ SK_NONNULL(1) void sk_init_config(struct sk_config *config, const char *config_file) { FILE *fh; if (NULL != config_file) { /* Parse config file (as given by option) */ #if DEBUG printf("processing config file (opt): %s\n", config_file); #endif fh = fopen(config_file, "r"); if (fh) { sk_config_parse_file(config, fh); fclose(fh); } else { sk_error("invalid config file: %s", config_file); exit(EXIT_FAILURE); } } else if ((config_file = sk_config_find())) { /* found a config file in default location */ #if DEBUG printf("processing config file (found): %s\n", config_file); #endif fh = fopen(config_file, "r"); if (NULL == fh) { sk_error("could not open config file (%s): %s", config_file, strerror(errno)); } else { sk_config_parse_file(config, fh); fclose(fh); } free((char *)config_file); } sk_config_parse_env(config); } int main(int argc, char *argv[]) { int exit_status = EXIT_SUCCESS; EXECNAME = argv[0]; #if DEBUG printf("Debugging enabled.\n"); #endif /* determine absolute path to ~/.skonfig */ char default_conf_root[PATH_MAX+1] = ""; (void)strncpy(default_conf_root, myhome(), arraylen(default_conf_root)-1); strncat(default_conf_root, "/.skonfig", (arraylen(default_conf_root) - strlen(default_conf_root))); struct sk_options options = { /* defaults */ .conf_root = (const char *)default_conf_root, .dry_run = false, .init_manifest = "manifest/init", .jobs = 1, .verbosity = 0, .target_host = NULL }; sk_get_global_opts(argc, argv, &options); /* check if conf_root exists */ if (!is_dir(options.conf_root)) { sk_error("cannot find configuration directory."); perror(options.conf_root); /* is_dir sets errno */ /* TODO: Add link to Getting Started guide */ return EXIT_FAILURE; } /* Read in skonfig config */ struct sk_config *config = sk_config_alloc(); if (NULL == config) { sk_error("could not allocate memory to store config data"); return EXIT_FAILURE; } sk_init_config(config, NULL); /* update config from command line arguments */ free(config->global.init_manifest); config->global.init_manifest = strdup(options.init_manifest); config->global.jobs = options.jobs; config->global.verbosity_level += options.verbosity; if (config->global.verbosity_level > SK_LOG_MAX) config->global.verbosity_level = SK_LOG_MAX; sk_log_level_set(config->global.verbosity_level); switch (config->global.coloured_output) { case SK_COLOUR_NEVER: sk_log_font_set(SK_LOG_MONOCHROME); break; case SK_COLOUR_ALWAYS: sk_log_font_set(SK_LOG_COLOURED); break; case SK_COLOUR_AUTO: if (isatty(fileno(stdout))) { sk_log_font_set(SK_LOG_COLOURED); } else { sk_log_font_set(SK_LOG_MONOCHROME); } break; } #if DEBUG sk_config_print(config, stdout); /* DEBUG */ #endif sk_debug("conf dir: %s", options.conf_root); sk_debug("target host: %s", options.target_host); exit_status = sk_run_start( options.target_host, options.dry_run, config); sk_config_free(config); return exit_status; }