diff --git a/cmd/root.go b/cmd/root.go index 2ef0b49..5a6b3bc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -42,7 +42,7 @@ var ( ) // Execute is the main entrypoint for this application -func Execute() { +func Execute() error { cobra.OnInitialize(initConfig) cmd := &cobra.Command{ @@ -52,12 +52,12 @@ func Execute() { if err := cfg.Validate(); err != nil { cfg.Logger.Error(err, "invalid configuration") - return nil + return err } if err := cfg.ParseModules(); err != nil { cfg.Logger.Error(err, "invalid module configuration") - return nil + return err } return builder.GenerateAndCompile(cfg) @@ -83,11 +83,15 @@ func Execute() { // tie Viper to flags if err := viper.BindPFlags(cmd.Flags()); err != nil { cfg.Logger.Error(err, "failed to bind flags") + return err } if err := cmd.Execute(); err != nil { cfg.Logger.Error(err, "failed to run") + return err } + + return nil } func initConfig() { @@ -113,6 +117,7 @@ func initConfig() { // convert Viper's internal state into our configuration object if err := viper.Unmarshal(&cfg); err != nil { cfg.Logger.Error(err, "failed to parse the config") + cobra.CheckErr(err) return } } diff --git a/go.mod b/go.mod index 88564c6..3f9b8c4 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ go 1.14 require ( github.com/go-logr/logr v0.2.1 github.com/go-logr/zapr v0.2.0 - github.com/spf13/cobra v1.0.0 + github.com/spf13/cobra v1.1.3 github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.6.1 go.uber.org/zap v1.10.0 diff --git a/go.sum b/go.sum index 32e9164..8d57556 100644 --- a/go.sum +++ b/go.sum @@ -280,11 +280,16 @@ github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= +github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M= +github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +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/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= +github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk= github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -455,6 +460,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/main.go b/main.go index 2399c22..0ef002a 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,15 @@ package main import ( + "fmt" + "os" + "github.com/open-telemetry/opentelemetry-collector-builder/cmd" ) func main() { - cmd.Execute() + if err := cmd.Execute(); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } }