Skip to content

Commit

Permalink
Fix #2: -v -u can be used together
Browse files Browse the repository at this point in the history
  • Loading branch information
icy committed Aug 22, 2020
1 parent 9970cf2 commit 2b0a265
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ whose names match a predefined prefix/regexp.

### Supported options

* `-v` : Scan and output ocurrences of variables in the input
* `-v`: Scan and output all occurrences of variables in the input.
* `-u`: Raise error when environment variable is not set.
This option doesn't work when `-v` is used.
When being used with `-v`, the program scans through the whole
input; otherwise, the program stops immediately when there is
any undefined (environment) variable.
* `-p regexp`: Limit substitution to variables that match this prefix.
You can use some regular expression as prefix.
Default to `[^}]+`. Can be used as an alternative
Expand Down Expand Up @@ -67,7 +69,7 @@ To limit substitution to variables that match some prefix, use `-p` option:
The second command raises an error because the variable `TEST_VAR` matches
the expected prefix `TEST_` and its value is not set.

You can also specify exactly a few variables to be substituted
You can also specify exactly a few variables to be substituted
(which is exactly an alternative to the `shell-format` option
in the original GNU tool `envsubst`):

Expand All @@ -82,7 +84,7 @@ input argument `-p PREFIX`, the program will build the final regexp

1. Hence you can't use for example `-p '^FOO'`.
2. You can also easily trick the program with some fun `PREFIX` ;)
However, as seen in
However, as seen in
https://github.com/icy/genvsub/blob/33e68048c6fe4b6ca0befadbc9fa5c19055ede8b/sub.go#L42
the program enforces input data to follow the form `${VARIABLE_NAME}`.
I'm still thinking if we can allow more tricks here.
Expand Down Expand Up @@ -144,7 +146,7 @@ This tool may be an answer. By accepting not-so-many side-effects, we can easily
$ kustomize build \
| genvsub -u -p "(ENV_[_A-Z0-9]+)|STG_NAMESPACE|IMAGE_TAG|RESOURCE_PREFIX_" \
> output.yaml
$ if [[ $? -eq 0 ]]; then
$ if [[ $? -eq 0 ]]; then
kubectl apply -f- < output.yaml
fi
```
Expand Down
23 changes: 19 additions & 4 deletions sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import "flag"
var regVarname = regexp.MustCompile(`.+`)
var allVarSet = true
var lastProcessedVar = ""
var errors = 0

/* users' controllers */
var setMinusU bool
Expand Down Expand Up @@ -52,14 +53,19 @@ func repl_func(in []byte) []byte {
}

var_name := in_st[2 : len(in_st)-1]
var_set, var_val := lookUpEnv(var_name)
allVarSet = allVarSet && var_set
lastProcessedVar = var_name
return []byte(var_val)
}

func lookUpEnv(var_name string) (bool, string) {
var_val, var_set := os.LookupEnv(var_name)
if !var_set {
var_val = fmt.Sprintf("<%s::error::variable_unset>", var_name)
fmt.Fprintf(os.Stderr, "%s\n", var_val)
}
allVarSet = allVarSet && var_set
lastProcessedVar = var_name
return []byte(var_val)
return var_set, var_val
}

// https://github.com/jprichardson/readline-go/blob/master/readline.go
Expand Down Expand Up @@ -92,7 +98,12 @@ func doLine(line string) {
if scanOnly {
if found := scanLine(line); found != nil {
for _, v := range found {
fmt.Printf("%s\n", v[2:len(v)-1])
var_name := string(v[2 : len(v)-1])
var_set, _ := lookUpEnv(var_name)
fmt.Printf("%s\n", var_name)
if !var_set {
errors += 1
}
}
}
} else {
Expand All @@ -113,4 +124,8 @@ func main() {
regVarname = regexp.MustCompile(fmt.Sprintf(`\${(%s)}`, varPrefix))
fmt.Fprintf(os.Stderr, ":: genvsub is reading from STDIN and looking for variables with regexp '%s'\n", regVarname)
eachLine(os.Stdin, doLine)

if setMinusU && errors > 0 {
os.Exit(1)
}
}
2 changes: 1 addition & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ test_prefix_test_complexv() {
G_ERRORS="$?"
_grep "^JIRA_USER_NAME"
_grep "^JIRA_USER_PASSWORD"
[[ "$G_ERRORS" -eq 0 ]]
[[ "$G_ERRORS" -ge 1 ]]
}

test_change_prefix() {
Expand Down

0 comments on commit 2b0a265

Please sign in to comment.