-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #526 from rsteube/context-envsubst
context: envsubst
- Loading branch information
Showing
13 changed files
with
1,835 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 drone.io | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.