Skip to content

Commit

Permalink
Make bundle validate subcommand respect verbosity (operator-framework…
Browse files Browse the repository at this point in the history
…#3795)

* Make bundle validate subcommand respect verbosity

This makes the bundle validate subcommand respect the verbosity level
by setting it directly in the logger that's actually used, and not in
the global logger as was done previously.

Closes: operator-framework#3793

* add unit test 

Co-authored-by: Austin Macdonald <austin@redhat.com>
  • Loading branch information
2 people authored and joelanford committed Sep 17, 2020
1 parent 1cd4d3b commit adc096b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
4 changes: 4 additions & 0 deletions changelog/fragments/bundle-validate-verbose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
entries:
- description: Fixed debug logging in the `bundle validate` subcommand of `operator-sdk`
kind: "bugfix"
breaking: false
16 changes: 10 additions & 6 deletions internal/cmd/operator-sdk/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,11 @@ func makeValidateCmd() *cobra.Command {
Use: "validate",
Short: "Validate an operator bundle",
RunE: func(cmd *cobra.Command, args []string) (err error) {
if viper.GetBool(flags.VerboseOpt) {
log.SetLevel(log.DebugLevel)
}

// Always print non-output logs to stderr as to not pollute actual command output.
// Note that it allows the JSON result be redirected to the Stdout. E.g
// if we run the command with `| jq . > result.json` the command will print just the logs
// and the file will have only the JSON result.
logger := log.NewEntry(internal.NewLoggerTo(os.Stderr))

logger := createLogger(viper.GetBool(flags.VerboseOpt))
if err = c.validate(args); err != nil {
return fmt.Errorf("invalid command args: %v", err)
}
Expand All @@ -126,6 +121,15 @@ func makeValidateCmd() *cobra.Command {
return cmd
}

// createLogger creates a new logrus Entry that is optionally verbose.
func createLogger(verbose bool) *log.Entry {
logger := log.NewEntry(internal.NewLoggerTo(os.Stderr))
if verbose {
logger.Logger.SetLevel(log.DebugLevel)
}
return logger
}

// validate verifies the command args
func (c bundleValidateCmd) validate(args []string) error {
if len(args) != 1 {
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/operator-sdk/bundle/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/bundle/internal"
log "github.com/sirupsen/logrus"
)

var _ = Describe("Running a bundle validate command", func() {
Expand All @@ -38,6 +39,19 @@ var _ = Describe("Running a bundle validate command", func() {
})
})

Describe("Creating a logger", func() {
It("that is Info Level when not verbose", func() {
verbose := false
logger := createLogger(verbose)
Expect(logger.Logger.GetLevel()).To(Equal(log.InfoLevel))
})
It("that is Debug level if verbose", func() {
verbose := true
logger := createLogger(verbose)
Expect(logger.Logger.GetLevel()).To(Equal(log.DebugLevel))
})
})

Describe("validate", func() {
var cmd bundleValidateCmd
BeforeEach(func() {
Expand Down

0 comments on commit adc096b

Please sign in to comment.