Skip to content

Commit

Permalink
Merge pull request #10763 from afbjorklund/silent-shell
Browse files Browse the repository at this point in the history
Silent output when talking to a shell
  • Loading branch information
medyagh authored Mar 10, 2021
2 parents 201c53e + 1c4c88e commit df7cb64
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
5 changes: 5 additions & 0 deletions cmd/minikube/cmd/docker-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cmd/minikube/cmd/podman-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion pkg/minikube/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package exit

import (
"fmt"
"os"
"runtime"

Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/minikube/mustload/mustload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package mustload
import (
"fmt"
"net"
"os"

"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/host"
Expand Down Expand Up @@ -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)
}
46 changes: 32 additions & 14 deletions pkg/minikube/out/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -234,11 +241,22 @@ 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 {
return isatty.IsTerminal(w.Fd())
}

// 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())
outFile = w
useColor = wantsColor(w.Fd())
useColor = wantsColor(w)
}

// SetJSON configures printing to STDOUT in JSON
Expand All @@ -251,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]
Expand Down Expand Up @@ -287,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
}

Expand Down

0 comments on commit df7cb64

Please sign in to comment.