Skip to content

Commit

Permalink
added more utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rayjlinden committed Apr 13, 2019
1 parent 5200d1e commit 540dbfc
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.7
0.3.0
2 changes: 0 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func MustLoad(filenames ...string) {
panic(err)
}
}
return
}

// Overload will read your env file(s) and load them into ENV for this process.
Expand Down Expand Up @@ -103,7 +102,6 @@ func MustOverload(filenames ...string) {
panic(err)
}
}
return
}

// Read all env (with same file loading semantics as Load) but return values as
Expand Down
4 changes: 2 additions & 2 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestLoadFileNotFound(t *testing.T) {
t.Error("File wasn't found but Load didn't return an error")
}

assert.Panics(t, func() { MustLoad("somefilethatwillneverexistever.env") }, "The code did not panic")
assert.Panics(t, func() { MustLoad("somefilethatwillneverexistever.env") }, "The code did not panic")
}

func TestOverloadFileNotFound(t *testing.T) {
Expand All @@ -73,7 +73,7 @@ func TestOverloadFileNotFound(t *testing.T) {
t.Error("File wasn't found but Overload didn't return an error")
}

assert.Panics(t, func() { MustOverload("somefilethatwillneverexistever.env") }, "The code did not panic")
assert.Panics(t, func() { MustOverload("somefilethatwillneverexistever.env") }, "The code did not panic")
}

func TestReadPlainEnv(t *testing.T) {
Expand Down
184 changes: 177 additions & 7 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package env

import (
"fmt"
"net/url"
"os"
"strconv"
"time"
)

// Set - sets an environment variable
Expand Down Expand Up @@ -64,7 +66,7 @@ func MustGetBool(key string) bool {
if err == nil {
return value
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be convert to boolean", key))
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to boolean", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
Expand Down Expand Up @@ -96,7 +98,7 @@ func MustGetInt(key string) int {
if err == nil {
return int(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be convert to int", key))
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to int", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
Expand Down Expand Up @@ -128,19 +130,19 @@ func MustGetUint(key string) uint {
if err == nil {
return uint(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be convert to uint", key))
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to uint", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}

// GetFloat - get an environment variable as float32
// GetFloat32 - get an environment variable as float32
func GetFloat32(key string) (float32, error) {
value, err := strconv.ParseFloat(os.Getenv(key), 32)
return float32(value), err
}

// GetOrFloat - get an environment variable or return default value if does not exist
// GetOrFloat32 - get an environment variable or return default value if does not exist
func GetOrFloat32(key string, defaultValue float32) float32 {
strValue, ok := os.LookupEnv(key)
if ok {
Expand All @@ -152,15 +154,183 @@ func GetOrFloat32(key string, defaultValue float32) float32 {
return defaultValue
}

// MustGetUFloat - get an environment variable or panic if does not exist
// MustGetUFloat32 - get an environment variable or panic if does not exist
func MustGetFloat32(key string) float32 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 32)
if err == nil {
return float32(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be convert to float32", key))
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to float32", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}

// GetFloat64 - get an environment variable as float64
func GetFloat64(key string) (float64, error) {
value, err := strconv.ParseFloat(os.Getenv(key), 64)
return float64(value), err
}

// GetOrFloat64 - get an environment variable or return default value if does not exist
func GetOrFloat64(key string, defaultValue float64) float64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 64)
if err == nil {
return float64(value)
}
}
return defaultValue
}

// MustGetUFloat64 - get an environment variable or panic if does not exist
func MustGetFloat64(key string) float64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseFloat(strValue, 64)
if err == nil {
return float64(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to float64", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}

// GetInt64 - get an environment variable as int64
func GetInt64(key string) (int64, error) {
value, err := strconv.ParseInt(os.Getenv(key), 10, 64)
return int64(value), err
}

// GetOrInt64 - get an environment variable or return default value if does not exist
func GetOrInt64(key string, defaultValue int64) int64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseInt(strValue, 10, 64)
if err == nil {
return int64(value)
}
}
return defaultValue
}

// MustGetInt64 - get an environment variable or panic if does not exist
func MustGetInt64(key string) int64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseInt(strValue, 10, 64)
if err == nil {
return int64(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to int64", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}

// GetUint64 - get an environment variable as uint
func GetUint64(key string) (uint64, error) {
value, err := strconv.ParseUint(os.Getenv(key), 10, 64)
return uint64(value), err
}

// GetOrUint64 - get an environment variable or return default value if does not exist
func GetOrUint64(key string, defaultValue uint64) uint64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseUint(strValue, 10, 64)
if err == nil {
return uint64(value)
}
}
return defaultValue
}

// MustGetUint64 - get an environment variable or panic if does not exist
func MustGetUint64(key string) uint64 {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := strconv.ParseUint(strValue, 10, 64)
if err == nil {
return uint64(value)
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to uint64", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}

// GetDuration - get an environment variable as time.Duration
func GetDuration(key string) (time.Duration, error) {
value, err := time.ParseDuration(os.Getenv(key))
return value, err
}

// GetOrDuration - get an environment variable or return default value if does not exist
func GetOrDuration(key string, defaultValue string) time.Duration {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := time.ParseDuration(strValue)
if err == nil {
return value
}
}
defaultDuration, err := time.ParseDuration(defaultValue)
if err != nil {
panic(fmt.Sprintf("default duration \"%s\" could not be converted to time.Duration", key))
}
return defaultDuration
}

// MustGetDuration - get an environment variable or panic if does not exist
func MustGetDuration(key string) time.Duration {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := time.ParseDuration(strValue)
if err == nil {
return value
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to time.Duration", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
}

// GetUrl - get an environment variable as url.URL
func GetUrl(key string) (*url.URL, error) {
value, err := url.ParseRequestURI(os.Getenv(key))
return value, err
}

// GetOrUrl - get an environment variable or return default value if does not exist
func GetOrUrl(key string, defaultValue string) *url.URL {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := url.ParseRequestURI(strValue)
if err == nil {
return value
}
}
defaultUrl, err := url.ParseRequestURI(defaultValue)
if err != nil {
panic(fmt.Sprintf("default duration \"%s\" could not be converted to url.URL", key))
}
return defaultUrl
}

// MustGetUrl - get an environment variable or panic if does not exist
func MustGetUrl(key string) *url.URL {
strValue, ok := os.LookupEnv(key)
if ok {
value, err := url.ParseRequestURI(strValue)
if err == nil {
return value
} else {
panic(fmt.Sprintf("environment variable \"%s\" could not be converted to url.URL", key))
}
}
panic(fmt.Sprintf("expected environment variable \"%s\" does not exist", key))
Expand Down
Loading

0 comments on commit 540dbfc

Please sign in to comment.