Skip to content

Commit

Permalink
hide progress bars when not running in a tty
Browse files Browse the repository at this point in the history
This should clean up some CI logs.
  • Loading branch information
WanzenBug committed Sep 2, 2021
1 parent 3c27445 commit 71b2b99
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 9 deletions.
10 changes: 10 additions & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"strings"

"github.com/google/go-containerregistry/pkg/authn"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
"golang.org/x/term"

"github.com/LINBIT/virter/internal/virter"
)
Expand Down Expand Up @@ -189,6 +191,14 @@ func imageCommand() *cobra.Command {
return imageCmd
}

func DefaultContainerOpt() mpb.ContainerOption {
if term.IsTerminal(int(os.Stderr.Fd())) {
return mpb.WithOutput(os.Stderr)
}

return mpb.WithOutput(nil)
}

type mpbProgress struct {
*mpb.Progress
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/image_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func imageBuildCommand() *cobra.Command {

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

p := mpb.NewWithContext(ctx)
p := mpb.NewWithContext(ctx, DefaultContainerOpt())

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

p := mpb.NewWithContext(ctx)
p := mpb.NewWithContext(ctx, DefaultContainerOpt())

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

p = mpb.NewWithContext(ctx)
p = mpb.NewWithContext(ctx, DefaultContainerOpt())

err = v.ImageBuild(ctx, tools, vmConfig, sshPingConfig, buildConfig, virter.WithProgress(DefaultProgressFormat(p)))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/image_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ read from stdin`,
log.WithError(err).Fatal("error creating import layer")
}

p := mpb.New()
p := mpb.New(DefaultContainerOpt())

var in io.Reader
if len(args) == 1 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ will be used.`,
}
defer v.ForceDisconnect()

p := mpb.New()
p := mpb.New(DefaultContainerOpt())

ctx, cancel := onInterruptWrap(context.Background())
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion cmd/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func imagePushCommand() *cobra.Command {
}
defer v.ForceDisconnect()

p := mpb.NewWithContext(ctx)
p := mpb.NewWithContext(ctx, DefaultContainerOpt())

img, err := GetLocalImage(ctx, source, source, v, PullPolicyNever, DefaultProgressFormat(p))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/image_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ squashed into a single file.`,
out = f
}

p := mpb.NewWithContext(ctx, mpb.WithOutput(os.Stderr))
p := mpb.NewWithContext(ctx, DefaultContainerOpt())

imgRef, err := GetLocalImage(ctx, image, image, v, PullPolicyNever, DefaultProgressFormat(p))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/vm_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ the virtual machine name if no other value is given.`,
ctx, cancel := onInterruptWrap(context.Background())
defer cancel()

p := mpb.NewWithContext(ctx)
p := mpb.NewWithContext(ctx, DefaultContainerOpt())

err = v.VMCommit(ctx, actualtime.ActualTime{}, vmName, imageName, shutdown, shutdownTimeout, viper.GetBool("libvirt.static_dhcp"), virter.WithProgress(DefaultProgressFormat(p)))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/vm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func vmRunCommand() *cobra.Command {
log.Fatal(err)
}

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

0 comments on commit 71b2b99

Please sign in to comment.