From bbe669b761db4cb03fca2dbf9a7890d5a7e120af Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Tue, 17 Mar 2020 00:52:38 -0500 Subject: hscript: Add new netconfigtype key and associated tests Not wired up to netaddress et al, but does parse properly. --- hscript/network.cc | 33 +++++++++++++++++++++++++++++++++ hscript/network.hh | 22 ++++++++++++++++++++++ hscript/script.cc | 3 +++ hscript/script_i.hh | 13 +++++++++++++ 4 files changed, 71 insertions(+) (limited to 'hscript') diff --git a/hscript/network.cc b/hscript/network.cc index 1750a60..a7b2bee 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -47,6 +47,39 @@ bool Network::execute() const { } +Key *NetConfigType::parseFromData(const std::string &data, int lineno, + int *errors, int *, const Script *script) { + std::string type = data; + ConfigSystem system; + + std::transform(type.cbegin(), type.cend(), type.begin(), ::tolower); + + if(type == "netifrc") { + system = Netifrc; + } else if(type == "eni") { + system = ENI; + } else { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netconfigtype: invalid or missing config type", + "one of 'netifrc', 'eni' required"); + return nullptr; + } + + return new NetConfigType(script, lineno, system); +} + +bool NetConfigType::validate() const { + /* Validation takes place during parsing. */ + return true; +} + +bool NetConfigType::execute() const { + /* This key doesn't perform any action by itself. */ + return true; +} + + Key *NetAddress::parseFromData(const std::string &data, int lineno, int *errors, int *, const Script *script) { long elements = std::count(data.cbegin(), data.cend(), ' ') + 1; diff --git a/hscript/network.hh b/hscript/network.hh index ec8d2cf..415a337 100644 --- a/hscript/network.hh +++ b/hscript/network.hh @@ -29,6 +29,28 @@ public: bool execute() const override; }; +class NetConfigType : public Key { +public: + enum ConfigSystem { + Netifrc, + ENI + }; +private: + ConfigSystem _sys; + + NetConfigType(const Script *_sc, int _line, const ConfigSystem _s) : + Key(_sc, _line), _sys(_s) {} +public: + static Key *parseFromData(const std::string &, int, int*, int*, + const Script *); + + /*! Retrieve the desired network configuration system. */ + ConfigSystem type() const { return this->_sys; } + + bool validate() const override; + bool execute() const override; +}; + class NetAddress : public Key { public: /*! Determines the type of address an interface will obtain. */ diff --git a/hscript/script.cc b/hscript/script.cc index 7db3ed0..fd9fc5a 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -49,6 +49,7 @@ const std::map valid_keys = { {"repository", &Repository::parseFromData}, {"signingkey", &SigningKey::parseFromData}, + {"netconfigtype", &NetConfigType::parseFromData}, {"netaddress", &NetAddress::parseFromData}, {"nameserver", &Nameserver::parseFromData}, {"netssid", &NetSSID::parseFromData}, @@ -78,6 +79,8 @@ bool Script::ScriptPrivate::store_key(const std::string &key_name, Key *obj, const ScriptOptions &opts) { if(key_name == "network") { return store_network(obj, lineno, errors, warnings, opts); + } else if(key_name == "netconfigtype") { + return store_netconfig(obj, lineno, errors, warnings, opts); } else if(key_name == "netaddress") { std::unique_ptr addr(dynamic_cast(obj)); this->addresses.push_back(std::move(addr)); diff --git a/hscript/script_i.hh b/hscript/script_i.hh index 0ed362e..a94a0d7 100644 --- a/hscript/script_i.hh +++ b/hscript/script_i.hh @@ -43,6 +43,8 @@ struct Script::ScriptPrivate { /*! Determines whether or not to enable networking. */ std::unique_ptr network; + /*! Determines the network configuration system to use. */ + std::unique_ptr netconfig; /*! The target system's hostname. */ std::unique_ptr hostname; /*! The packages to install to the target system. */ @@ -126,6 +128,17 @@ struct Script::ScriptPrivate { return true; } + bool store_netconfig(Key *obj, int line, int *errors, int *, ScriptOptions) { + if(netconfig) { + DUPLICATE_ERROR(netconfig, "netconfigtype", + netconfig->type()); + return false; + } + std::unique_ptr nc(dynamic_cast(obj)); + netconfig = std::move(nc); + return true; + } + bool store_hostname(Key* obj, int line, int *errors, int *, ScriptOptions) { if(hostname) { DUPLICATE_ERROR(hostname, "hostname", hostname->value()) -- cgit v1.2.3-70-g09d2