Skip to content

Commit

Permalink
main: add --verbose,-v persistent flag that increases verbosity
Browse files Browse the repository at this point in the history
This commit adds `--verbose,-v` which will increase the verbosity
of logrus and also switch the --progress to "verbose". This is
addressing the feedback we got in
#765
and a followup for #776

The new `-v` clashes unfortunately with cobras default for version,
so there is no single dash flag for version anymore. Most unix tools
(e.g. cp,rsync,mv,curl,ssh,tar) use "-v" for "--verbose" so IMHO we
should follow suite. Unfortuantely there is no consistency in linux,
e.g. git,gcc are counter examples where it means version). I would
still go with -v for verbose as ssh,tar,curl are probably used
more often to get verbose output.
  • Loading branch information
mvo5 committed Jan 9, 2025
1 parent 003423a commit 788f615
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
25 changes: 16 additions & 9 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,24 @@ func chownR(path string, chown string) error {
var rootLogLevel string

func rootPreRunE(cmd *cobra.Command, _ []string) error {
if rootLogLevel == "" {
verbose, _ := cmd.Flags().GetBool("verbose")
progress, _ := cmd.Flags().GetString("progress")
switch {
case rootLogLevel != "":
level, err := logrus.ParseLevel(rootLogLevel)
if err != nil {
return err
}
logrus.SetLevel(level)
case verbose:
logrus.SetLevel(logrus.InfoLevel)
default:
logrus.SetLevel(logrus.ErrorLevel)
return nil
}

level, err := logrus.ParseLevel(rootLogLevel)
if err != nil {
return err
if verbose && progress == "auto" {
cmd.Flags().Set("progress", "verbose")

Check failure on line 564 in bib/cmd/bootc-image-builder/main.go

View workflow job for this annotation

GitHub Actions / ⌨ Lint & unittests

Error return value of `(*github.com/spf13/pflag.FlagSet).Set` is not checked (errcheck)
}

logrus.SetLevel(level)

return nil
}

Expand Down Expand Up @@ -607,6 +613,7 @@ func buildCobraCmdline() (*cobra.Command, error) {
rootCmd.SetVersionTemplate(version)

rootCmd.PersistentFlags().StringVar(&rootLogLevel, "log-level", "", "logging level (debug, info, error); default error")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, `Switch to verbose mode`)

buildCmd := &cobra.Command{
Use: "build IMAGE_NAME",
Expand All @@ -620,7 +627,7 @@ func buildCobraCmdline() (*cobra.Command, error) {
SilenceUsage: true,
Example: rootCmd.Use + " build quay.io/centos-bootc/centos-bootc:stream9\n" +
rootCmd.Use + " quay.io/centos-bootc/centos-bootc:stream9\n",
Version: rootCmd.Version,
Version: rootCmd.Version,
}
buildCmd.SetVersionTemplate(version)

Expand Down
46 changes: 46 additions & 0 deletions bib/cmd/bootc-image-builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -573,3 +574,48 @@ func TestCobraCmdline(t *testing.T) {
})
}
}

func TestCobraCmdlineVerbose(t *testing.T) {
for _, tc := range []struct {
cmdline []string
expectedProgress string
expectedLogrusLevel logrus.Level
}{
{
[]string{"quay.io..."},
"auto",
logrus.ErrorLevel,
},
{
[]string{"-v", "quay.io..."},
"verbose",
logrus.InfoLevel,
},
} {
restore := mockOsArgs(tc.cmdline)
defer restore()

rootCmd, err := main.BuildCobraCmdline()
assert.NoError(t, err)

// collect progressFlag value
var progressFlag string
for _, cmd := range rootCmd.Commands() {
cmd.RunE = func(cmd *cobra.Command, args []string) error {
if progressFlag != "" {
t.Error("progressFlag set twice")
}
progressFlag, err = cmd.Flags().GetString("progress")
assert.NoError(t, err)
return nil
}
}

t.Run(tc.expectedProgress, func(t *testing.T) {
err = rootCmd.Execute()
assert.NoError(t, err)
assert.Equal(t, tc.expectedProgress, progressFlag)
assert.Equal(t, tc.expectedLogrusLevel, logrus.GetLevel())
})
}
}
2 changes: 1 addition & 1 deletion test/test_opts.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_bib_errors_only_once(tmp_path, container_storage, build_fake_container)
assert res.stderr.count(needle) == 1


@pytest.mark.parametrize("version_argument", ["version", "--version", "-v"])
@pytest.mark.parametrize("version_argument", ["version", "--version"])
def test_bib_version(tmp_path, container_storage, build_fake_container, version_argument):
output_path = tmp_path / "output"
output_path.mkdir(exist_ok=True)
Expand Down

0 comments on commit 788f615

Please sign in to comment.