From db13a1d586ad60f445dc2b97468f2d12121c8ffb Mon Sep 17 00:00:00 2001 From: aaronschweig Date: Thu, 3 Apr 2025 10:20:20 +0200 Subject: [PATCH 1/2] feat(config): update configuration handling to use cobra commands --- config/config.go | 19 ++++++++++++++----- config/config_test.go | 15 ++++++++++++--- go.mod | 2 ++ go.sum | 3 +++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index efaa900..62afe63 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ import ( "time" "github.com/openmfp/golang-commons/context/keys" + "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" ) @@ -125,19 +126,27 @@ func traverseStruct(value reflect.Value, flagSet *pflag.FlagSet, prefix string) } } -func NewConfigFor(serviceConfig any) (*viper.Viper, error) { +func NewDefaultConfig(rootCmd *cobra.Command) (*viper.Viper, error) { v := viper.NewWithOptions( viper.EnvKeyReplacer(strings.NewReplacer("-", "_")), ) v.AutomaticEnv() - err := v.BindPFlags(generateFlagSet(serviceConfig)) + err := v.BindPFlags(CommonFlags()) + rootCmd.PersistentFlags().AddFlagSet(CommonFlags()) + + return v, err +} + +func BindConfigToFlags(v *viper.Viper, cmd *cobra.Command, config any) error { + flagSet := generateFlagSet(config) + err := v.BindPFlags(flagSet) if err != nil { - return nil, err + return err } - err = v.BindPFlags(CommonFlags()) + cmd.Flags().AddFlagSet(flagSet) - return v, err + return nil } diff --git a/config/config_test.go b/config/config_test.go index c3bd84f..962fecb 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,6 +6,8 @@ import ( "time" "github.com/openmfp/golang-commons/config" + "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) @@ -18,7 +20,7 @@ func TestSetConfigInContext(t *testing.T) { assert.Equal(t, configStr, retrievedConfig) } -func TestNewConfigFor(t *testing.T) { +func TestBindConfigToFlags(t *testing.T) { type test struct { config.CommonServiceConfig @@ -35,10 +37,17 @@ func TestNewConfigFor(t *testing.T) { testStruct := test{} - v, err := config.NewConfigFor(&testStruct) + v := viper.New() + + err := config.BindConfigToFlags(v, &cobra.Command{}, &testStruct) assert.NoError(t, err) - err = v.Unmarshal(&testStruct) +} + +func TestNewDefaultConfig(t *testing.T) { + v, err := config.NewDefaultConfig(&cobra.Command{}) assert.NoError(t, err) + v.Unmarshal(&config.CommonServiceConfig{}) + assert.NoError(t, err) } diff --git a/go.mod b/go.mod index cc910b9..b4c0fe9 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/openfga/openfga v1.7.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.34.0 + github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.6 github.com/spf13/viper v1.20.1 github.com/stretchr/testify v1.10.0 @@ -64,6 +65,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/karlseguin/ccache/v3 v3.0.6 // indirect diff --git a/go.sum b/go.sum index 11dfc81..d4c418a 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,7 @@ github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -253,6 +254,7 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo= github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= @@ -270,6 +272,7 @@ github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4= From 6beb9d7585513f2a504384e7032d9391baae2318 Mon Sep 17 00:00:00 2001 From: aaronschweig Date: Thu, 3 Apr 2025 10:22:35 +0200 Subject: [PATCH 2/2] chore: linting --- config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index 962fecb..b6690dc 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -48,6 +48,6 @@ func TestNewDefaultConfig(t *testing.T) { v, err := config.NewDefaultConfig(&cobra.Command{}) assert.NoError(t, err) - v.Unmarshal(&config.CommonServiceConfig{}) + err = v.Unmarshal(&config.CommonServiceConfig{}) assert.NoError(t, err) }