-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add varToBool function
- Loading branch information
Showing
16 changed files
with
159 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
set(TARGET muenvsubst) | ||
|
||
find_package(args-parser REQUIRED) | ||
find_package(inja REQUIRED) | ||
|
||
set(SOURCES | ||
inja_renderer.cpp | ||
inja_renderer.h | ||
main.cpp | ||
tools.cpp | ||
tools.h | ||
) | ||
|
||
add_executable(${TARGET} ${SOURCES}) | ||
|
||
target_link_libraries(${TARGET} | ||
args-parser::args-parser | ||
pantor::inja | ||
) | ||
|
||
include(GNUInstallDirs) | ||
install(TARGETS ${TARGET} | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "inja_renderer.h" | ||
|
||
#include <inja/inja.hpp> | ||
#include <sstream> | ||
#include <stdexcept> | ||
|
||
#include "tools.h" | ||
|
||
using namespace std; | ||
using namespace std::placeholders; | ||
|
||
inline void toLower(string& res) | ||
{ | ||
transform(res.begin(), res.end(), res.begin(), [](unsigned char c) { return std::tolower(c); }); | ||
} | ||
|
||
inline void ltrim(std::string& s) | ||
{ | ||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); | ||
} | ||
|
||
inline void rtrim(std::string& s) | ||
{ | ||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end()); | ||
} | ||
|
||
inja::json split(const inja::Arguments& args) | ||
{ | ||
if (args.size() != 2) | ||
throw runtime_error("Expected 2 parameters: split(s: string, delimiter: string): string[]"); | ||
auto s = args[0]->get<string>(); | ||
auto delimiter = args[1]->get<string>(); | ||
return stringSplit(s, delimiter); | ||
} | ||
|
||
inja::json varToBool(const inja::Arguments& args, inja::json envs) | ||
{ | ||
if (args.size() != 1) | ||
throw runtime_error("Expected 1 parameters: toBool(varName: string): bool"); | ||
auto name = args[0]->get<string>(); | ||
auto val = envs[name]; | ||
if (val.is_null()) | ||
return false; | ||
if (val.is_boolean()) | ||
return val.get<bool>(); | ||
if (val.is_string()) { | ||
auto s = val.get<string>(); | ||
toLower(s); | ||
ltrim(s); | ||
rtrim(s); | ||
return s == "true" || s == "yes" || s == "on" || s == "1"; | ||
} | ||
throw runtime_error((stringstream() << "Unsupported env value type for name " << name).str()); | ||
} | ||
|
||
inja::json error(const inja::Arguments& args) | ||
{ | ||
if (args.size() != 1) | ||
throw runtime_error("Expected 1 parameters: error(message: string)"); | ||
auto message = args[0]->get<string>(); | ||
throw runtime_error(message); | ||
} | ||
|
||
string renderWithInja(const string& tmpl, char** envp) | ||
{ | ||
auto envs = getAllEnvs<inja::json>(envp); | ||
inja::Environment env; | ||
env.add_callback("split", bind(split, _1)); | ||
env.add_callback("error", bind(error, _1)); | ||
env.add_callback("varToBool", [&](const inja::Arguments& args) { return varToBool(args, envs); }); | ||
return env.render(tmpl, envs); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <args-parser/all.hpp> | ||
|
||
#include "inja_renderer.h" | ||
#include "tools.h" | ||
|
||
using namespace std; | ||
using namespace std::placeholders; | ||
using namespace Args; | ||
|
||
const char* APP_VERSION = "1.2.0"; | ||
|
||
int main(int argc, char** argv, char** envp) | ||
{ | ||
bool showVersion = false; | ||
CmdLine cmd(argc, argv); | ||
cmd.addHelp( | ||
true, argv[0], "Substitutes environment variables using one of the templating engines, as envsubst does."); | ||
cmd.addArgWithFlagAndName('V', "version", false, false, "Output version information and exit"); | ||
try { | ||
cmd.parse(); | ||
|
||
if (cmd.isDefined("-V")) { | ||
cout << "1.2.0" << endl; | ||
return 0; | ||
} | ||
|
||
auto tmpl = readAllInput(); | ||
cout << renderWithInja(tmpl, envp); | ||
|
||
} catch (const HelpHasBeenPrintedException&) { | ||
return 0; | ||
} catch (const BaseException& x) { | ||
cerr << x.desc() << "\n"; | ||
return 1; | ||
} catch (const exception& x) { | ||
cerr << x.what() << "\n"; | ||
return 1; | ||
} | ||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
[requires] | ||
args-parser/6.3.3 | ||
mbits-mstch/1.0.4 | ||
inja/3.4.0 | ||
|
||
[generators] | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.