Skip to content

Commit

Permalink
Add envsubst package
Browse files Browse the repository at this point in the history
Forked from https://github.com/drone/envsubst

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Apr 7, 2024
1 parent cfa07c7 commit 03e96b4
Show file tree
Hide file tree
Showing 13 changed files with 2,499 additions and 0 deletions.
39 changes: 39 additions & 0 deletions envsubst/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# envsubst

`github.com/fluxcd/pkg/envsubst` is a Go package for expanding variables in a string using `${var}` syntax.
Includes support for bash string replacement functions.

This package is a fork of [drone/envsubst](https://github.com/drone/envsubst).

## Supported Functions

| __Expression__ | __Meaning__ |
|-------------------------------|---------------------------------------------------------------------|
| `${var}` | Value of `$var` |
| `${#var}` | String length of `$var` |
| `${var^}` | Uppercase first character of `$var` |
| `${var^^}` | Uppercase all characters in `$var` |
| `${var,}` | Lowercase first character of `$var` |
| `${var,,}` | Lowercase all characters in `$var` |
| `${var:n}` | Offset `$var` `n` characters from start |
| `${var:n:len}` | Offset `$var` `n` characters with max length of `len` |
| `${var#pattern}` | Strip shortest `pattern` match from start |
| `${var##pattern}` | Strip longest `pattern` match from start |
| `${var%pattern}` | Strip shortest `pattern` match from end |
| `${var%%pattern}` | Strip longest `pattern` match from end |
| `${var-default}` | If `$var` is not set, evaluate expression as `$default` |
| `${var:-default}` | If `$var` is not set or is empty, evaluate expression as `$default` |
| `${var=default}` | If `$var` is not set, evaluate expression as `$default` |
| `${var:=default}` | If `$var` is not set or is empty, evaluate expression as `$default` |
| `${var/pattern/replacement}` | Replace as few `pattern` matches as possible with `replacement` |
| `${var//pattern/replacement}` | Replace as many `pattern` matches as possible with `replacement` |
| `${var/#pattern/replacement}` | Replace `pattern` match with `replacement` from `$var` start |
| `${var/%pattern/replacement}` | Replace `pattern` match with `replacement` from `$var` end |

For a deeper reference, see [bash-hackers](https://wiki.bash-hackers.org/syntax/pe#case_modification) or [gnu pattern matching](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html).

## Unsupported Functions

* `${var+default}`
* `${var:?default}`
* `${var:+default}`
39 changes: 39 additions & 0 deletions envsubst/eval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2024 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Forked from: https://github.com/drone/envsubst
// MIT License
// Copyright (c) 2017 drone.io.

package envsubst

import "os"

// Eval replaces ${var} in the string based on the mapping function.
func Eval(s string, mapping func(string) string) (string, error) {
t, err := Parse(s)
if err != nil {
return s, err
}
return t.Execute(mapping)
}

// EvalEnv replaces ${var} in the string according to the values of the
// current environment variables. References to undefined variables are
// replaced by the empty string.
func EvalEnv(s string) (string, error) {
return Eval(s, os.Getenv)
}
248 changes: 248 additions & 0 deletions envsubst/eval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/*
Copyright 2024 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Forked from: https://github.com/drone/envsubst
// MIT License
// Copyright (c) 2017 drone.io.

package envsubst

import "testing"

// test cases sourced from tldp.org
// http://www.tldp.org/LDP/abs/html/parameter-substitution.html

func TestExpand(t *testing.T) {
var expressions = []struct {
params map[string]string
input string
output string
}{
// text-only
{
params: map[string]string{},
input: "abcdEFGH28ij",
output: "abcdEFGH28ij",
},
// length
{
params: map[string]string{"var01": "abcdEFGH28ij"},
input: "${#var01}",
output: "12",
},
// uppercase first
{
params: map[string]string{"var01": "abcdEFGH28ij"},
input: "${var01^}",
output: "AbcdEFGH28ij",
},
// uppercase
{
params: map[string]string{"var01": "abcdEFGH28ij"},
input: "${var01^^}",
output: "ABCDEFGH28IJ",
},
// lowercase first
{
params: map[string]string{"var01": "ABCDEFGH28IJ"},
input: "${var01,}",
output: "aBCDEFGH28IJ",
},
// lowercase
{
params: map[string]string{"var01": "ABCDEFGH28IJ"},
input: "${var01,,}",
output: "abcdefgh28ij",
},
// substring with position
{
params: map[string]string{"path_name": "/home/bozo/ideas/thoughts.for.today"},
input: "${path_name:11}",
output: "ideas/thoughts.for.today",
},
// substring with position and length
{
params: map[string]string{"path_name": "/home/bozo/ideas/thoughts.for.today"},
input: "${path_name:11:5}",
output: "ideas",
},
// default not used
{
params: map[string]string{"var": "abc"},
input: "${var=xyz}",
output: "abc",
},
// default used
{
params: map[string]string{},
input: "${var=xyz}",
output: "xyz",
},
{
params: map[string]string{"default_var": "foo"},
input: "something ${var=${default_var}}",
output: "something foo",
},
{
params: map[string]string{"default_var": "foo1"},
input: `foo: ${var=${default_var}-suffix}`,
output: "foo: foo1-suffix",
},
{
params: map[string]string{"default_var": "foo1"},
input: `foo: ${var=prefix${default_var}-suffix}`,
output: "foo: prefixfoo1-suffix",
},
{
params: map[string]string{},
input: "${var:=xyz}",
output: "xyz",
},
// replace suffix
{
params: map[string]string{"stringZ": "abcABC123ABCabc"},
input: "${stringZ/%abc/XYZ}",
output: "abcABC123ABCXYZ",
},
// replace prefix
{
params: map[string]string{"stringZ": "abcABC123ABCabc"},
input: "${stringZ/#abc/XYZ}",
output: "XYZABC123ABCabc",
},
// replace all
{
params: map[string]string{"stringZ": "abcABC123ABCabc"},
input: "${stringZ//abc/xyz}",
output: "xyzABC123ABCxyz",
},
// replace first
{
params: map[string]string{"stringZ": "abcABC123ABCabc"},
input: "${stringZ/abc/xyz}",
output: "xyzABC123ABCabc",
},
// delete shortest match prefix
{
params: map[string]string{"filename": "bash.string.txt"},
input: "${filename#*.}",
output: "string.txt",
},
{
params: map[string]string{"filename": "path/to/file"},
input: "${filename#*/}",
output: "to/file",
},
{
params: map[string]string{"filename": "/path/to/file"},
input: "${filename#*/}",
output: "path/to/file",
},
// delete longest match prefix
{
params: map[string]string{"filename": "bash.string.txt"},
input: "${filename##*.}",
output: "txt",
},
{
params: map[string]string{"filename": "path/to/file"},
input: "${filename##*/}",
output: "file",
},
{
params: map[string]string{"filename": "/path/to/file"},
input: "${filename##*/}",
output: "file",
},
// delete shortest match suffix
{
params: map[string]string{"filename": "bash.string.txt"},
input: "${filename%.*}",
output: "bash.string",
},
// delete longest match suffix
{
params: map[string]string{"filename": "bash.string.txt"},
input: "${filename%%.*}",
output: "bash",
},

// nested parameters
{
params: map[string]string{"var01": "abcdEFGH28ij"},
input: "${var=${var01^^}}",
output: "ABCDEFGH28IJ",
},
// escaped
{
params: map[string]string{"var01": "abcdEFGH28ij"},
input: "$${var01}",
output: "${var01}",
},
{
params: map[string]string{"var01": "abcdEFGH28ij"},
input: "some text ${var01}$${var$${var01}$var01${var01}",
output: "some text abcdEFGH28ij${var${var01}$var01abcdEFGH28ij",
},
{
params: map[string]string{"default_var": "foo"},
input: "something $${var=${default_var}}",
output: "something ${var=foo}",
},
// some common escaping use cases
{
params: map[string]string{"stringZ": "foo/bar"},
input: `${stringZ/\//-}`,
output: "foo-bar",
},
{
params: map[string]string{"stringZ": "foo/bar/baz"},
input: `${stringZ//\//-}`,
output: "foo-bar-baz",
},
// escape outside of expansion shouldn't be processed
{
params: map[string]string{"default_var": "foo"},
input: "\\\\something ${var=${default_var}}",
output: "\\\\something foo",
},
// substitute with a blank string
{
params: map[string]string{"stringZ": "foo.bar"},
input: `${stringZ/./}`,
output: "foobar",
},
}

for _, expr := range expressions {
t.Run(expr.input, func(t *testing.T) {
t.Logf(expr.input)
output, err := Eval(expr.input, func(s string) string {
return expr.params[s]
})
if err != nil {
t.Errorf("Want %q expanded but got error %q", expr.input, err)
}

if output != expr.output {
t.Errorf("Want %q expanded to %q, got %q",
expr.input,
expr.output,
output)
}
})
}
}
Loading

0 comments on commit 03e96b4

Please sign in to comment.