Skip to content

Commit

Permalink
refactor: change the package name to stdlib, as well as add env packa…
Browse files Browse the repository at this point in the history
…ge (#2)

Signed-off-by: Ardika Bagus <me@ardikabs.com>
  • Loading branch information
ardikabs authored Nov 17, 2023
1 parent fe3cfee commit 25590b0
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 83 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ardikabs/golib
module github.com/ardikabs/go-stdlib

go 1.18
go 1.20

require (
github.com/pkg/errors v0.9.1
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand All @@ -158,8 +157,6 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
Expand Down Expand Up @@ -321,8 +318,6 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
76 changes: 76 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package env

import (
"os"
"strconv"
"strings"
"time"
)

type Type interface {
bool | string | int | []string | []int | time.Duration
}

func Lookup[T Type](key string, defaultValue T) T {
value, ok := os.LookupEnv(key)
if !ok {
return defaultValue
}

var res any
switch any(defaultValue).(type) {
case bool:
value = strings.ToLower(value)
valid := value == "1" || value == "true" || value == "ok" || value == "yes"
if value == "" || !valid {
return defaultValue
}

res = valid
case time.Duration:
d, err := time.ParseDuration(value)
if err != nil {
return defaultValue
}

res = d
case int:
i, err := strconv.ParseInt(value, 10, 0)
if err != nil {
return defaultValue
}

res = int(i)
case []int:
if value == "" {
return defaultValue
}

list := strings.Split(value, ",")
arr := make([]int, 0, len(list))

for _, v := range list {
i, err := strconv.ParseInt(v, 10, 0)
if err != nil {
return defaultValue
}
arr = append(arr, int(i))
}

res = arr
case string:
if value == "" {
return defaultValue
}

res = value
case []string:
if value == "" {
return defaultValue
}

res = strings.Split(value, ",")
}

return res.(T)
}
73 changes: 73 additions & 0 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package env_test

import (
"os"
"testing"
"time"

"github.com/ardikabs/go-stdlib/pkg/env"
"github.com/stretchr/testify/require"
)

func TestLookup(t *testing.T) {
t.Setenv("HTTP_ADDR", "0.0.0.0")
t.Setenv("HTTP_PORT", "8080")
t.Setenv("DEBUG_MODE", "1")
t.Setenv("HTTP_TIMEOUT", "30s")
t.Setenv("HTTP_RESERVED_CONTENT_TYPES", "text/plain,application/json")
t.Setenv("HTTP_RESERVED_STATUS_CODES", "401,429,503")
defer os.Clearenv()

gotString := env.Lookup("HTTP_ADDR", "127.0.0.1")
require.Equal(t, "0.0.0.0", gotString)

gotInt := env.Lookup("HTTP_PORT", 80)
require.Equal(t, 8080, gotInt)

gotBool := env.Lookup("DEBUG_MODE", false)
require.Equal(t, true, gotBool)

gotDuration := env.Lookup("HTTP_TIMEOUT", time.Duration(15*time.Second))
require.Equal(t, time.Duration(30*time.Second), gotDuration)

gotStringArr := env.Lookup("HTTP_RESERVED_CONTENT_TYPES", []string{"text/html"})
require.Equal(t, []string{"text/plain", "application/json"}, gotStringArr)

gotIntArr := env.Lookup("HTTP_RESERVED_STATUS_CODES", []int{500})
require.Equal(t, []int{401, 429, 503}, gotIntArr)
}

func TestLookupDefaultValue(t *testing.T) {
t.Setenv("HTTP_ADDR", "")
t.Setenv("HTTP_PORT", "invalid-value")
t.Setenv("DEBUG_MODE", "invalid-value")
t.Setenv("HTTP_TIMEOUT", "invalid-value")
t.Setenv("HTTP_RESERVED_CONTENT_TYPES", "")
t.Setenv("HTTP_RESERVED_STATUS_CODES", "")
t.Setenv("RANGE_OF_RETRY_BACKOFF_SECONDS", "10,20,30a")
defer os.Clearenv()

got := env.Lookup("AGENT_MODE", true)
require.Equal(t, true, got)

gotString := env.Lookup("HTTP_ADDR", "127.0.0.1")
require.Equal(t, "127.0.0.1", gotString)

gotInt := env.Lookup("HTTP_PORT", 80)
require.Equal(t, 80, gotInt)

gotBool := env.Lookup("DEBUG_MODE", false)
require.Equal(t, false, gotBool)

gotDuration := env.Lookup("HTTP_TIMEOUT", time.Duration(15*time.Second))
require.Equal(t, time.Duration(15*time.Second), gotDuration)

gotStringArr := env.Lookup("HTTP_RESERVED_CONTENT_TYPES", []string{"text/html"})
require.Equal(t, []string{"text/html"}, gotStringArr)

gotIntArr := env.Lookup("HTTP_RESERVED_STATUS_CODES", []int{500})
require.Equal(t, []int{500}, gotIntArr)

gotBadIntArr := env.Lookup("RANGE_OF_RETRY_BACKOFF_SECONDS", []int{1, 5, 10})
require.Equal(t, []int{1, 5, 10}, gotBadIntArr)
}
2 changes: 1 addition & 1 deletion pkg/errs/error_test.go → pkg/errs/errs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/ardikabs/golib/pkg/errs"
"github.com/ardikabs/go-stdlib/pkg/errs"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/errs/httperror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"testing"

"github.com/ardikabs/golib/pkg/errs"
"github.com/ardikabs/go-stdlib/pkg/errs"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os/exec"
"testing"

. "github.com/ardikabs/golib/pkg/exec"
. "github.com/ardikabs/go-stdlib/pkg/exec"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/httpc/constant.go → pkg/httpclient/constant.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package httpc
package httpclient

const (
// HeaderContentType
Expand Down
2 changes: 1 addition & 1 deletion pkg/httpc/errors.go → pkg/httpclient/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package httpc
package httpclient

import "fmt"

Expand Down
4 changes: 2 additions & 2 deletions pkg/httpc/options.go → pkg/httpclient/options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package httpc
package httpclient

import (
"bytes"
Expand All @@ -7,7 +7,7 @@ import (

"fmt"

"github.com/ardikabs/golib/pkg/tool"
"github.com/ardikabs/go-stdlib/pkg/tool"
)

// WithContext set the context for the http request operation
Expand Down
2 changes: 1 addition & 1 deletion pkg/httpc/request.go → pkg/httpclient/request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package httpc
package httpclient

import (
"context"
Expand Down
Loading

0 comments on commit 25590b0

Please sign in to comment.