Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global --verbose flag to operator-sdk command #1361

Merged
merged 13 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/operator-sdk/internal/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Operator-SDK Authors
briantopping marked this conversation as resolved.
Show resolved Hide resolved
//
// 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 internal

// global command-line flags
const (
VerboseOpt = "verbose"
)
6 changes: 4 additions & 2 deletions cmd/operator-sdk/internal/genutil/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"path/filepath"
"strings"

"github.com/spf13/viper"
estroz marked this conversation as resolved.
Show resolved Hide resolved
"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

flags "github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal"
log "github.com/sirupsen/logrus"
)

Expand All @@ -47,7 +48,8 @@ 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) {
briantopping marked this conversation as resolved.
Show resolved Hide resolved
_ = os.Setenv(projutil.GoFlagsEnv, os.Getenv(projutil.GoFlagsEnv)+" -v")
briantopping marked this conversation as resolved.
Show resolved Hide resolved
return projutil.ExecCmd(cmd)
}
cmd.Stdout = ioutil.Discard
Expand Down
13 changes: 13 additions & 0 deletions cmd/operator-sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/build"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/completion"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate"
flags "github.com/operator-framework/operator-sdk/cmd/operator-sdk/internal"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/migrate"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/new"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/olmcatalog"
Expand All @@ -33,15 +34,24 @@ import (
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/up"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/version"
osdkversion "github.com/operator-framework/operator-sdk/version"
log "github.com/sirupsen/logrus"
estroz marked this conversation as resolved.
Show resolved Hide resolved
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)


func main() {
root := &cobra.Command{
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) {
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is set")
}
},
}

root.AddCommand(new.NewCmd())
Expand All @@ -58,6 +68,9 @@ func main() {
root.AddCommand(olmcatalog.NewCmd())
root.AddCommand(version.NewCmd())

root.PersistentFlags().Bool(flags.VerboseOpt, false, "Enable verbose logging")
_ = viper.BindPFlag(flags.VerboseOpt, root.PersistentFlags().Lookup(flags.VerboseOpt))
briantopping marked this conversation as resolved.
Show resolved Hide resolved

if err := root.Execute(); err != nil {
os.Exit(1)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/operator-sdk/scorecard/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func NewCmd() *cobra.Command {
scorecardCmd.Flags().String(scorecard.ProxyImageOpt, fmt.Sprintf("quay.io/operator-framework/scorecard-proxy:%s", strings.TrimSuffix(version.Version, "+git")), "Image name for scorecard proxy")
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().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)
Expand Down
4 changes: 0 additions & 4 deletions internal/pkg/scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const (
ProxyImageOpt = "proxy-image"
ProxyPullPolicyOpt = "proxy-pull-policy"
CRDsDirOpt = "crds-dir"
VerboseOpt = "verbose"
)

const (
Expand Down Expand Up @@ -93,9 +92,6 @@ func ScorecardTests(cmd *cobra.Command, args []string) error {
return err
}
cmd.SilenceUsage = true
if viper.GetBool(VerboseOpt) {
log.SetLevel(log.DebugLevel)
}
defer func() {
if err := cleanupScorecard(); err != nil {
log.Errorf("Failed to clenup resources: (%v)", err)
Expand Down
2 changes: 2 additions & 0 deletions internal/util/projutil/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
briantopping marked this conversation as resolved.
Show resolved Hide resolved
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to exec %#v: %v", cmd.Args, err)
}
Expand Down
10 changes: 0 additions & 10 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -185,12 +184,3 @@ func MustSetGopath(currentGopath string) string {
}
return newGopath
}

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 {
gf, ok := os.LookupEnv(GoFlagsEnv)
return ok && len(gf) != 0 && flagRe.MatchString(gf)
}