From c6b0e5fc81d905f57688c696f80e480712f612ab Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Thu, 2 Apr 2020 05:08:04 -0500 Subject: image: Significantly refactor how backends are registered --- image/creator.cc | 64 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'image/creator.cc') diff --git a/image/creator.cc b/image/creator.cc index ce688fa..70571fa 100644 --- a/image/creator.cc +++ b/image/creator.cc @@ -13,7 +13,7 @@ #include #include /* EXIT_* */ #include -#include "backends/backends.hh" +#include "backends/basic.hh" #include "hscript/script.hh" #include "util/output.hh" @@ -28,9 +28,13 @@ bool pretty = true; /*! Controls ASCII colour output */ /*! Entry-point for the image creation utility. */ int main(int argc, char *argv[]) { using namespace boost::program_options; + using namespace Horizon::Image; + bool needs_help{}, disable_pretty{}; int exit_code = EXIT_SUCCESS; - std::string if_path{"/etc/horizon/installfile"}, ir_dir{"/target"}, type_code{"tar"}; + std::string if_path{"/etc/horizon/installfile"}, ir_dir{"/target"}, + output_path{"image.tar"}, type_code{"tar"}; + BasicBackend *backend = nullptr; Horizon::ScriptOptions opts; Horizon::Script *my_script; @@ -89,9 +93,9 @@ int main(int argc, char *argv[]) { if(type_code == "list") { std::cout << "Type codes known by this build of Image Creation:" << std::endl << std::endl; - for(auto &backend : Horizon::Image::known_backends) { - std::cout << std::setw(10) << std::left << backend.type_code - << backend.description << std::endl; + for(const auto &candidate : BasicBackend::available_backends()) { + std::cout << std::setw(10) << std::left << candidate.type_code + << candidate.description << std::endl; } return EXIT_SUCCESS; } @@ -100,6 +104,27 @@ int main(int argc, char *argv[]) { if_path = vm["installfile"].as(); } + if(!vm["ir-dir"].empty()) { + ir_dir = vm["ir-dir"].as(); + } + + if(!vm["output"].empty()) { + output_path = vm["output"].as(); + } + + /* Load the proper backend. */ + for(const auto &candidate : BasicBackend::available_backends()) { + if(candidate.type_code == type_code) { + backend = candidate.creation_fn(ir_dir, output_path); + break; + } + } + if(backend == nullptr) { + output_error("command-line", "unsupported backend or internal error", + type_code); + return EXIT_FAILURE; + } + opts.set(Horizon::InstallEnvironment); opts.set(Horizon::ImageOnly); @@ -112,15 +137,34 @@ int main(int argc, char *argv[]) { /* if an error occurred during parsing, bail out now */ if(!my_script) { - return EXIT_FAILURE; - } + exit_code = EXIT_FAILURE; + goto early_trouble; + } else { + int ret; - if(!vm["ir-dir"].empty()) { - my_script->setTargetDirectory(vm["ir-dir"].as()); + if(!my_script->execute()) { + exit_code = EXIT_FAILURE; + goto trouble; + } + +#define RUN_PHASE_OR_TROUBLE(_PHASE, _FRIENDLY) \ + ret = backend->_PHASE();\ + if(ret != 0) {\ + output_error("internal", "error during output " _FRIENDLY,\ + std::to_string(ret));\ + exit_code = EXIT_FAILURE;\ + goto trouble;\ } - my_script->execute(); + RUN_PHASE_OR_TROUBLE(prepare, "preparation"); + RUN_PHASE_OR_TROUBLE(create, "creation"); + RUN_PHASE_OR_TROUBLE(finalise, "finalisation"); + } +trouble: /* delete the Script and exit */ delete my_script; +early_trouble: /* no script yet */ + delete backend; + return exit_code; } -- cgit v1.2.3-60-g2f50