Skip to content

Commit

Permalink
Add command line parameters to read and write file
Browse files Browse the repository at this point in the history
  • Loading branch information
navrocky committed Feb 11, 2025
1 parent 9843c2b commit 8d8be95
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ sudo chmod +x /usr/local/bin/muenvsubst
## Usage

```
Substitutes environment variables using one of the templating engines, as
envsubst does.
Substitutes environment variables using Inja templating engine, as envsubst
does.
USAGE: ./muenvsubst [ -h, --help <arg> ] [ -V, --version ]
USAGE: ./muenvsubst [ -h, --help <arg> ] [ -i, --in <arg> ] [ -o,
--out <arg> ] [ -v, --version ]
OPTIONAL:
-h, --help <arg> Print this help.
-V, --version Output version information and exit
-i, --in <arg> Input file
-o, --out <arg> Output file
-v, --version Output version information and exit
```

## Inja syntax
Expand Down
24 changes: 17 additions & 7 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@ 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");
cmd.addHelp(true, argv[0], "Substitutes environment variables using Inja templating engine, as envsubst does.");
cmd.addArgWithFlagAndName('v', "version", false, false, "Output version information and exit");
cmd.addArgWithFlagAndName('i', "in", true, false, "Input file");
cmd.addArgWithFlagAndName('o', "out", true, false, "Output file");
try {
cmd.parse();

if (cmd.isDefined("-V")) {
if (cmd.isDefined("-v")) {
cout << APP_VERSION << endl;
return 0;
}

auto tmpl = readAllInput();
cout << renderWithInja(tmpl, envp);

string tmpl;
if (cmd.isDefined("-i")) {
tmpl = readFile(cmd.value("-i"));
} else {
tmpl = readAllInput();
}
auto out = renderWithInja(tmpl, envp);
if (cmd.isDefined("-o")) {
writeFile(cmd.value("-o"), out);
} else {
cout << out;
}
} catch (const HelpHasBeenPrintedException&) {
return 0;
} catch (const BaseException& x) {
Expand Down
29 changes: 29 additions & 0 deletions lib/tools.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "tools.h"

#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>

using namespace std;

Expand All @@ -25,6 +28,32 @@ std::string readAllInput()
return string(it, end);
}

std::string readFile(const string& file)
{
try {
if (!filesystem::exists(file))
throw runtime_error("File does not exists");
ifstream ifs(file, ios::in);
ifs.exceptions(ifs.exceptions() | ios::failbit | ios::badbit);
stringstream ss;
ss << ifs.rdbuf();
return ss.str();
} catch (const exception& e) {
throw runtime_error((stringstream() << "Cannot read file: \"" << file << "\". Error: " << e.what()).str());
}
}

void writeFile(const std::string& file, const std::string& content)
{
try {
ofstream ofs(file, ios::out);
ofs.exceptions(ofs.exceptions() | ios::failbit | ios::badbit);
ofs << content;
} catch (const exception& e) {
throw runtime_error((stringstream() << "Cannot write file: \"" << file << "\". Error: " << e.what()).str());
}
}

vector<string> stringSplit(const string& s, const string& delimiter)
{
vector<string> res;
Expand Down
2 changes: 2 additions & 0 deletions lib/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <vector>

std::string readAllInput();
std::string readFile(const std::string& file);
void writeFile(const std::string& file, const std::string& content);

struct KeyValue {
std::string key;
Expand Down

0 comments on commit 8d8be95

Please sign in to comment.