generated from bep/golibtemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
108 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
[](https://github.com/bep/golibtemplate/actions?query=workflow:Test) | ||
[](https://goreportcard.com/report/github.com/bep/golibtemplate) | ||
[](https://godoc.org/github.com/bep/golibtemplate) | ||
[](https://github.com/bep/helpers/actions?query=workflow:Test) | ||
[](https://goreportcard.com/report/github.com/bep/helpers) | ||
[](https://godoc.org/github.com/bep/helpers) | ||
|
||
Some helper packages with some helper code that I have had a tendency to copy from project to project over the years, a clear sign that I should consider some reuse. |
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,27 @@ | ||
package envhelpers | ||
|
||
import "strings" | ||
|
||
// SetEnvVars sets vars on the form key=value in the oldVars slice. | ||
func SetEnvVars(oldVars *[]string, keyValues ...string) { | ||
for i := 0; i < len(keyValues); i += 2 { | ||
setEnvVar(oldVars, keyValues[i], keyValues[i+1]) | ||
} | ||
} | ||
|
||
// SplitEnvVar splits an env var into key and value. | ||
func SplitEnvVar(v string) (string, string) { | ||
name, value, _ := strings.Cut(v, "=") | ||
return name, value | ||
} | ||
|
||
func setEnvVar(vars *[]string, key, value string) { | ||
for i := range *vars { | ||
if strings.HasPrefix((*vars)[i], key+"=") { | ||
(*vars)[i] = key + "=" + value | ||
return | ||
} | ||
} | ||
// New var. | ||
*vars = append(*vars, key+"="+value) | ||
} |
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 envhelpers | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestSetEnvVars(t *testing.T) { | ||
t.Parallel() | ||
c := qt.New(t) | ||
vars := []string{"FOO=bar", "HUGO=cool", "BAR=foo"} | ||
SetEnvVars(&vars, "HUGO", "rocking!", "NEW", "bar") | ||
c.Assert(vars, qt.DeepEquals, []string{"FOO=bar", "HUGO=rocking!", "BAR=foo", "NEW=bar"}) | ||
|
||
key, val := SplitEnvVar("HUGO=rocks") | ||
c.Assert(key, qt.Equals, "HUGO") | ||
c.Assert(val, qt.Equals, "rocks") | ||
} |
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 @@ | ||
package envhelpers | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var varRe = regexp.MustCompile(`\$\{(\w+)\}`) | ||
|
||
// Expand replaces ${var} (and only that) in the string based on the mapping function. | ||
// This signature is identical to the one in the Go standard library, | ||
// but has a more restrictive scope with less ambiguity. | ||
// The value inside ${} must match \w+ (see regexp.MatchString). | ||
func Expand(s string, mapping func(string) string) string { | ||
if !strings.Contains(s, "${") { | ||
return s | ||
} | ||
return varRe.ReplaceAllStringFunc(s, func(varName string) string { | ||
return mapping(varName[2 : len(varName)-1]) | ||
}) | ||
} |
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,24 @@ | ||
package envhelpers | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestExpand(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
m := func(s string) string { | ||
return s + "-expanded" | ||
} | ||
|
||
c.Assert(Expand("", m), qt.Equals, "") | ||
c.Assert(Expand("no vars", m), qt.Equals, "no vars") | ||
c.Assert(Expand("one var: ${myvar}.", m), qt.Equals, "one var: myvar-expanded.") | ||
c.Assert(Expand("two vars: first: ${first}, second ${second}.", m), qt.Equals, "two vars: first: first-expanded, second second-expanded.") | ||
c.Assert(Expand("with space: ${ myvar }.", m), qt.Equals, "with space: ${ myvar }.") | ||
c.Assert(Expand("with special char: ${myvar&}.", m), qt.Equals, "with special char: ${myvar&}.") | ||
c.Assert(Expand("not without brackets: $myvar", m), qt.Equals, "not without brackets: $myvar") | ||
c.Assert(Expand("multiline: ${first}\n\nanother: ${second}", m), qt.Equals, "multiline: first-expanded\n\nanother: second-expanded") | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
module github.com/bep/golibtemplate | ||
module github.com/bep/helpers | ||
|
||
go 1.18 | ||
|
||
require github.com/frankban/quicktest v1.14.3 | ||
|
||
require ( | ||
github.com/frankban/quicktest v1.14.2 // indirect | ||
github.com/google/go-cmp v0.5.7 // indirect | ||
github.com/google/go-cmp v0.5.8 // indirect | ||
github.com/kr/pretty v0.3.0 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/rogpeppe/go-internal v1.6.1 // indirect | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect | ||
github.com/rogpeppe/go-internal v1.8.1 // indirect | ||
) |
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= | ||
github.com/frankban/quicktest v1.14.2 h1:SPb1KFFmM+ybpEjPUhCCkZOM5xlovT5UbrMvWnXyBns= | ||
github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= | ||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= | ||
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= | ||
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= | ||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= | ||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= | ||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= | ||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= | ||
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= | ||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= | ||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= | ||
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= | ||
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= |
This file was deleted.
Oops, something went wrong.