Skip to content

Commit

Permalink
Merge #3
Browse files Browse the repository at this point in the history
3: Refactor package-naming and add env-util r=Jaskaranbir a=Jaskaranbir



Co-authored-by: Jaskaranbir <jaskaranbir.dhillon@gmail.com>
  • Loading branch information
ninja-bruh and Jaskaranbir committed Sep 13, 2018
2 parents 828e34d + 8daf424 commit bec34e8
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 26 deletions.
23 changes: 16 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This package provides some convenience utilities. New utilities will be added as

### Current Utilities:

* **env**
* [ValidateEnv][8]

* **errors**
* [ErrorStackTrace][7]

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

[0]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils
[1]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#AreElementsInSlice
[2]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#AreElementsInSliceStrict
[3]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#IndexInSlice
[4]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#IsElementInSlice
[5]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#ParseHosts
[6]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#StandardizeSpaces
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/utils#ErrorStackTrace
[0]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil
[1]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AreElementsInSlice
[2]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#AreElementsInSliceStrict
[3]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#IndexInSlice
[4]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#IsElementInSlice
[5]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ParseHosts
[6]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#StandardizeSpaces
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ErrorStackTrace
[7]: https://godoc.org/github.com/TerrexTech/go-commonutils/commonutil#ValidateEnv
3 changes: 3 additions & 0 deletions commonutil/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package commonutil provides some commonly used
// convenient utilities for TerrexTech.
package commonutil
25 changes: 25 additions & 0 deletions commonutil/envutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package commonutil

import (
"errors"
"os"
)

// ValidateEnv checks if the provided environment variables are set to some value.
// Otherwise the missing variable's name is returned along with an error.
// If multiple variables are missing, this will return error on the first variable
// which was not found.
func ValidateEnv(envVars ...string) (string, error) {
for _, varname := range envVars {
envVar := os.Getenv(varname)
if envVar == "" {
err := errors.New(
"Error while bootstrapping Cassandra table: " +
"Following env-var is required but was not found: " +
varname,
)
return varname, err
}
}
return "", nil
}
56 changes: 56 additions & 0 deletions commonutil/envutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package commonutil

import (
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ErrorUtils", func() {
Describe("ValidateEnv", func() {
var unsetEnv = func(envVars ...string) {
for _, v := range envVars {
os.Unsetenv(v)
}
}

It("should not return any error if all specified env-vars are set", func() {
// Just trying not to override any existing env-vars
envVars := []string{
"v113451e1234",
"v213451e1234",
"v313451e1234",
"v413451e1234",
}

for _, v := range envVars {
os.Setenv(v, v)
}

varName, err := ValidateEnv(envVars...)
Expect(err).ToNot(HaveOccurred())
Expect(varName).To(BeEmpty())
unsetEnv(envVars...)
})

It("should return error if some env-vars is missing", func() {
envVars := []string{
"v113451e1234",
"v213451e1234",
"v513451e1234",
}

for _, v := range envVars {
os.Setenv(v, v)
}

envVars = append(envVars, "v313451e1234")
envVars = append(envVars, "v413451e1234")
varName, err := ValidateEnv(envVars...)
Expect(err).To(HaveOccurred())
Expect(varName).To(Equal("v313451e1234"))
unsetEnv(envVars...)
})
})
})
2 changes: 1 addition & 1 deletion utils/errorutils.go → commonutil/errorutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import "fmt"

Expand Down
6 changes: 3 additions & 3 deletions utils/errorutils_test.go → commonutil/errorutil_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import (
"regexp"
Expand All @@ -15,12 +15,12 @@ var _ = Describe("ErrorUtils", func() {
st := ErrorStackTrace(err)

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

// Check if the error has regex match, then its a StackTrace
// and our test is good.
Expect(match != "").To(BeTrue())
Expect(match).ToNot(BeEmpty())
})
})
})
2 changes: 1 addition & 1 deletion utils/sliceutils.go → commonutil/sliceutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import (
"reflect"
Expand Down
2 changes: 1 addition & 1 deletion utils/sliceutils_test.go → commonutil/sliceutil_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import (
. "github.com/onsi/ginkgo"
Expand Down
2 changes: 1 addition & 1 deletion utils/strutils.go → commonutil/strutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion utils/strutils_test.go → commonutil/strutil_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import (
. "github.com/onsi/ginkgo"
Expand Down
6 changes: 3 additions & 3 deletions utils/utils_suite_test.go → commonutil/util_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package commonutil

import (
"testing"
Expand All @@ -7,7 +7,7 @@ import (
. "github.com/onsi/gomega"
)

func TestUtils(t *testing.T) {
func TestUtil(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils Suite")
RunSpecs(t, "Util Suite")
}

0 comments on commit bec34e8

Please sign in to comment.