Skip to content

Commit 71b2b99

Browse files
committed
hide progress bars when not running in a tty
This should clean up some CI logs.
1 parent 3c27445 commit 71b2b99

8 files changed

+19
-9
lines changed

cmd/image.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"os"
89
"strings"
910

1011
"github.com/google/go-containerregistry/pkg/authn"
@@ -14,6 +15,7 @@ import (
1415
"github.com/spf13/cobra"
1516
"github.com/vbauerster/mpb/v7"
1617
"github.com/vbauerster/mpb/v7/decor"
18+
"golang.org/x/term"
1719

1820
"github.com/LINBIT/virter/internal/virter"
1921
)
@@ -189,6 +191,14 @@ func imageCommand() *cobra.Command {
189191
return imageCmd
190192
}
191193

194+
func DefaultContainerOpt() mpb.ContainerOption {
195+
if term.IsTerminal(int(os.Stderr.Fd())) {
196+
return mpb.WithOutput(os.Stderr)
197+
}
198+
199+
return mpb.WithOutput(nil)
200+
}
201+
192202
type mpbProgress struct {
193203
*mpb.Progress
194204
}

cmd/image_build.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func imageBuildCommand() *cobra.Command {
106106

107107
shutdownTimeout := viper.GetDuration("time.shutdown_timeout")
108108

109-
p := mpb.NewWithContext(ctx)
109+
p := mpb.NewWithContext(ctx, DefaultContainerOpt())
110110

111111
baseImage, err := GetLocalImage(ctx, baseImageName, baseImageName, v, pullPolicy, DefaultProgressFormat(p))
112112
if err != nil {
@@ -136,7 +136,7 @@ func imageBuildCommand() *cobra.Command {
136136
} else if unchanged {
137137
log.Info("Image already up-to-date, skipping provision, pulling instead")
138138

139-
p := mpb.NewWithContext(ctx)
139+
p := mpb.NewWithContext(ctx, DefaultContainerOpt())
140140

141141
_, err := GetLocalImage(ctx, newImageName, args[1], v, PullPolicyAlways, DefaultProgressFormat(p))
142142
if err != nil {
@@ -193,7 +193,7 @@ func imageBuildCommand() *cobra.Command {
193193
ResetMachineID: resetMachineID,
194194
}
195195

196-
p = mpb.NewWithContext(ctx)
196+
p = mpb.NewWithContext(ctx, DefaultContainerOpt())
197197

198198
err = v.ImageBuild(ctx, tools, vmConfig, sshPingConfig, buildConfig, virter.WithProgress(DefaultProgressFormat(p)))
199199
if err != nil {

cmd/image_load.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ read from stdin`,
3636
log.WithError(err).Fatal("error creating import layer")
3737
}
3838

39-
p := mpb.New()
39+
p := mpb.New(DefaultContainerOpt())
4040

4141
var in io.Reader
4242
if len(args) == 1 {

cmd/image_pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ will be used.`,
3131
}
3232
defer v.ForceDisconnect()
3333

34-
p := mpb.New()
34+
p := mpb.New(DefaultContainerOpt())
3535

3636
ctx, cancel := onInterruptWrap(context.Background())
3737
defer cancel()

cmd/image_push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func imagePushCommand() *cobra.Command {
3636
}
3737
defer v.ForceDisconnect()
3838

39-
p := mpb.NewWithContext(ctx)
39+
p := mpb.NewWithContext(ctx, DefaultContainerOpt())
4040

4141
img, err := GetLocalImage(ctx, source, source, v, PullPolicyNever, DefaultProgressFormat(p))
4242
if err != nil {

cmd/image_save.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ squashed into a single file.`,
5151
out = f
5252
}
5353

54-
p := mpb.NewWithContext(ctx, mpb.WithOutput(os.Stderr))
54+
p := mpb.NewWithContext(ctx, DefaultContainerOpt())
5555

5656
imgRef, err := GetLocalImage(ctx, image, image, v, PullPolicyNever, DefaultProgressFormat(p))
5757
if err != nil {

cmd/vm_commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ the virtual machine name if no other value is given.`,
4040
ctx, cancel := onInterruptWrap(context.Background())
4141
defer cancel()
4242

43-
p := mpb.NewWithContext(ctx)
43+
p := mpb.NewWithContext(ctx, DefaultContainerOpt())
4444

4545
err = v.VMCommit(ctx, actualtime.ActualTime{}, vmName, imageName, shutdown, shutdownTimeout, viper.GetBool("libvirt.static_dhcp"), virter.WithProgress(DefaultProgressFormat(p)))
4646
if err != nil {

cmd/vm_run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func vmRunCommand() *cobra.Command {
134134
log.Fatal(err)
135135
}
136136

137-
p := mpb.New()
137+
p := mpb.New(DefaultContainerOpt())
138138
image, err := GetLocalImage(ctx, args[0], args[0], v, pullPolicy, DefaultProgressFormat(p))
139139
if err != nil {
140140
log.Fatalf("Error while getting image: %v", err)

0 commit comments

Comments
 (0)