Skip to content

Commit

Permalink
fix: update config
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Jan 18, 2024
1 parent cea7e16 commit a05b181
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 75 deletions.
11 changes: 0 additions & 11 deletions config/aaa.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package config

const (
defaultConfigName = "config"
defaultConfigType = "yaml"
defaultConfigPath = "./configs"
)

var (
configDir string
configPath string
)

type NamedConfig interface {
// ConfigUniqueName unique name
ConfigUniqueName() string
Expand Down
123 changes: 123 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package config

import (
"github.com/a8m/envsubst"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/pathutil"
"github.com/pubgo/funk/vars"
"gopkg.in/yaml.v3"
"log"
"os"
"path/filepath"
)

const (
defaultConfigName = "config"
defaultConfigType = "yaml"
defaultConfigPath = "./configs"
)

var (
configDir string
configPath string
)

func init() {
vars.RegisterValue("config", map[string]any{
"config_type": defaultConfigType,
"config_name": defaultConfigName,
"config_path": configPath,
"config_dir": configDir,
})
}

func LoadFromPath[T any](val *T, path string) {
dir := filepath.Dir(path)
configBytes := assert.Must1(os.ReadFile(path))
configBytes = assert.Must1(envsubst.Bytes(configBytes))

assert.Must(yaml.Unmarshal(configBytes, val))

var res Resources
assert.Must(yaml.Unmarshal(configBytes, &res))

var cfgList []T
for _, resPath := range res.Resources {
resAbsPath := filepath.Join(dir, resPath)
if pathutil.IsNotExist(resAbsPath) {
log.Panicln("resources config path not found:", resAbsPath)
}

resBytes := assert.Must1(os.ReadFile(resAbsPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))

var cfg1 T
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
cfgList = append(cfgList, cfg1)
}

for _, resPath := range res.PatchResources {
resAbsPath := filepath.Join(dir, resPath)
if pathutil.IsNotExist(resAbsPath) {
continue
}

resBytes := assert.Must1(os.ReadFile(resAbsPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))

var cfg1 T
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
cfgList = append(cfgList, cfg1)
}

assert.Must(Merge(val, cfgList...))
}

func Load[T any]() T {
if configPath != "" {
configDir = filepath.Dir(configPath)
} else {
configPath, configDir = getConfigPath(defaultConfigName, defaultConfigType)
}

configBytes := assert.Must1(os.ReadFile(configPath))
configBytes = assert.Must1(envsubst.Bytes(configBytes))

var cfg T
assert.Must(yaml.Unmarshal(configBytes, &cfg))

var res Resources
assert.Must(yaml.Unmarshal(configBytes, &res))

var cfgList []T
for _, resPath := range res.Resources {
resAbsPath := filepath.Join(configDir, resPath)
if pathutil.IsNotExist(resAbsPath) {
log.Panicln("resources config path not found:", resAbsPath)
}

resBytes := assert.Must1(os.ReadFile(resAbsPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))

var cfg1 T
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
cfgList = append(cfgList, cfg1)
}

for _, resPath := range res.PatchResources {
resAbsPath := filepath.Join(configDir, resPath)
if pathutil.IsNotExist(resAbsPath) {
continue
}

resBytes := assert.Must1(os.ReadFile(resAbsPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))

var cfg1 T
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
cfgList = append(cfgList, cfg1)
}

assert.Must(Merge(&cfg, cfgList...))
return cfg
}
4 changes: 4 additions & 0 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type Node struct {
value *yaml.Node
}

func (c *Node) YamlNode() *yaml.Node {
return c.value
}

func (c *Node) MarshalYAML() (interface{}, error) {
return c.value, nil
}
Expand Down
67 changes: 5 additions & 62 deletions config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
"reflect"

"dario.cat/mergo"
"github.com/a8m/envsubst"
"gopkg.in/yaml.v3"

"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/errors"
"github.com/pubgo/funk/pathutil"
"github.com/pubgo/funk/result"
"github.com/pubgo/funk/vars"
)

func GetConfigDir() string {
Expand Down Expand Up @@ -74,60 +72,6 @@ func SetConfigPath(confPath string) {
configPath = confPath
}

func Load[T any]() T {
if configPath != "" {
configDir = filepath.Dir(configPath)
} else {
configPath, configDir = getConfigPath(defaultConfigName, defaultConfigType)
}

configBytes := assert.Must1(os.ReadFile(configPath))
configBytes = assert.Must1(envsubst.Bytes(configBytes))

var cfg T
assert.Must(yaml.Unmarshal(configBytes, &cfg))

var res Resources
assert.Must(yaml.Unmarshal(configBytes, &res))

var cfgList []T
for _, resPath := range res.Resources {
var cfg1 T
resAbsPath := filepath.Join(configDir, resPath)
if pathutil.IsNotExist(resAbsPath) {
log.Panicln("resources config path not found:", resAbsPath)
}
resBytes := assert.Must1(os.ReadFile(resAbsPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
cfgList = append(cfgList, cfg1)
}

for _, resPath := range res.PatchResources {
var cfg1 T
resAbsPath := filepath.Join(configDir, resPath)
if pathutil.IsNotExist(resAbsPath) {
continue
}
resBytes := assert.Must1(os.ReadFile(resAbsPath))
resBytes = assert.Must1(envsubst.Bytes(resBytes))
assert.Must(yaml.Unmarshal(resBytes, &cfg1))
cfgList = append(cfgList, cfg1)
}

assert.Must(Merge(&cfg, cfgList...))

vars.RegisterValue("config", map[string]any{
"config_type": defaultConfigType,
"config_name": defaultConfigName,
"config_path": configPath,
"config_dir": configDir,
"config_data": cfg,
})

return cfg
}

func MergeR[A any, B any | *any](dst *A, src ...B) (ret result.Result[*A]) {
if len(src) == 0 {
return ret.WithVal(dst)
Expand Down Expand Up @@ -211,11 +155,10 @@ func unmarshalOneOrList[T any](list *[]T, value *yaml.Node) error {
}
*list = append(*list, t)
return nil
} else if value.Kind == yaml.SequenceNode {
if err := value.Decode(list); err != nil {
return err
}
return nil
}
return fmt.Errorf("unmarshalable node: %v", value.Value)

if value.Kind == yaml.SequenceNode {
return value.Decode(list)
}
return errors.Format("unmarshalled node: %v", value.Value)
}
14 changes: 14 additions & 0 deletions config/util_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package config

import (
"os"
"testing"

"github.com/a8m/envsubst"
"github.com/pubgo/funk/pretty"
"github.com/stretchr/testify/assert"
)

func TestEnv(t *testing.T) {
os.Setenv("hello", "world")
data, err := envsubst.String("${hello}")
assert.Nil(t, err)
assert.Equal(t, data, "world")

os.Setenv("hello", "")
data, err = envsubst.String("${hello:-abc}")
assert.Nil(t, err)
assert.Equal(t, data, "abc")
}

func TestConfigPath(t *testing.T) {
t.Log(getConfigPath("", ""))
assert.Panics(t, func() {
Expand Down
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func New(msg string) error {
func NewFmt(msg string, args ...interface{}) error {
return WrapCaller(&Err{Msg: fmt.Sprintf(msg, args...)}, 1)
}
func Format(msg string, args ...interface{}) error {
return WrapCaller(&Err{Msg: fmt.Sprintf(msg, args...)}, 1)
}

func NewTags(msg string, tags ...Tag) error {
return WrapCaller(&Err{Msg: msg, Tags: tags}, 1)
Expand Down
10 changes: 8 additions & 2 deletions vars/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ func Map(name string) *expvar.Map {
return v.(*expvar.Map)
}

var _ json.Marshaler = (*Value)(nil)

type Value func() interface{}

func (f Value) MarshalJSON() ([]byte, error) {
return json.Marshal(f())
}

func (f Value) Value() interface{} { return f() }

func (f Value) String() (r string) {
Expand Down Expand Up @@ -78,10 +84,10 @@ func (f Value) String() (r string) {
}
}

func Register(name string, data func() interface{}) {
func Register(name string, value Value) {
defer recovery.Exit()
assert.If(Has(name), "name:%s already exists", name)
expvar.Publish(name, Value(data))
expvar.Publish(name, value)
}

func RegisterValue(name string, data interface{}) {
Expand Down

0 comments on commit a05b181

Please sign in to comment.