From 64046ec80d1f6614714d8cfb7ab235faabfcaa39 Mon Sep 17 00:00:00 2001 From: Renan Date: Wed, 4 Oct 2023 18:48:41 -0300 Subject: [PATCH] adding config stuff --- go.mod | 6 ++++ go.sum | 15 +++++++++ internal/config/config.go | 59 ++++++++++++++++++++++++++++++++++ internal/config/config_test.go | 21 ++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 internal/config/config.go create mode 100644 internal/config/config_test.go diff --git a/go.mod b/go.mod index 0761ea0de..12945bffd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,13 @@ go 1.21.1 require github.com/spf13/cobra v1.7.0 +require github.com/stretchr/testify v1.8.4 + require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f3366a91a..5e862f2f5 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,25 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 000000000..bb1d8c1c3 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,59 @@ +package config + +import ( + "fmt" + "os" + "reflect" + "strconv" +) + +const prefix = "CARTESI_" + +type Config struct { + GraphQLPort int `env:"GRAPHQL_PORT" default:"8080"` + InspectPort int `env:"INSPECT_PORT" default:"8081"` +} + +func Load() (*Config, error) { + config := &Config{} + v := reflect.Indirect(reflect.ValueOf(config)) + t := reflect.TypeOf(*config) + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + env, ok := field.Tag.Lookup("env") + if !ok { + return nil, fmt.Errorf(`field "%s" must have an "env" tag`, field.Name) + } + + s := os.Getenv(prefix + env) + if s == "" { + s = field.Tag.Get("default") + if s == "" { + return nil, fmt.Errorf(`empty value for %s`, env) + } + } + value, err := convert(s, field.Type) + if err != nil { + return nil, err + } + + v.FieldByName(field.Name).Set(value) + } + + return config, nil +} + +func convert(s string, t reflect.Type) (reflect.Value, error) { + kind := t.Kind() + switch kind { + case reflect.Int: + n, err := strconv.Atoi(s) + return reflect.ValueOf(n), err + case reflect.String: + return reflect.ValueOf(s), nil + default: + err := fmt.Errorf(`string "%s" cannot be converted to type %s`, s, kind) + return reflect.ValueOf(nil), err + } +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 000000000..bbfa6dc5a --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,21 @@ +package config + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConfigDefaults(t *testing.T) { + _, err := Load() + assert.Nil(t, err) +} + +func TestConfigTodo(t *testing.T) { + if os.Setenv("CARTESI_INSPECT_PORT", "aaa") != nil { + assert.Fail(t, "internal") + } + _, err := Load() + assert.Nil(t, err) +}