Skip to content

Commit 206b5a3

Browse files
committed
refactor: move all packages under pkg/ and add config
1 parent 487a955 commit 206b5a3

File tree

16 files changed

+589
-20
lines changed

16 files changed

+589
-20
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,3 @@ Common go packages.
1010
## Prerequisites
1111

1212
1. Go 1.18+
13-
14-
## Packages
15-
16-
1. [HTTP Client Builder](./httpc): HTTP Client builder with batteries included.
17-
2. [Errors](./errs): A constructed errors.
18-
3. [Toolkit](./tool): A set of toolkit helpers.
19-
4. [Validator](./validator): A simple validator to validate any boolean case

go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,23 @@ require github.com/stretchr/testify v1.8.0
66

77
require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/fsnotify/fsnotify v1.5.4 // indirect
10+
github.com/hashicorp/hcl v1.0.0 // indirect
11+
github.com/magiconair/properties v1.8.6 // indirect
12+
github.com/mitchellh/mapstructure v1.5.0 // indirect
13+
github.com/pelletier/go-toml v1.9.5 // indirect
14+
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
915
github.com/pkg/errors v0.9.1 // indirect
1016
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/spf13/afero v1.9.2 // indirect
18+
github.com/spf13/cast v1.5.0 // indirect
19+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
20+
github.com/spf13/pflag v1.0.5 // indirect
21+
github.com/spf13/viper v1.13.0 // indirect
22+
github.com/subosito/gotenv v1.4.1 // indirect
23+
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
24+
golang.org/x/text v0.3.7 // indirect
25+
gopkg.in/ini.v1 v1.67.0 // indirect
26+
gopkg.in/yaml.v2 v2.4.0 // indirect
1127
gopkg.in/yaml.v3 v3.0.1 // indirect
1228
)

go.sum

Lines changed: 459 additions & 0 deletions
Large diffs are not rendered by default.

pkg/config/config.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package config
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
10+
"github.com/spf13/viper"
11+
)
12+
13+
func NewConfig(configPath, configName, envPrefix string) *viper.Viper {
14+
fang := viper.New()
15+
16+
if envPrefix != "" {
17+
fang.SetEnvPrefix(envPrefix)
18+
}
19+
20+
fang.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
21+
fang.AutomaticEnv()
22+
23+
fang.SetConfigName(configName)
24+
fang.AddConfigPath(".")
25+
fang.AddConfigPath(configPath)
26+
27+
if err := fang.ReadInConfig(); err != nil { // Handle errors reading the config file
28+
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
29+
panic(fmt.Errorf("fatal error config file: %w", err))
30+
}
31+
}
32+
33+
return fang
34+
}
35+
36+
// GetString get integer value in the viper and environment variable with default value
37+
func GetString(viperkey string, env string, defaultVal string) string {
38+
if value := viper.GetString(viperkey); value != "" {
39+
return value
40+
}
41+
42+
if value := os.Getenv(env); value != "" {
43+
return value
44+
}
45+
46+
return defaultVal
47+
}
48+
49+
// GetInt get integer value in the viper and environment variable with default value
50+
func GetInt(viperkey string, env string, defaultVal int) int {
51+
if value := viper.Get(viperkey); value != nil {
52+
switch value.(type) {
53+
case string:
54+
if v, err := strconv.Atoi(value.(string)); err == nil {
55+
return v
56+
}
57+
case int:
58+
return value.(int)
59+
}
60+
}
61+
62+
if value := os.Getenv(env); value != "" {
63+
if v, err := strconv.Atoi(value); err == nil {
64+
return v
65+
}
66+
}
67+
return defaultVal
68+
}
69+
70+
// GetBool get bool value in the viper and environment variable with default value
71+
func GetBool(viperkey string, env string, defaultVal bool) bool {
72+
if value := viper.GetString(viperkey); value != "" {
73+
return viper.GetBool(viperkey)
74+
}
75+
76+
value := os.Getenv(env)
77+
boolVal, err := strconv.ParseBool(value)
78+
if err != nil {
79+
return defaultVal
80+
}
81+
82+
return boolVal
83+
}
84+
85+
// GetStringFromBase64Encoded get string from base64 encoded value in the viper and environment variable
86+
func GetStringFromBase64Encoded(viperkey string, env string) string {
87+
value := viper.GetString(viperkey)
88+
if value == "" {
89+
value = os.Getenv(env)
90+
}
91+
92+
content, err := base64.StdEncoding.DecodeString(value)
93+
if err != nil {
94+
return ""
95+
}
96+
97+
return string(content)
98+
}

errs/errs.go renamed to pkg/errs/errs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func E(args ...interface{}) error {
209209
}
210210
default:
211211
_, file, line, _ := runtime.Caller(1)
212-
return fmt.Errorf("errors.E: bad call from %s:%d: %v, unknown type %T, value %v in error call", file, line, args, arg, arg)
212+
return fmt.Errorf("errs.E: bad call from %s:%d: %v, unknown type %T, value %v in error call", file, line, args, arg, arg)
213213
}
214214
}
215215

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package errs_test
22

33
import (
4+
"fmt"
45
"testing"
56

6-
"github.com/ardikabs/golib/errs"
7-
"github.com/pkg/errors"
7+
"github.com/ardikabs/golib/pkg/errs"
88
"github.com/stretchr/testify/assert"
99
)
1010

@@ -66,19 +66,21 @@ func TestKindIs(t *testing.T) {
6666

6767
func TestMatch(t *testing.T) {
6868
user := errs.UserName("ardikabs")
69-
err := errors.New("network unreachable")
69+
code := errs.Code("os_network")
70+
param := errs.Parameter("param")
71+
err := fmt.Errorf("network unreachable")
7072

7173
// Now construct a reference error, which might not have all
7274
// the fields of the error from the test.
73-
want := errs.E(errs.IO, user, err)
75+
want := errs.E(errs.IO, user, param, code, err)
7476

7577
// Construct an error, one we pretend to have received from a test.
76-
err1 := errs.E(errs.IO, user, err)
78+
err1 := errs.E(errs.IO, user, param, code, err)
7779
match := errs.Match(want, err1)
7880
assert.True(t, match, "Expect to be matched, but got mismatched")
7981

8082
// Now one that's incorrect - wrong Kind.
81-
err2 := errs.E(errs.Database, user, err)
83+
err2 := errs.E(errs.Database, user, param, code, err)
8284
match = errs.Match(want, err2)
8385
assert.False(t, match, "Expect to be mismatched, but matched")
8486
}
File renamed without changes.
File renamed without changes.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"encoding/json"
66

77
"fmt"
8-
"github.com/ardikabs/golib/tool"
8+
9+
"github.com/ardikabs/golib/pkg/tool"
910
)
1011

1112
// WithPath set the URL Path
File renamed without changes.

httpc/request_test.go renamed to pkg/httpc/request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http/httptest"
99
"testing"
1010

11-
"github.com/ardikabs/golib/httpc"
11+
"github.com/ardikabs/golib/pkg/httpc"
1212
"github.com/stretchr/testify/assert"
1313
"github.com/stretchr/testify/require"
1414
)
File renamed without changes.
File renamed without changes.

tool/tool_test.go renamed to pkg/tool/tool_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"regexp"
55
"testing"
66

7-
"github.com/ardikabs/golib/tool"
7+
"github.com/ardikabs/golib/pkg/tool"
88
"github.com/stretchr/testify/assert"
99
)
1010

validator/validator.go renamed to pkg/validator/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package validator
22

33
import (
4-
"github.com/ardikabs/golib/errs"
4+
"github.com/ardikabs/golib/pkg/errs"
55
)
66

77
type Validator struct {

validator/validator_test.go renamed to pkg/validator/validator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package validator_test
33
import (
44
"testing"
55

6-
"github.com/ardikabs/golib/errs"
7-
"github.com/ardikabs/golib/validator"
6+
"github.com/ardikabs/golib/pkg/errs"
7+
"github.com/ardikabs/golib/pkg/validator"
88
"github.com/stretchr/testify/assert"
99
)
1010

0 commit comments

Comments
 (0)