Merge pull request #10763 from afbjorklund/silent-shell

Silent output when talking to a shell
pull/10787/head
Medya Ghazizadeh 2021-03-10 13:26:41 -08:00 committed by GitHub
commit df7cb644af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 17 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,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
@ -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]
@ -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
}