Skip to content

Commit 5e566d3

Browse files
committed
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).
1 parent 3760bf7 commit 5e566d3

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

cmd/minikube/cmd/docker-env.go

+5
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ var dockerEnvCmd = &cobra.Command{
260260
return
261261
}
262262

263+
if !out.IsTerminal(os.Stdout) {
264+
out.SetSilent(true)
265+
exit.SetShell(true)
266+
}
267+
263268
cname := ClusterFlagValue()
264269
co := mustload.Running(cname)
265270
driverName := co.CP.Host.DriverName

cmd/minikube/cmd/podman-env.go

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ var podmanEnvCmd = &cobra.Command{
153153
return
154154
}
155155

156+
if !out.IsTerminal(os.Stdout) {
157+
out.SetSilent(true)
158+
exit.SetShell(true)
159+
}
160+
156161
cname := ClusterFlagValue()
157162
co := mustload.Running(cname)
158163
driverName := co.CP.Host.DriverName

pkg/minikube/exit/exit.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
package exit
1919

2020
import (
21+
"fmt"
2122
"os"
2223
"runtime"
2324

@@ -27,6 +28,15 @@ import (
2728
"k8s.io/minikube/pkg/minikube/style"
2829
)
2930

31+
var (
32+
shell bool
33+
)
34+
35+
// SetShell configures if we are doing a shell configuration or not
36+
func SetShell(s bool) {
37+
shell = s
38+
}
39+
3040
// Message outputs a templated message and exits without interpretation
3141
func Message(r reason.Kind, format string, args ...out.V) {
3242
if r.ID == "" {
@@ -54,7 +64,15 @@ func Message(r reason.Kind, format string, args ...out.V) {
5464
out.Error(r, "Exiting due to {{.fatal_code}}: {{.fatal_msg}}", args...)
5565
}
5666

57-
os.Exit(r.ExitCode)
67+
Code(r.ExitCode)
68+
}
69+
70+
// Code will exit with a code
71+
func Code(code int) {
72+
if shell {
73+
out.Output(os.Stdout, fmt.Sprintf("false exit code %d\n", code))
74+
}
75+
os.Exit(code)
5876
}
5977

6078
// Advice is syntactic sugar to output a message with dynamically generated advice

pkg/minikube/mustload/mustload.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package mustload
2020
import (
2121
"fmt"
2222
"net"
23-
"os"
2423

2524
"github.com/docker/machine/libmachine"
2625
"github.com/docker/machine/libmachine/host"
@@ -175,5 +174,5 @@ func ExampleCmd(cname string, action string) string {
175174
func exitTip(action string, profile string, code int) {
176175
command := ExampleCmd(profile, action)
177176
out.Styled(style.Workaround, `To start a cluster, run: "{{.command}}"`, out.V{"command": command})
178-
os.Exit(code)
177+
exit.Code(code)
179178
}

pkg/minikube/out/out.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import (
5050
// NOTE: If you do not want colorized output, set MINIKUBE_IN_STYLE=false in your environment.
5151

5252
var (
53+
// silent will disable all output, if called from a script. Set using SetSilent()
54+
silent bool
5355
// outFile is where Out* functions send output to. Set using SetOutFile()
5456
outFile fdWriter
5557
// errFile is where Err* functions send output to. Set using SetErrFile()
@@ -122,6 +124,11 @@ func String(format string, a ...interface{}) {
122124
// Flush log buffer so that output order makes sense
123125
klog.Flush()
124126

127+
if silent {
128+
klog.Infof(format, a...)
129+
return
130+
}
131+
125132
if outFile == nil {
126133
klog.Warningf("[unset outFile]: %s", fmt.Sprintf(format, a...))
127134
return
@@ -131,7 +138,13 @@ func String(format string, a ...interface{}) {
131138
if spin.Active() {
132139
spin.Stop()
133140
}
134-
_, err := fmt.Fprintf(outFile, format, a...)
141+
142+
Output(outFile, format, a...)
143+
}
144+
145+
// Output writes a basic formatted string
146+
func Output(file fdWriter, format string, a ...interface{}) {
147+
_, err := fmt.Fprintf(file, format, a...)
135148
if err != nil {
136149
klog.Errorf("Fprintf failed: %v", err)
137150
}
@@ -152,10 +165,7 @@ func spinnerString(format string, a ...interface{}) {
152165
if spin.Active() {
153166
spin.Stop()
154167
}
155-
_, err := fmt.Fprintf(outFile, format, a...)
156-
if err != nil {
157-
klog.Errorf("Fprintf failed: %v", err)
158-
}
168+
Output(outFile, format, a...)
159169
// Start spinning at the end of the printed line
160170
spin.Start()
161171
}
@@ -194,10 +204,7 @@ func Err(format string, a ...interface{}) {
194204
if spin.Active() {
195205
spin.Stop()
196206
}
197-
_, err := fmt.Fprintf(errFile, format, a...)
198-
if err != nil {
199-
klog.Errorf("Fprint failed: %v", err)
200-
}
207+
Output(errFile, format, a...)
201208
}
202209

203210
// ErrLn writes a basic formatted string with a newline to stderr
@@ -234,6 +241,20 @@ func FailureT(format string, a ...V) {
234241
ErrT(style.Failure, format, a...)
235242
}
236243

244+
// IsTerminal returns whether we have a terminal or not
245+
func IsTerminal(w fdWriter) bool {
246+
fd := w.Fd()
247+
isT := isatty.IsTerminal(fd)
248+
klog.Infof("isatty.IsTerminal(%d) = %v\n", fd, isT)
249+
return isT
250+
}
251+
252+
// SetSilent configures whether output is disabled or not
253+
func SetSilent(q bool) {
254+
klog.Infof("Setting silent to %v", q)
255+
silent = q
256+
}
257+
237258
// SetOutFile configures which writer standard output goes to.
238259
func SetOutFile(w fdWriter) {
239260
klog.Infof("Setting OutFile to fd %d ...", w.Fd())

0 commit comments

Comments
 (0)