From 5e566d3039dd506dbaad6cc0bd1b785b52aece55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Tue, 9 Mar 2021 17:44:12 +0100 Subject: [PATCH 1/2] Silent output when talking to a shell If the output is not a terminal, then assume that we are running docker-env or podman-env under "eval" or similar shell construct. So don't output all the interactive information, but only return the actual exit code for some further troubleshooting (perhaps). --- cmd/minikube/cmd/docker-env.go | 5 ++++ cmd/minikube/cmd/podman-env.go | 5 ++++ pkg/minikube/exit/exit.go | 20 +++++++++++++++- pkg/minikube/mustload/mustload.go | 3 +-- pkg/minikube/out/out.go | 39 ++++++++++++++++++++++++------- 5 files changed, 60 insertions(+), 12 deletions(-) diff --git a/cmd/minikube/cmd/docker-env.go b/cmd/minikube/cmd/docker-env.go index 159ad7df4d..9be9583bde 100644 --- a/cmd/minikube/cmd/docker-env.go +++ b/cmd/minikube/cmd/docker-env.go @@ -260,6 +260,11 @@ var dockerEnvCmd = &cobra.Command{ return } + if !out.IsTerminal(os.Stdout) { + out.SetSilent(true) + exit.SetShell(true) + } + cname := ClusterFlagValue() co := mustload.Running(cname) driverName := co.CP.Host.DriverName diff --git a/cmd/minikube/cmd/podman-env.go b/cmd/minikube/cmd/podman-env.go index 970811e930..144b9027d3 100644 --- a/cmd/minikube/cmd/podman-env.go +++ b/cmd/minikube/cmd/podman-env.go @@ -153,6 +153,11 @@ var podmanEnvCmd = &cobra.Command{ return } + if !out.IsTerminal(os.Stdout) { + out.SetSilent(true) + exit.SetShell(true) + } + cname := ClusterFlagValue() co := mustload.Running(cname) driverName := co.CP.Host.DriverName diff --git a/pkg/minikube/exit/exit.go b/pkg/minikube/exit/exit.go index 6ffc4c4e91..fddaf8ae2f 100644 --- a/pkg/minikube/exit/exit.go +++ b/pkg/minikube/exit/exit.go @@ -18,6 +18,7 @@ limitations under the License. package exit import ( + "fmt" "os" "runtime" @@ -27,6 +28,15 @@ import ( "k8s.io/minikube/pkg/minikube/style" ) +var ( + shell bool +) + +// SetShell configures if we are doing a shell configuration or not +func SetShell(s bool) { + shell = s +} + // Message outputs a templated message and exits without interpretation func Message(r reason.Kind, format string, args ...out.V) { if r.ID == "" { @@ -54,7 +64,15 @@ func Message(r reason.Kind, format string, args ...out.V) { out.Error(r, "Exiting due to {{.fatal_code}}: {{.fatal_msg}}", args...) } - os.Exit(r.ExitCode) + Code(r.ExitCode) +} + +// Code will exit with a code +func Code(code int) { + if shell { + out.Output(os.Stdout, fmt.Sprintf("false exit code %d\n", code)) + } + os.Exit(code) } // Advice is syntactic sugar to output a message with dynamically generated advice diff --git a/pkg/minikube/mustload/mustload.go b/pkg/minikube/mustload/mustload.go index cbda0b3893..1634ad318f 100644 --- a/pkg/minikube/mustload/mustload.go +++ b/pkg/minikube/mustload/mustload.go @@ -20,7 +20,6 @@ package mustload import ( "fmt" "net" - "os" "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/host" @@ -175,5 +174,5 @@ func ExampleCmd(cname string, action string) string { func exitTip(action string, profile string, code int) { command := ExampleCmd(profile, action) out.Styled(style.Workaround, `To start a cluster, run: "{{.command}}"`, out.V{"command": command}) - os.Exit(code) + exit.Code(code) } diff --git a/pkg/minikube/out/out.go b/pkg/minikube/out/out.go index cd85c64a6c..526c347790 100644 --- a/pkg/minikube/out/out.go +++ b/pkg/minikube/out/out.go @@ -50,6 +50,8 @@ import ( // NOTE: If you do not want colorized output, set MINIKUBE_IN_STYLE=false in your environment. var ( + // silent will disable all output, if called from a script. Set using SetSilent() + silent bool // outFile is where Out* functions send output to. Set using SetOutFile() outFile fdWriter // errFile is where Err* functions send output to. Set using SetErrFile() @@ -122,6 +124,11 @@ func String(format string, a ...interface{}) { // Flush log buffer so that output order makes sense klog.Flush() + if silent { + klog.Infof(format, a...) + return + } + if outFile == nil { klog.Warningf("[unset outFile]: %s", fmt.Sprintf(format, a...)) return @@ -131,7 +138,13 @@ func String(format string, a ...interface{}) { if spin.Active() { spin.Stop() } - _, err := fmt.Fprintf(outFile, format, a...) + + Output(outFile, format, a...) +} + +// Output writes a basic formatted string +func Output(file fdWriter, format string, a ...interface{}) { + _, err := fmt.Fprintf(file, format, a...) if err != nil { klog.Errorf("Fprintf failed: %v", err) } @@ -152,10 +165,7 @@ func spinnerString(format string, a ...interface{}) { if spin.Active() { spin.Stop() } - _, err := fmt.Fprintf(outFile, format, a...) - if err != nil { - klog.Errorf("Fprintf failed: %v", err) - } + Output(outFile, format, a...) // Start spinning at the end of the printed line spin.Start() } @@ -194,10 +204,7 @@ func Err(format string, a ...interface{}) { if spin.Active() { spin.Stop() } - _, err := fmt.Fprintf(errFile, format, a...) - if err != nil { - klog.Errorf("Fprint failed: %v", err) - } + Output(errFile, format, a...) } // ErrLn writes a basic formatted string with a newline to stderr @@ -234,6 +241,20 @@ func FailureT(format string, a ...V) { ErrT(style.Failure, format, a...) } +// IsTerminal returns whether we have a terminal or not +func IsTerminal(w fdWriter) bool { + fd := w.Fd() + isT := isatty.IsTerminal(fd) + klog.Infof("isatty.IsTerminal(%d) = %v\n", fd, isT) + return isT +} + +// SetSilent configures whether output is disabled or not +func SetSilent(q bool) { + klog.Infof("Setting silent to %v", q) + silent = q +} + // SetOutFile configures which writer standard output goes to. func SetOutFile(w fdWriter) { klog.Infof("Setting OutFile to fd %d ...", w.Fd()) From 1c4c88e94272df2a2133e4f94422db3537314781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Wed, 10 Mar 2021 08:17:03 +0100 Subject: [PATCH 2/2] Use helper for IsTerminal also in wantsColor --- pkg/minikube/out/out.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pkg/minikube/out/out.go b/pkg/minikube/out/out.go index 526c347790..bdc069ad96 100644 --- a/pkg/minikube/out/out.go +++ b/pkg/minikube/out/out.go @@ -243,10 +243,7 @@ func FailureT(format string, a ...V) { // IsTerminal returns whether we have a terminal or not func IsTerminal(w fdWriter) bool { - fd := w.Fd() - isT := isatty.IsTerminal(fd) - klog.Infof("isatty.IsTerminal(%d) = %v\n", fd, isT) - return isT + return isatty.IsTerminal(w.Fd()) } // SetSilent configures whether output is disabled or not @@ -259,7 +256,7 @@ func SetSilent(q bool) { func SetOutFile(w fdWriter) { klog.Infof("Setting OutFile to fd %d ...", w.Fd()) outFile = w - useColor = wantsColor(w.Fd()) + useColor = wantsColor(w) } // SetJSON configures printing to STDOUT in JSON @@ -272,11 +269,11 @@ func SetJSON(j bool) { func SetErrFile(w fdWriter) { klog.Infof("Setting ErrFile to fd %d...", w.Fd()) errFile = w - useColor = wantsColor(w.Fd()) + useColor = wantsColor(w) } // wantsColor determines if the user might want colorized output. -func wantsColor(fd uintptr) bool { +func wantsColor(w fdWriter) bool { // First process the environment: we allow users to force colors on or off. // // MINIKUBE_IN_STYLE=[1, T, true, TRUE] @@ -308,8 +305,8 @@ func wantsColor(fd uintptr) bool { return false } - isT := isatty.IsTerminal(fd) - klog.Infof("isatty.IsTerminal(%d) = %v\n", fd, isT) + isT := IsTerminal(w) + klog.Infof("isatty.IsTerminal(%d) = %v\n", w.Fd(), isT) return isT }