-
Notifications
You must be signed in to change notification settings - Fork 0
/
sedenv.cpp
87 lines (70 loc) · 2.14 KB
/
sedenv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <strings.h>
#include <vector>
using namespace std;
#define env_var_list vector<string>
string read_input();
env_var_list get_substituion_list(string config);
// env_var_name variables shouldn't have __xx__ wrappers
string get_env_value(string env_var_name);
string replace_text(string text, string env_var_name, string replace);
int main(int argc, char *argv[]) {
if (argc > 1) {
if (string(argv[1]) == "--help" || string(argv[1]) == "-h") {
cout << "https://github.com/philliptaylorpro/sedenv/blob/master/README.md" << endl;
return 0;
}
}
string config = read_input();
const env_var_list env_var_names = get_substituion_list(config);
for (env_var_list::const_iterator env_var_name = env_var_names.begin(); env_var_name != env_var_names.end(); ++env_var_name) {
clog << "ENV VAR SUBSTITUTION: " << *env_var_name << endl;
string env_var_value = get_env_value(*env_var_name);
config = replace_text(config, *env_var_name, env_var_value);
}
cout << config;
return 0;
}
string read_input() {
string input;
for (std::string line; std::getline(std::cin, line);) {
input += line;
input += '\n';
}
return input;
}
env_var_list get_substituion_list(string config) {
env_var_list retval;
char prev = '0';
bool inStr = false;
string collected;
for (std::string::iterator it = config.begin(); it != config.end(); ++it) {
if (*it == '_' && prev == '_') {
if (inStr) {
inStr = false;
collected.pop_back(); // __ENV_VAR__ collects as ENV_VAR_ without this due to two character flag.
retval.push_back(collected);
collected = "";
} else {
inStr = true;
collected = "";
}
} else {
if (inStr)
collected += *it;
}
prev = *it;
}
return retval;
}
string get_env_value(string env_var_name) {
const char *raw_value = std::getenv(env_var_name.c_str());
if (raw_value)
return string(raw_value);
else
return string("");
}
string replace_text(string text, string find, string replace) {
text.replace(text.find("__" + find + "__"), find.length() + 4, replace);
return text;
}