From 5e315c0f173733d2b77aeed5f0b7e07503fd8f82 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 20 Oct 2021 12:40:52 -0400 Subject: [PATCH] Disable ETUI for piped input (#571) * fixed piped input Signed-off-by: Alex Goodman * allow pipedinput helper to raise an error Signed-off-by: Alex Goodman * factor out verbosity check to function Signed-off-by: Alex Goodman --- cmd/packages.go | 13 +++++++++- cmd/power_user.go | 2 +- internal/config/application.go | 47 ++++++++++++++++++---------------- internal/input.go | 16 ++++++++++++ 4 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 internal/input.go diff --git a/cmd/packages.go b/cmd/packages.go index 8d3d0028f..47d55eb3c 100644 --- a/cmd/packages.go +++ b/cmd/packages.go @@ -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() { diff --git a/cmd/power_user.go b/cmd/power_user.go index 85126f737..647db2337 100644 --- a/cmd/power_user.go +++ b/cmd/power_user.go @@ -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)..., ) } diff --git a/internal/config/application.go b/internal/config/application.go index dd5f9ce60..ea6a6ab3f 100644 --- a/internal/config/application.go +++ b/internal/config/application.go @@ -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 } diff --git a/internal/input.go b/internal/input.go new file mode 100644 index 000000000..92b267407 --- /dev/null +++ b/internal/input.go @@ -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 +}