Add SimpleConfig.
https://godocs.io/git.wntrmute.dev/kyle/goutils/config is one of the more useful Go packages in my standard go library that gets used for building services. I needed something similar for another Shimmering Clarity project, and thus I figured I'd add it into SCSL.
This commit is contained in:
81
test/config-explorer.cc
Normal file
81
test/config-explorer.cc
Normal file
@@ -0,0 +1,81 @@
|
||||
///
|
||||
/// \file test/config-explorer.cc
|
||||
/// \author K. Isom <kyle@imap.cc>
|
||||
/// \date 2023-10-21
|
||||
/// \brief Commandline tools for interacting with simple configurations.
|
||||
///
|
||||
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
||||
///
|
||||
/// Permission to use, copy, modify, and/or distribute this software for
|
||||
/// any purpose with or without fee is hereby granted, provided that
|
||||
/// the above copyright notice and this permission notice appear in all /// copies.
|
||||
///
|
||||
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
||||
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
/// PERFORMANCE OF THIS SOFTWARE.
|
||||
///
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <scsl/Flags.h>
|
||||
#include <scsl/SimpleConfig.h>
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int retc = 1;
|
||||
bool listKeys;
|
||||
std::string fileName;
|
||||
std::string prefix;
|
||||
std::string defaultValue;
|
||||
|
||||
auto *flags = new scsl::Flags("config-explorer",
|
||||
"interact with a simple configuration system");
|
||||
flags->Register("-d", "", "set a default value");
|
||||
flags->Register("-f", scsl::FlagType::String, "path to a configuration file");
|
||||
flags->Register("-l", false, "list cached keys at the end");
|
||||
flags->Register("-p", "CX_",
|
||||
"prefix for configuration environment variables");
|
||||
auto parsed = flags->Parse(argc, argv);
|
||||
if (parsed != scsl::Flags::ParseStatus::OK) {
|
||||
std::cerr << "Failed to parse flags: "
|
||||
<< scsl::Flags::ParseStatusToString(parsed) << "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
flags->GetString("-d", defaultValue);
|
||||
flags->GetString("-f", fileName);
|
||||
flags->GetBool("-l", listKeys);
|
||||
flags->GetString("-p", prefix);
|
||||
scsl::SimpleConfig::SetPrefixGlobal(prefix);
|
||||
|
||||
if (!fileName.empty()) {
|
||||
if (scsl::SimpleConfig::LoadGlobal(fileName) != 0) {
|
||||
std::cerr << "[!] failed to load " << fileName << "\n";
|
||||
return retc;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &key : flags->Args()) {
|
||||
auto val = scsl::SimpleConfig::GetGlobal(key, defaultValue);
|
||||
std::cout << key << ": " << val << "\n";
|
||||
}
|
||||
|
||||
if (listKeys) {
|
||||
std::cout << "[+] cached keys\n";
|
||||
for (auto &key : scsl::SimpleConfig::KeyListGlobal()) {
|
||||
std::cout << "\t- " << key << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
delete flags;
|
||||
return retc;
|
||||
}
|
||||
@@ -42,30 +42,30 @@ TestTrimming(std::string line, std::string lExpected, std::string rExpected, std
|
||||
std::string result;
|
||||
std::string message;
|
||||
|
||||
result = string::TrimLeadingWhitespaceDup(line);
|
||||
result = scstring::TrimLeadingWhitespaceDup(line);
|
||||
message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'";
|
||||
sctest::Assert(result == lExpected, message);
|
||||
|
||||
result = string::TrimTrailingWhitespaceDup(line);
|
||||
result = scstring::TrimTrailingWhitespaceDup(line);
|
||||
message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'";
|
||||
sctest::Assert(result == rExpected, message);
|
||||
|
||||
result = string::TrimWhitespaceDup(line);
|
||||
result = scstring::TrimWhitespaceDup(line);
|
||||
message = "TrimDup(\"" + line + "\"): '" + result + "'";
|
||||
sctest::Assert(result == expected, message);
|
||||
|
||||
result = line;
|
||||
string::TrimLeadingWhitespace(result);
|
||||
scstring::TrimLeadingWhitespace(result);
|
||||
message = "TrimLeadingDup(\"" + line + "\"): '" + result + "'";
|
||||
sctest::Assert(result == lExpected, message);
|
||||
|
||||
result = line;
|
||||
string::TrimTrailingWhitespace(result);
|
||||
scstring::TrimTrailingWhitespace(result);
|
||||
message = "TrimTrailingDup(\"" + line + "\"): '" + result + "'";
|
||||
sctest::Assert(result == rExpected, message);
|
||||
|
||||
result = line;
|
||||
string::TrimWhitespace(result);
|
||||
scstring::TrimWhitespace(result);
|
||||
message = "TrimDup(\"" + line + "\"): '" + result + "'";
|
||||
sctest::Assert(result == expected, message);
|
||||
}
|
||||
@@ -75,7 +75,7 @@ std::function<bool()>
|
||||
TestSplit(std::string line, std::string delim, size_t maxCount, std::vector<std::string> expected)
|
||||
{
|
||||
return [line, delim, maxCount, expected]() {
|
||||
return string::SplitN(line, delim, maxCount) == expected;
|
||||
return scstring::SplitN(line, delim, maxCount) == expected;
|
||||
|
||||
};
|
||||
}
|
||||
@@ -86,7 +86,7 @@ TestSplitChar()
|
||||
{
|
||||
auto expected = std::vector<std::string>{"hello", "world"};
|
||||
const auto *inputLine = "hello=world\n";
|
||||
auto actual = string::SplitKeyValuePair(inputLine, '=');
|
||||
auto actual = scstring::SplitKeyValuePair(inputLine, '=');
|
||||
|
||||
return actual == expected;
|
||||
}
|
||||
@@ -109,11 +109,11 @@ TestWrapping()
|
||||
"hope so.",
|
||||
};
|
||||
|
||||
auto wrapped = string::WrapText(testLine, 16);
|
||||
auto wrapped = scstring::WrapText(testLine, 16);
|
||||
if (wrapped.size() != expected.size()) {
|
||||
std::cerr << string::VectorToString(wrapped)
|
||||
std::cerr << scstring::VectorToString(wrapped)
|
||||
<< " != "
|
||||
<< string::VectorToString(expected)
|
||||
<< scstring::VectorToString(expected)
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ TestWrapping()
|
||||
return false;
|
||||
}
|
||||
|
||||
// string::WriteTabIndented(std::cout, wrapped, 4, true);
|
||||
// scstring::WriteTabIndented(std::cout, wrapped, 4, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -149,7 +149,6 @@ main(int argc, char *argv[])
|
||||
if (parsed != scsl::Flags::ParseStatus::OK) {
|
||||
std::cerr << "Failed to parse flags: "
|
||||
<< scsl::Flags::ParseStatusToString(parsed) << "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sctest::SimpleSuite suite;
|
||||
|
||||
Reference in New Issue
Block a user