Skip to content

Commit 92d8cee

Browse files
asmacdoJAORMX
andauthored
Make bundle validate subcommand respect verbosity (#3795) (#3812)
* 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: #3793 * add unit test Co-authored-by: Austin Macdonald <[email protected]> Co-authored-by: Juan Osorio Robles <[email protected]>
1 parent 3b8d579 commit 92d8cee

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
entries:
2+
- description: Fixed debug logging in the `bundle validate` subcommand of `operator-sdk`
3+
kind: "bugfix"
4+
breaking: false

cmd/operator-sdk/bundle/validate.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,11 @@ func makeValidateCmd() *cobra.Command {
123123
Use: "validate",
124124
Short: "Validate an operator bundle",
125125
RunE: func(cmd *cobra.Command, args []string) (err error) {
126-
if viper.GetBool(flags.VerboseOpt) {
127-
log.SetLevel(log.DebugLevel)
128-
}
129-
130126
// Always print non-output logs to stderr as to not pollute actual command output.
131127
// Note that it allows the JSON result be redirected to the Stdout. E.g
132128
// if we run the command with `| jq . > result.json` the command will print just the logs
133129
// and the file will have only the JSON result.
134-
logger := log.NewEntry(internal.NewLoggerTo(os.Stderr))
135-
130+
logger := createLogger(viper.GetBool(flags.VerboseOpt))
136131
if err = c.validate(args); err != nil {
137132
return fmt.Errorf("invalid command args: %v", err)
138133
}
@@ -156,6 +151,15 @@ func makeValidateCmd() *cobra.Command {
156151
return cmd
157152
}
158153

154+
// createLogger creates a new logrus Entry that is optionally verbose.
155+
func createLogger(verbose bool) *log.Entry {
156+
logger := log.NewEntry(internal.NewLoggerTo(os.Stderr))
157+
if verbose {
158+
logger.Logger.SetLevel(log.DebugLevel)
159+
}
160+
return logger
161+
}
162+
159163
// validate verifies the command args
160164
func (c bundleValidateCmd) validate(args []string) error {
161165
if len(args) != 1 {

cmd/operator-sdk/bundle/validate_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
. "github.com/onsi/ginkgo"
1919
. "github.com/onsi/gomega"
2020
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/bundle/internal"
21+
log "github.com/sirupsen/logrus"
2122
)
2223

2324
var _ = Describe("Running a bundle validate command", func() {
@@ -38,6 +39,19 @@ var _ = Describe("Running a bundle validate command", func() {
3839
})
3940
})
4041

42+
Describe("Creating a logger", func() {
43+
It("that is Info Level when not verbose", func() {
44+
verbose := false
45+
logger := createLogger(verbose)
46+
Expect(logger.Logger.GetLevel()).To(Equal(log.InfoLevel))
47+
})
48+
It("that is Debug level if verbose", func() {
49+
verbose := true
50+
logger := createLogger(verbose)
51+
Expect(logger.Logger.GetLevel()).To(Equal(log.DebugLevel))
52+
})
53+
})
54+
4155
Describe("validate", func() {
4256
var cmd bundleValidateCmd
4357
BeforeEach(func() {

0 commit comments

Comments
 (0)