From e835dafe1a05ccefb402e30fb673f2b48834fd98 Mon Sep 17 00:00:00 2001 From: Tordarus Date: Thu, 20 Jan 2022 12:23:20 +0100 Subject: [PATCH] initial commit --- go.mod | 3 + slices.go | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ vars.go | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 go.mod create mode 100644 slices.go create mode 100644 vars.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f8c2cf5 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.tordarus.net/Tordarus/envvars + +go 1.17 diff --git a/slices.go b/slices.go new file mode 100644 index 0000000..d145a5d --- /dev/null +++ b/slices.go @@ -0,0 +1,162 @@ +package envvars + +import ( + "os" + "strconv" + "strings" +) + +func StringSlice(key, sep string) []string { + if v, ok := os.LookupEnv(key); ok { + return strings.Split(v, sep) + } + return []string{} +} + +func ByteSlice(key, sep string) []byte { + return []byte(String(key, "")) +} + +func RuneSlice(key, sep string) []rune { + return []rune(String(key, "")) +} + +func IntSlice(key, sep string) []int { + res := make([]int, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseInt(s, 10, 64); err != nil { + res = append(res, int(v)) + } + } + return res +} + +func Int8Slice(key, sep string) []int8 { + res := make([]int8, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseInt(s, 10, 64); err != nil { + res = append(res, int8(v)) + } + } + return res +} + +func Int16Slice(key, sep string) []int16 { + res := make([]int16, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseInt(s, 10, 64); err != nil { + res = append(res, int16(v)) + } + } + return res +} + +func Int32Slice(key, sep string) []int32 { + res := make([]int32, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseInt(s, 10, 64); err != nil { + res = append(res, int32(v)) + } + } + return res +} + +func Int64Slice(key, sep string) []int64 { + res := make([]int64, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseInt(s, 10, 64); err != nil { + res = append(res, v) + } + } + return res +} + +func Uint8Slice(key, sep string) []uint8 { + res := make([]uint8, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseUint(s, 10, 64); err != nil { + res = append(res, uint8(v)) + } + } + return res +} + +func Uint16Slice(key, sep string) []uint16 { + res := make([]uint16, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseUint(s, 10, 64); err != nil { + res = append(res, uint16(v)) + } + } + return res +} + +func Uint32Slice(key, sep string) []uint32 { + res := make([]uint32, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseUint(s, 10, 64); err != nil { + res = append(res, uint32(v)) + } + } + return res +} + +func Uint64Slice(key, sep string) []uint64 { + res := make([]uint64, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseUint(s, 10, 64); err != nil { + res = append(res, v) + } + } + return res +} + +func Float32Slice(key, sep string) []float32 { + res := make([]float32, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseFloat(s, 64); err != nil { + res = append(res, float32(v)) + } + } + return res +} + +func Float64Slice(key, sep string) []float64 { + res := make([]float64, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseFloat(s, 64); err != nil { + res = append(res, v) + } + } + return res +} + +func Complex64Slice(key, sep string) []complex64 { + res := make([]complex64, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseComplex(s, 64); err != nil { + res = append(res, complex64(v)) + } + } + return res +} + +func Complex128Slice(key, sep string) []complex128 { + res := make([]complex128, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseComplex(s, 64); err != nil { + res = append(res, v) + } + } + return res +} + +func BoolSlice(key, sep string) []bool { + res := make([]bool, 0) + for _, s := range StringSlice(key, sep) { + if v, err := strconv.ParseBool(s); err != nil { + res = append(res, v) + } + } + return res +} diff --git a/vars.go b/vars.go new file mode 100644 index 0000000..658dfdd --- /dev/null +++ b/vars.go @@ -0,0 +1,150 @@ +package envvars + +import ( + "os" + "strconv" +) + +func String(key, defaultValue string) string { + if v, ok := os.LookupEnv(key); ok { + return v + } + return defaultValue +} + +func Byte(key string, defaultValue byte) byte { + return Uint8(key, defaultValue) +} + +func Rune(key string, defaultValue rune) rune { + for _, rn := range String(key, string(defaultValue)) { + return rn + } + return defaultValue +} + +func Int(key string, defaultValue int) int { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseInt(v, 10, 64); err != nil { + return int(v2) + } + } + return defaultValue +} + +func Int8(key string, defaultValue int8) int8 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseInt(v, 10, 64); err != nil { + return int8(v2) + } + } + return defaultValue +} + +func Int16(key string, defaultValue int16) int16 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseInt(v, 10, 64); err != nil { + return int16(v2) + } + } + return defaultValue +} + +func Int32(key string, defaultValue int32) int32 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseInt(v, 10, 64); err != nil { + return int32(v2) + } + } + return defaultValue +} + +func Int64(key string, defaultValue int64) int64 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseInt(v, 10, 64); err != nil { + return v2 + } + } + return defaultValue +} + +func Uint8(key string, defaultValue uint8) uint8 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseUint(v, 10, 64); err != nil { + return uint8(v2) + } + } + return defaultValue +} + +func Uint16(key string, defaultValue uint16) uint16 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseUint(v, 10, 64); err != nil { + return uint16(v2) + } + } + return defaultValue +} + +func Uint32(key string, defaultValue uint32) uint32 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseUint(v, 10, 64); err != nil { + return uint32(v2) + } + } + return defaultValue +} + +func Uint64(key string, defaultValue uint64) uint64 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseUint(v, 10, 64); err != nil { + return v2 + } + } + return defaultValue +} + +func Float32(key string, defaultValue float32) float32 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseFloat(v, 64); err != nil { + return float32(v2) + } + } + return defaultValue +} + +func Float64(key string, defaultValue float64) float64 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseFloat(v, 64); err != nil { + return v2 + } + } + return defaultValue +} + +func Complex64(key string, defaultValue complex64) complex64 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseComplex(v, 64); err != nil { + return complex64(v2) + } + } + return defaultValue +} + +func Complex128(key string, defaultValue complex128) complex128 { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseComplex(v, 64); err != nil { + return v2 + } + } + return defaultValue +} + +func Bool(key string, defaultValue bool) bool { + if v, ok := os.LookupEnv(key); ok { + if v2, err := strconv.ParseBool(v); err != nil { + return v2 + } + } + return defaultValue +}