diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-08 21:19:13 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-08 21:19:13 -0500 |
commit | 7c1efb0e68e7decd2d5276e7ff02f65bde9bbf9a (patch) | |
tree | 4d11018a7dda0c5ba6b1c1e6bb1eaa86de6db026 /hscript | |
parent | be1cc09981d24c94a07f08c615e66e3a261294af (diff) | |
download | horizon-7c1efb0e68e7decd2d5276e7ff02f65bde9bbf9a.tar.gz horizon-7c1efb0e68e7decd2d5276e7ff02f65bde9bbf9a.tar.bz2 horizon-7c1efb0e68e7decd2d5276e7ff02f65bde9bbf9a.tar.xz horizon-7c1efb0e68e7decd2d5276e7ff02f65bde9bbf9a.zip |
hscript: Initial attempt at handling 'pkginstall' key
Diffstat (limited to 'hscript')
-rw-r--r-- | hscript/meta.cc | 31 | ||||
-rw-r--r-- | hscript/meta.hh | 12 | ||||
-rw-r--r-- | hscript/script.cc | 23 |
3 files changed, 58 insertions, 8 deletions
diff --git a/hscript/meta.cc b/hscript/meta.cc index 83c6269..f333f0b 100644 --- a/hscript/meta.cc +++ b/hscript/meta.cc @@ -19,8 +19,7 @@ using namespace Horizon::Keys; Key *Hostname::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { std::regex valid_re("[A-Za-z0-9.]*"); - bool valid = std::regex_match(data, valid_re); - if(!valid) { + if(!std::regex_match(data, valid_re)) { if(errors) *errors += 1; output_error("installfile:" + std::to_string(lineno), "hostname: expected machine or DNS name", @@ -39,3 +38,31 @@ bool Hostname::execute() const { /* Write the hostname to /etc/hostname in the target environment. */ return false; } + + +Key *PkgInstall::parseFromData(const std::string data, int lineno, int *errors, + int *warnings) { + std::regex valid_pkg("[0-9A-Za-z_-]*((>?<|[<>]?=|[~>])[0-9A-Za-z-_.]+)?"); + std::string next_pkg; + std::istringstream stream(data); + std::set<std::string> all_pkgs; + + while(stream >> next_pkg) { + if(!std::regex_match(next_pkg, valid_pkg)) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "pkginstall: expected package name", + "'" + next_pkg + "' is not a valid package or atom"); + return nullptr; + } + if(all_pkgs.find(next_pkg) != all_pkgs.end()) { + if(warnings) *warnings += 1; + output_warning("installfile:" + std::to_string(lineno), + "pkginstall: package '" + next_pkg + + "' is already in the target package set"); + continue; + } + all_pkgs.insert(next_pkg); + } + return new PkgInstall(lineno, all_pkgs); +} diff --git a/hscript/meta.hh b/hscript/meta.hh index 626ce91..6515259 100644 --- a/hscript/meta.hh +++ b/hscript/meta.hh @@ -14,6 +14,7 @@ #define __HSCRIPT_META_HH_ #include <string> +#include <set> #include "key.hh" namespace Horizon { @@ -33,6 +34,17 @@ public: }; class PkgInstall : public Key { +private: + const std::set<std::string> _pkgs; + PkgInstall(int _line, const std::set<std::string> my_pkgs) : Key(_line), + _pkgs(my_pkgs) {} +public: + static Key *parseFromData(const std::string data, int lineno, int *errors, + int *warnings); + const std::set<std::string> packages() const { return _pkgs; } + bool validate() const override { return true; } + bool execute() const override { return true; } + }; class Language : public Key { diff --git a/hscript/script.cc b/hscript/script.cc index 5ff7cb2..709fbaf 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -14,6 +14,7 @@ #include <fstream> #include <iostream> #include <map> +#include <set> #include "script.hh" #include "disk.hh" @@ -71,7 +72,7 @@ struct Script::ScriptPrivate { /*! The target system's hostname. */ std::unique_ptr<Horizon::Keys::Hostname> hostname; /*! The packages to install to the target system. */ - std::vector<std::string> packages; + std::set<std::string> packages; /*! The root shadow line. */ std::unique_ptr<Horizon::Keys::RootPassphrase> rootpw; /*! Target system's mountpoints. */ @@ -82,16 +83,18 @@ struct Script::ScriptPrivate { * @param key_obj The Key object associated with the key. * @param errors Output parameter: if given, incremented on error. * @param warnings Output parameter: if given, incremented on warning. + * @param opts Script parsing options. */ bool store_key(const std::string key_name, Keys::Key *key_obj, int lineno, - int *errors, int *warnings) { + int *errors, int *warnings, ScriptOptions opts) { #define DUPLICATE_ERROR(OBJ, KEY, OLD_VAL) \ std::string err_str("previous value was ");\ err_str += OLD_VAL;\ err_str += " at installfile:" + std::to_string(OBJ->lineno());\ if(errors) *errors += 1;\ output_error("installfile:" + std::to_string(lineno),\ - "duplicate value for key '" + KEY + "'", err_str); + "duplicate value for key '" + KEY + "'", err_str,\ + opts.test(Pretty)); if(key_name == "network") { if(this->network) { @@ -116,8 +119,16 @@ struct Script::ScriptPrivate { this->hostname = std::move(name); return true; } else if(key_name == "pkginstall") { - /*! TODO: implement */ - return false; + Keys::PkgInstall *install = dynamic_cast<Keys::PkgInstall *>(key_obj); + for(auto &pkg : install->packages()) { + if(opts.test(StrictMode) && packages.find(pkg) != packages.end()) { + output_warning("installfile:" + std::to_string(lineno), + "package '" + pkg + "' has already been specified", + "", opts.test(Pretty)); + } + packages.insert(pkg); + } + return true; } else if(key_name == "rootpw") { /*! TODO: implement */ return false; @@ -215,7 +226,7 @@ const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { } if(!the_script->internal->store_key(key, key_obj, lineno, &errors, - &warnings)) { + &warnings, opts)) { PARSER_ERROR("stopping due to prior errors") continue; } |