diff --git a/CHANGELOG.md b/CHANGELOG.md index 542964c834..9fca9e4658 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - New option for [`operator-sdk build --image-builder`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#build), which can be used to specify which image builder to use. Adds support for [buildah](https://github.com/containers/buildah/). ([#1311](https://github.com/operator-framework/operator-sdk/pull/1311)) - Manager is now configured with a new `DynamicRESTMapper`, which accounts for the fact that the default `RESTMapper`, which only checks resource types at startup, can't handle the case of first creating a CRD and then an instance of that CRD. ([#1329](https://github.com/operator-framework/operator-sdk/pull/1329)) +- Unify CLI debug logging under a global `--verbose` flag ([#1361](https://github.com/operator-framework/operator-sdk/pull/1361)) ### Changed diff --git a/cmd/operator-sdk/internal/genutil/genutil.go b/cmd/operator-sdk/internal/genutil/genutil.go index e9c1c53558..b49ad9d5ca 100644 --- a/cmd/operator-sdk/internal/genutil/genutil.go +++ b/cmd/operator-sdk/internal/genutil/genutil.go @@ -22,10 +22,12 @@ import ( "path/filepath" "strings" + flags "github.com/operator-framework/operator-sdk/internal/pkg/flags" "github.com/operator-framework/operator-sdk/internal/pkg/scaffold" "github.com/operator-framework/operator-sdk/internal/util/projutil" log "github.com/sirupsen/logrus" + "github.com/spf13/viper" ) func buildCodegenBinaries(genDirs []string, binDir, codegenSrcDir string) error { @@ -47,7 +49,7 @@ func runGoBuildCodegen(binDir, repoDir, genDir string) error { } // Only print binary build info if verbosity is explicitly set. - if projutil.IsGoVerbose() { + if viper.GetBool(flags.VerboseOpt) { return projutil.ExecCmd(cmd) } cmd.Stdout = ioutil.Discard diff --git a/cmd/operator-sdk/main.go b/cmd/operator-sdk/main.go index 8826dcaa10..14dcea2a74 100644 --- a/cmd/operator-sdk/main.go +++ b/cmd/operator-sdk/main.go @@ -15,6 +15,7 @@ package main import ( + "github.com/operator-framework/operator-sdk/internal/util/projutil" "os" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) @@ -32,8 +33,12 @@ import ( "github.com/operator-framework/operator-sdk/cmd/operator-sdk/test" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/up" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/version" + flags "github.com/operator-framework/operator-sdk/internal/pkg/flags" osdkversion "github.com/operator-framework/operator-sdk/version" + + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/spf13/viper" _ "k8s.io/client-go/plugin/pkg/client/auth" ) @@ -42,6 +47,17 @@ func main() { Use: "operator-sdk", Short: "An SDK for building operators with ease", Version: osdkversion.Version, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if viper.GetBool(flags.VerboseOpt) { + err := projutil.SetGoVerbose() + if err != nil { + log.Errorf("Could not set GOFLAGS: (%v)", err) + return + } + log.SetLevel(log.DebugLevel) + log.Debug("Debug logging is set") + } + }, } root.AddCommand(new.NewCmd()) @@ -58,6 +74,11 @@ func main() { root.AddCommand(olmcatalog.NewCmd()) root.AddCommand(version.NewCmd()) + root.PersistentFlags().Bool(flags.VerboseOpt, false, "Enable verbose logging") + if err := viper.BindPFlags(root.PersistentFlags()); err != nil { + log.Fatalf("Failed to bind root flags: %v", err) + } + if err := root.Execute(); err != nil { os.Exit(1) } diff --git a/cmd/operator-sdk/scorecard/cmd.go b/cmd/operator-sdk/scorecard/cmd.go index 9cca90a2ba..c8c8542b1c 100644 --- a/cmd/operator-sdk/scorecard/cmd.go +++ b/cmd/operator-sdk/scorecard/cmd.go @@ -52,7 +52,6 @@ func NewCmd() *cobra.Command { scorecardCmd.Flags().String(scorecard.ProxyPullPolicyOpt, "Always", "Pull policy for scorecard proxy image") scorecardCmd.Flags().String(scorecard.CRDsDirOpt, scaffold.CRDsDir, "Directory containing CRDs (all CRD manifest filenames must have the suffix 'crd.yaml')") scorecardCmd.Flags().StringP(scorecard.OutputFormatOpt, "o", "human-readable", "Output format for results. Valid values: human-readable, json") - scorecardCmd.Flags().Bool(scorecard.VerboseOpt, false, "Enable verbose logging") if err := viper.BindPFlags(scorecardCmd.Flags()); err != nil { log.Fatalf("Failed to bind scorecard flags to viper: %v", err) diff --git a/doc/sdk-cli-reference.md b/doc/sdk-cli-reference.md index 631c4633d2..bc6189f84d 100644 --- a/doc/sdk-cli-reference.md +++ b/doc/sdk-cli-reference.md @@ -5,6 +5,10 @@ Usage: operator-sdk [command] ``` +### Global Flags + +* `--verbose` - enable debug logging + ## build ### Args diff --git a/internal/pkg/flags/flags.go b/internal/pkg/flags/flags.go new file mode 100644 index 0000000000..cc374cff85 --- /dev/null +++ b/internal/pkg/flags/flags.go @@ -0,0 +1,20 @@ +// Copyright 2019 The Operator-SDK Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package flags + +// global command-line flags +const ( + VerboseOpt = "verbose" +) diff --git a/internal/pkg/scorecard/scorecard.go b/internal/pkg/scorecard/scorecard.go index f2a57dfec9..b783d03e2f 100644 --- a/internal/pkg/scorecard/scorecard.go +++ b/internal/pkg/scorecard/scorecard.go @@ -32,7 +32,7 @@ import ( "github.com/ghodss/yaml" olmapiv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1" olminstall "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install" - logrus "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" v1 "k8s.io/api/core/v1" @@ -63,7 +63,6 @@ const ( ProxyImageOpt = "proxy-image" ProxyPullPolicyOpt = "proxy-pull-policy" CRDsDirOpt = "crds-dir" - VerboseOpt = "verbose" OutputFormatOpt = "output" ) @@ -283,9 +282,6 @@ func ScorecardTests(cmd *cobra.Command, args []string) error { return err } cmd.SilenceUsage = true - if viper.GetBool(VerboseOpt) { - log.SetLevel(logrus.DebugLevel) - } suites, err := runTests() if err != nil { return err diff --git a/internal/util/projutil/exec.go b/internal/util/projutil/exec.go index d8f6dc3a9a..e01ee12ceb 100644 --- a/internal/util/projutil/exec.go +++ b/internal/util/projutil/exec.go @@ -22,11 +22,13 @@ import ( "strings" homedir "github.com/mitchellh/go-homedir" + log "github.com/sirupsen/logrus" ) func ExecCmd(cmd *exec.Cmd) error { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + log.Debugf("Running %#v", cmd.Args) if err := cmd.Run(); err != nil { return fmt.Errorf("failed to exec %#v: %v", cmd.Args, err) } diff --git a/internal/util/projutil/project_util.go b/internal/util/projutil/project_util.go index 80dde4773d..20beb0aeda 100644 --- a/internal/util/projutil/project_util.go +++ b/internal/util/projutil/project_util.go @@ -188,9 +188,15 @@ func MustSetGopath(currentGopath string) string { var flagRe = regexp.MustCompile("(.* )?-v(.* )?") -// IsGoVerbose returns true if GOFLAGS contains "-v". This function is useful -// when deciding whether to make "go" command output verbose. -func IsGoVerbose() bool { +// SetGoVerbose sets GOFLAGS="${GOFLAGS} -v" if GOFLAGS does not +// already contain "-v" to make "go" command output verbose. +func SetGoVerbose() error { gf, ok := os.LookupEnv(GoFlagsEnv) - return ok && len(gf) != 0 && flagRe.MatchString(gf) + if !ok || len(gf) == 0 { + return os.Setenv(GoFlagsEnv, "-v") + } + if !flagRe.MatchString(gf) { + return os.Setenv(GoFlagsEnv, gf+" -v") + } + return nil }