Skip to content

Commit bec34e8

Browse files
Merge #3
3: Refactor package-naming and add env-util r=Jaskaranbir a=Jaskaranbir Co-authored-by: Jaskaranbir <jaskaranbir.dhillon@gmail.com>
2 parents 828e34d + 8daf424 commit bec34e8

12 files changed

Lines changed: 123 additions & 26 deletions

Gopkg.lock

Lines changed: 16 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This package provides some convenience utilities. New utilities will be added as
77

88
### Current Utilities:
99

10+
* **env**
11+
* [ValidateEnv][8]
12+
1013
* **errors**
1114
* [ErrorStackTrace][7]
1215

@@ -20,11 +23,12 @@ This package provides some convenience utilities. New utilities will be added as
2023
* [ParseHosts][5]
2124
* [StandardizeSpaces][6]
2225

23-
[0]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils
24-
[1]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#AreElementsInSlice
25-
[2]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#AreElementsInSliceStrict
26-
[3]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#IndexInSlice
27-
[4]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#IsElementInSlice
28-
[5]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#ParseHosts
29-
[6]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#StandardizeSpaces
30-
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#ErrorStackTrace
26+
[0]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil
27+
[1]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AreElementsInSlice
28+
[2]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AreElementsInSliceStrict
29+
[3]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#IndexInSlice
30+
[4]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#IsElementInSlice
31+
[5]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ParseHosts
32+
[6]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#StandardizeSpaces
33+
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ErrorStackTrace
34+
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ValidateEnv

commonutil/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package commonutil provides some commonly used
2+
// convenient utilities for TerrexTech.
3+
package commonutil

commonutil/envutil.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package commonutil
2+
3+
import (
4+
"errors"
5+
"os"
6+
)
7+
8+
// ValidateEnv checks if the provided environment variables are set to some value.
9+
// Otherwise the missing variable's name is returned along with an error.
10+
// If multiple variables are missing, this will return error on the first variable
11+
// which was not found.
12+
func ValidateEnv(envVars ...string) (string, error) {
13+
for _, varname := range envVars {
14+
envVar := os.Getenv(varname)
15+
if envVar == "" {
16+
err := errors.New(
17+
"Error while bootstrapping Cassandra table: " +
18+
"Following env-var is required but was not found: " +
19+
varname,
20+
)
21+
return varname, err
22+
}
23+
}
24+
return "", nil
25+
}

commonutil/envutil_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package commonutil
2+
3+
import (
4+
"os"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = Describe("ErrorUtils", func() {
11+
Describe("ValidateEnv", func() {
12+
var unsetEnv = func(envVars ...string) {
13+
for _, v := range envVars {
14+
os.Unsetenv(v)
15+
}
16+
}
17+
18+
It("should not return any error if all specified env-vars are set", func() {
19+
// Just trying not to override any existing env-vars
20+
envVars := []string{
21+
"v113451e1234",
22+
"v213451e1234",
23+
"v313451e1234",
24+
"v413451e1234",
25+
}
26+
27+
for _, v := range envVars {
28+
os.Setenv(v, v)
29+
}
30+
31+
varName, err := ValidateEnv(envVars...)
32+
Expect(err).ToNot(HaveOccurred())
33+
Expect(varName).To(BeEmpty())
34+
unsetEnv(envVars...)
35+
})
36+
37+
It("should return error if some env-vars is missing", func() {
38+
envVars := []string{
39+
"v113451e1234",
40+
"v213451e1234",
41+
"v513451e1234",
42+
}
43+
44+
for _, v := range envVars {
45+
os.Setenv(v, v)
46+
}
47+
48+
envVars = append(envVars, "v313451e1234")
49+
envVars = append(envVars, "v413451e1234")
50+
varName, err := ValidateEnv(envVars...)
51+
Expect(err).To(HaveOccurred())
52+
Expect(varName).To(Equal("v313451e1234"))
53+
unsetEnv(envVars...)
54+
})
55+
})
56+
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package commonutil
22

33
import "fmt"
44

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package commonutil
22

33
import (
44
"regexp"
@@ -15,12 +15,12 @@ var _ = Describe("ErrorUtils", func() {
1515
st := ErrorStackTrace(err)
1616

1717
// Sample string that passes this regex: "errorutils_test.go:16"
18-
rgx := regexp.MustCompile(`errorutils_test\.go:[0-9]+`)
18+
rgx := regexp.MustCompile(`errorutil_test\.go:[0-9]+`)
1919
match := rgx.FindString(st)
2020

2121
// Check if the error has regex match, then its a StackTrace
2222
// and our test is good.
23-
Expect(match != "").To(BeTrue())
23+
Expect(match).ToNot(BeEmpty())
2424
})
2525
})
2626
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package commonutil
22

33
import (
44
"reflect"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package commonutil
22

33
import (
44
. "github.com/onsi/ginkgo"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package commonutil
22

33
import (
44
"strings"

0 commit comments

Comments
 (0)