Skip to content

Commit 25590b0

Browse files
authored
refactor: change the package name to stdlib, as well as add env package (#2)
Signed-off-by: Ardika Bagus <me@ardikabs.com>
1 parent fe3cfee commit 25590b0

File tree

16 files changed

+227
-83
lines changed

16 files changed

+227
-83
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
module github.com/ardikabs/golib
1+
module github.com/ardikabs/go-stdlib
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/pkg/errors v0.9.1

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
139139
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
140140
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
141141
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
142-
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
143142
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
144143
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
145144
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
@@ -158,8 +157,6 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
158157
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
159158
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
160159
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
161-
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
162-
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
163160
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
164161
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
165162
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
@@ -321,8 +318,6 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
321318
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
322319
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
323320
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
324-
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
325-
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
326321
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
327322
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
328323
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/env/env.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package env
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"strings"
7+
"time"
8+
)
9+
10+
type Type interface {
11+
bool | string | int | []string | []int | time.Duration
12+
}
13+
14+
func Lookup[T Type](key string, defaultValue T) T {
15+
value, ok := os.LookupEnv(key)
16+
if !ok {
17+
return defaultValue
18+
}
19+
20+
var res any
21+
switch any(defaultValue).(type) {
22+
case bool:
23+
value = strings.ToLower(value)
24+
valid := value == "1" || value == "true" || value == "ok" || value == "yes"
25+
if value == "" || !valid {
26+
return defaultValue
27+
}
28+
29+
res = valid
30+
case time.Duration:
31+
d, err := time.ParseDuration(value)
32+
if err != nil {
33+
return defaultValue
34+
}
35+
36+
res = d
37+
case int:
38+
i, err := strconv.ParseInt(value, 10, 0)
39+
if err != nil {
40+
return defaultValue
41+
}
42+
43+
res = int(i)
44+
case []int:
45+
if value == "" {
46+
return defaultValue
47+
}
48+
49+
list := strings.Split(value, ",")
50+
arr := make([]int, 0, len(list))
51+
52+
for _, v := range list {
53+
i, err := strconv.ParseInt(v, 10, 0)
54+
if err != nil {
55+
return defaultValue
56+
}
57+
arr = append(arr, int(i))
58+
}
59+
60+
res = arr
61+
case string:
62+
if value == "" {
63+
return defaultValue
64+
}
65+
66+
res = value
67+
case []string:
68+
if value == "" {
69+
return defaultValue
70+
}
71+
72+
res = strings.Split(value, ",")
73+
}
74+
75+
return res.(T)
76+
}

pkg/env/env_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package env_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
"time"
7+
8+
"github.com/ardikabs/go-stdlib/pkg/env"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestLookup(t *testing.T) {
13+
t.Setenv("HTTP_ADDR", "0.0.0.0")
14+
t.Setenv("HTTP_PORT", "8080")
15+
t.Setenv("DEBUG_MODE", "1")
16+
t.Setenv("HTTP_TIMEOUT", "30s")
17+
t.Setenv("HTTP_RESERVED_CONTENT_TYPES", "text/plain,application/json")
18+
t.Setenv("HTTP_RESERVED_STATUS_CODES", "401,429,503")
19+
defer os.Clearenv()
20+
21+
gotString := env.Lookup("HTTP_ADDR", "127.0.0.1")
22+
require.Equal(t, "0.0.0.0", gotString)
23+
24+
gotInt := env.Lookup("HTTP_PORT", 80)
25+
require.Equal(t, 8080, gotInt)
26+
27+
gotBool := env.Lookup("DEBUG_MODE", false)
28+
require.Equal(t, true, gotBool)
29+
30+
gotDuration := env.Lookup("HTTP_TIMEOUT", time.Duration(15*time.Second))
31+
require.Equal(t, time.Duration(30*time.Second), gotDuration)
32+
33+
gotStringArr := env.Lookup("HTTP_RESERVED_CONTENT_TYPES", []string{"text/html"})
34+
require.Equal(t, []string{"text/plain", "application/json"}, gotStringArr)
35+
36+
gotIntArr := env.Lookup("HTTP_RESERVED_STATUS_CODES", []int{500})
37+
require.Equal(t, []int{401, 429, 503}, gotIntArr)
38+
}
39+
40+
func TestLookupDefaultValue(t *testing.T) {
41+
t.Setenv("HTTP_ADDR", "")
42+
t.Setenv("HTTP_PORT", "invalid-value")
43+
t.Setenv("DEBUG_MODE", "invalid-value")
44+
t.Setenv("HTTP_TIMEOUT", "invalid-value")
45+
t.Setenv("HTTP_RESERVED_CONTENT_TYPES", "")
46+
t.Setenv("HTTP_RESERVED_STATUS_CODES", "")
47+
t.Setenv("RANGE_OF_RETRY_BACKOFF_SECONDS", "10,20,30a")
48+
defer os.Clearenv()
49+
50+
got := env.Lookup("AGENT_MODE", true)
51+
require.Equal(t, true, got)
52+
53+
gotString := env.Lookup("HTTP_ADDR", "127.0.0.1")
54+
require.Equal(t, "127.0.0.1", gotString)
55+
56+
gotInt := env.Lookup("HTTP_PORT", 80)
57+
require.Equal(t, 80, gotInt)
58+
59+
gotBool := env.Lookup("DEBUG_MODE", false)
60+
require.Equal(t, false, gotBool)
61+
62+
gotDuration := env.Lookup("HTTP_TIMEOUT", time.Duration(15*time.Second))
63+
require.Equal(t, time.Duration(15*time.Second), gotDuration)
64+
65+
gotStringArr := env.Lookup("HTTP_RESERVED_CONTENT_TYPES", []string{"text/html"})
66+
require.Equal(t, []string{"text/html"}, gotStringArr)
67+
68+
gotIntArr := env.Lookup("HTTP_RESERVED_STATUS_CODES", []int{500})
69+
require.Equal(t, []int{500}, gotIntArr)
70+
71+
gotBadIntArr := env.Lookup("RANGE_OF_RETRY_BACKOFF_SECONDS", []int{1, 5, 10})
72+
require.Equal(t, []int{1, 5, 10}, gotBadIntArr)
73+
}

pkg/errs/error_test.go renamed to pkg/errs/errs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
"github.com/ardikabs/golib/pkg/errs"
8+
"github.com/ardikabs/go-stdlib/pkg/errs"
99
"github.com/stretchr/testify/assert"
1010
)
1111

pkg/errs/httperror_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88
"testing"
99

10-
"github.com/ardikabs/golib/pkg/errs"
10+
"github.com/ardikabs/go-stdlib/pkg/errs"
1111
"github.com/rs/zerolog"
1212
"github.com/stretchr/testify/assert"
1313
)

pkg/exec/exec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os/exec"
66
"testing"
77

8-
. "github.com/ardikabs/golib/pkg/exec"
8+
. "github.com/ardikabs/go-stdlib/pkg/exec"
99
"github.com/stretchr/testify/assert"
1010
)
1111

pkg/httpc/constant.go renamed to pkg/httpclient/constant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpc
1+
package httpclient
22

33
const (
44
// HeaderContentType

pkg/httpc/errors.go renamed to pkg/httpclient/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpc
1+
package httpclient
22

33
import "fmt"
44

pkg/httpc/options.go renamed to pkg/httpclient/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpc
1+
package httpclient
22

33
import (
44
"bytes"
@@ -7,7 +7,7 @@ import (
77

88
"fmt"
99

10-
"github.com/ardikabs/golib/pkg/tool"
10+
"github.com/ardikabs/go-stdlib/pkg/tool"
1111
)
1212

1313
// WithContext set the context for the http request operation

pkg/httpc/request.go renamed to pkg/httpclient/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package httpc
1+
package httpclient
22

33
import (
44
"context"

0 commit comments

Comments
 (0)