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).
pull/10763/head
Anders F Björklund 2021-03-09 17:44:12 +01:00
parent 3760bf765e
commit 5e566d3039
5 changed files with 60 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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())