Skip to content

Commit

Permalink
Disable ETUI for piped input (anchore#571)
Browse files Browse the repository at this point in the history
* fixed piped input

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* allow pipedinput helper to raise an error

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* factor out verbosity check to function

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman committed Oct 20, 2021
1 parent 8d8e831 commit 9d1083c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
13 changes: 12 additions & 1 deletion cmd/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,21 @@ func packagesExec(_ *cobra.Command, args []string) error {
setupSignals(),
eventSubscription,
stereoscope.Cleanup,
ui.Select(appConfig.CliOptions.Verbosity > 0, appConfig.Quiet, reporter)...,
ui.Select(isVerbose(), appConfig.Quiet, reporter)...,
)
}

func isVerbose() (result bool) {
isPipedInput, err := internal.IsPipedInput()
if err != nil {
// since we can't tell if there was piped input we assume that there could be to disable the ETUI
log.Warnf("unable to determine if there is piped input: %+v", err)
return true
}
// verbosity should consider if there is piped input (in which case we should not show the ETUI)
return appConfig.CliOptions.Verbosity > 0 || isPipedInput
}

func packagesExecWorker(userInput string) <-chan error {
errs := make(chan error)
go func() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/power_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func powerUserExec(_ *cobra.Command, args []string) error {
setupSignals(),
eventSubscription,
stereoscope.Cleanup,
ui.Select(appConfig.CliOptions.Verbosity > 0, appConfig.Quiet, reporter)...,
ui.Select(isVerbose(), appConfig.Quiet, reporter)...,
)
}

Expand Down
47 changes: 25 additions & 22 deletions internal/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,38 @@ func (cfg *Application) parseUploadOptions() error {
}

func (cfg *Application) parseLogLevelOption() error {
if cfg.Quiet {
switch {
case cfg.Quiet:
// TODO: this is bad: quiet option trumps all other logging options (such as to a file on disk)
// we should be able to quiet the console logging and leave file logging alone...
// ... this will be an enhancement for later
cfg.Log.LevelOpt = logrus.PanicLevel
} else {
if cfg.Log.Level != "" {
if cfg.CliOptions.Verbosity > 0 {
return fmt.Errorf("cannot explicitly set log level (cfg file or env var) and use -v flag together")
}
case cfg.Log.Level != "":
if cfg.CliOptions.Verbosity > 0 {
return fmt.Errorf("cannot explicitly set log level (cfg file or env var) and use -v flag together")
}

lvl, err := logrus.ParseLevel(strings.ToLower(cfg.Log.Level))
if err != nil {
return fmt.Errorf("bad log level configured (%q): %w", cfg.Log.Level, err)
}
// set the log level explicitly
cfg.Log.LevelOpt = lvl
} else {
// set the log level implicitly
switch v := cfg.CliOptions.Verbosity; {
case v == 1:
cfg.Log.LevelOpt = logrus.InfoLevel
case v >= 2:
cfg.Log.LevelOpt = logrus.DebugLevel
default:
cfg.Log.LevelOpt = logrus.ErrorLevel
}
lvl, err := logrus.ParseLevel(strings.ToLower(cfg.Log.Level))
if err != nil {
return fmt.Errorf("bad log level configured (%q): %w", cfg.Log.Level, err)
}

cfg.Log.LevelOpt = lvl
if cfg.Log.LevelOpt >= logrus.InfoLevel {
cfg.CliOptions.Verbosity = 1
}
default:

switch v := cfg.CliOptions.Verbosity; {
case v == 1:
cfg.Log.LevelOpt = logrus.InfoLevel
case v >= 2:
cfg.Log.LevelOpt = logrus.DebugLevel
default:
cfg.Log.LevelOpt = logrus.ErrorLevel
}
}

return nil
}

Expand Down
16 changes: 16 additions & 0 deletions internal/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package internal

import (
"fmt"
"os"
)

// IsPipedInput returns true if there is no input device, which means the user **may** be providing input via a pipe.
func IsPipedInput() (bool, error) {
fi, err := os.Stdin.Stat()
if err != nil {
return false, fmt.Errorf("unable to determine if there is piped input: %w", err)
}

return fi.Mode()&os.ModeCharDevice == 0, nil
}

0 comments on commit 9d1083c

Please sign in to comment.