Skip to content

Commit 99f8357

Browse files
committed
Add a --greedy-defaults option
1 parent 3839a66 commit 99f8357

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "envsub"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Stephen Connolly <[email protected]>"]
55

66
[dependencies]

README.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The program pipes stdin to stdout one line at a time applying the required subst
2020
+
2121
NOTE: If your suffix starts with `:` or `-` you will not have a good time as this will conflict with the default value separator that is internal to the pattern.
2222

23+
`-g`, `--greedy-defaults`:: Enables greedy replacement of unmatched default values. With this option if you have `${FOO}` and there is no corresponding variable `FOO` then that will remain untouched but `${FOO:-BAR}` or `${FOO-BAR}` will be replaced as `BAR` whereas without this option they would remain untouched.
24+
2325
`-v`, `--var`:: Only replace the named variable. _Can be specified multiple times_ if you want to replace multiple variables. If not specified then all variables from the environment can be substituted.
2426

2527
`-V`, `--version`:: Display the program version and exit.

src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn create_options() -> Options {
2626
let mut opts = Options::new();
2727
opts.optflag("h", "help", "print this help menu and exit");
2828
opts.optflag("V", "version", "print the version and exit");
29+
opts.optflag("g", "greedy-defaults", "allow expansion of undefined variable defaults");
2930
opts.optmulti(
3031
"v",
3132
"variable",
@@ -84,6 +85,20 @@ fn main() {
8485
Some(v) => v,
8586
None => "}".to_string(),
8687
};
88+
let greedy = match matches.opt_present("g") {
89+
true => Some(
90+
Regex::new(
91+
format!(
92+
r#"{}[a-zA-Z_][a-zA-Z0-9_]*:?-(.*?){}"#,
93+
regex::escape(prefix.clone().as_str()),
94+
regex::escape(suffix.clone().as_str())
95+
)
96+
.as_str(),
97+
)
98+
.unwrap()
99+
),
100+
false => None,
101+
};
87102
let vars = match matches.opt_present("v") {
88103
true => {
89104
let mut vars = HashMap::new();
@@ -178,6 +193,13 @@ fn main() {
178193
})
179194
.to_string();
180195
}
196+
out = match greedy.clone() {
197+
Some(regex) => regex.replace_all(
198+
out.as_str(),
199+
|caps: &Captures| caps.get(1).map_or("", |m| m.as_str())
200+
).to_string(),
201+
None => out,
202+
};
181203
writer.write(out.as_bytes()).unwrap();
182204
writer.write("\n".as_bytes()).unwrap();
183205
writer.flush().unwrap();

0 commit comments

Comments
 (0)