diff --git a/cmd/image.go b/cmd/image.go index 25a47f8..8548513 100644 --- a/cmd/image.go +++ b/cmd/image.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "os" "strings" "github.com/google/go-containerregistry/pkg/authn" @@ -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" ) @@ -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 } diff --git a/cmd/image_build.go b/cmd/image_build.go index 9e8d5ca..e44cf12 100644 --- a/cmd/image_build.go +++ b/cmd/image_build.go @@ -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 { @@ -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 { @@ -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 { diff --git a/cmd/image_load.go b/cmd/image_load.go index 78bc20b..d75a3b6 100644 --- a/cmd/image_load.go +++ b/cmd/image_load.go @@ -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 { diff --git a/cmd/image_pull.go b/cmd/image_pull.go index f2be591..1b931d2 100644 --- a/cmd/image_pull.go +++ b/cmd/image_pull.go @@ -31,7 +31,7 @@ will be used.`, } defer v.ForceDisconnect() - p := mpb.New() + p := mpb.New(DefaultContainerOpt()) ctx, cancel := onInterruptWrap(context.Background()) defer cancel() diff --git a/cmd/image_push.go b/cmd/image_push.go index 845707a..8c448ce 100644 --- a/cmd/image_push.go +++ b/cmd/image_push.go @@ -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 { diff --git a/cmd/image_save.go b/cmd/image_save.go index 246961c..55bc479 100644 --- a/cmd/image_save.go +++ b/cmd/image_save.go @@ -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 { diff --git a/cmd/vm_commit.go b/cmd/vm_commit.go index 4fc0e8c..685d205 100644 --- a/cmd/vm_commit.go +++ b/cmd/vm_commit.go @@ -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 { diff --git a/cmd/vm_run.go b/cmd/vm_run.go index 1c3f4fa..93ff6a4 100644 --- a/cmd/vm_run.go +++ b/cmd/vm_run.go @@ -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)