Skip to content

Commit

Permalink
WIP: disable spinners when not running in a tty
Browse files Browse the repository at this point in the history
Using a regular log line instead.
  • Loading branch information
r4f4 committed Jan 29, 2025
1 parent 0a1dcf6 commit ec33f84
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
6 changes: 5 additions & 1 deletion v2/internal/pkg/batch/concurrent_chan_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package batch
import (
"context"
"fmt"
"io"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (o *ChannelConcurrentBatch) Worker(ctx context.Context, collectorSchema v2a
o.Log.Info(emoji.Pushpin+" images to %s %d ", opts.Function, len(collectorSchema.AllImages))

total := len(collectorSchema.AllImages)
p := mpb.New(mpb.PopCompletedMode())
p := mpb.New(mpb.PopCompletedMode(), mpb.ContainerOptional(mpb.WithOutput(io.Discard), !opts.Global.IsTerminal))
results := make(chan GoroutineResult, total)
progressCh := make(chan int, total)
semaphore := make(chan struct{}, o.MaxGoroutines)
Expand All @@ -92,6 +93,9 @@ func (o *ChannelConcurrentBatch) Worker(ctx context.Context, collectorSchema v2a
default:
}

if !opts.Global.IsTerminal {
o.Log.Info("Batch copying %s to %s", img.Origin, img.Destination)
}
sp := newSpinner(img, opts.LocalStorageFQDN, p)

semaphore <- struct{}{}
Expand Down
6 changes: 5 additions & 1 deletion v2/internal/pkg/batch/concurrent_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package batch
import (
"context"
"fmt"
"io"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -64,7 +65,7 @@ func (o *ConcurrentBatch) Worker(ctx context.Context, collectorSchema v2alpha1.C
imgOverallIndex := 0
for batchIndex, batch := range batches {

p := mpb.New()
p := mpb.New(mpb.ContainerOptional(mpb.WithOutput(io.Discard), !opts.Global.IsTerminal))
total := len(collectorSchema.AllImages)
for _, img := range batch.Images.AllImages {
imgOverallIndex++
Expand All @@ -73,6 +74,9 @@ func (o *ConcurrentBatch) Worker(ctx context.Context, collectorSchema v2alpha1.C
if strings.Contains(img.Destination, opts.LocalStorageFQDN) {
imageText += emoji.RightArrow + " cache "
}
if !opts.Global.IsTerminal {
o.Log.Info("Batch copying %s to %s", img.Origin, img.Destination)
}
spinner := p.AddSpinner(
1, mpb.BarFillerMiddleware(spinners.PositionSpinnerLeft),
mpb.BarWidth(3),
Expand Down
10 changes: 8 additions & 2 deletions v2/internal/pkg/cli/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"text/template"
"time"

"golang.org/x/term"
"k8s.io/kubectl/pkg/util/templates"

"github.com/distribution/distribution/v3/configuration"
Expand Down Expand Up @@ -154,8 +155,11 @@ func NewMirrorCmd(log clog.PluggableLoggerInterface) *cobra.Command {

global := &mirror.GlobalOptions{
SecurePolicy: false,
IsTerminal: term.IsTerminal(int(os.Stdout.Fd())),
}

fmt.Printf("Are we running from a terminal? %v\n", global.IsTerminal)

flagSharedOpts, sharedOpts := mirror.SharedImageFlags()
flagDepTLS, deprecatedTLSVerifyOpt := mirror.DeprecatedTLSVerifyFlags()
flagSrcOpts, srcOpts := mirror.ImageSrcFlags(global, sharedOpts, deprecatedTLSVerifyOpt, "src-", "screds")
Expand Down Expand Up @@ -1151,13 +1155,15 @@ func (o *ExecutorSchema) RebuildCatalogs(ctx context.Context, operatorImgs v2alp
o.Log.Info(emoji.RepeatSingleButton + " rebuilding catalogs")

for _, copyImage := range oImgs {

if copyImage.Type == v2alpha1.TypeOperatorCatalog {
if o.Opts.IsMirrorToMirror() && strings.Contains(copyImage.Source, o.Opts.LocalStorageFQDN) {
// CLID-275: this is the ref to the already rebuilt catalog, which needs to be mirrored to destination.
continue
}
p := mpb.New()
if !o.Opts.Global.IsTerminal {
o.Log.Info("Rebuilding catalog %s", copyImage.Origin)
}
p := mpb.New(mpb.ContainerOptional(mpb.WithOutput(io.Discard), !o.Opts.Global.IsTerminal))
spinner := p.AddSpinner(
1, mpb.BarFillerMiddleware(spinners.PositionSpinnerLeft),
mpb.BarWidth(3),
Expand Down
1 change: 1 addition & 0 deletions v2/internal/pkg/mirror/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type GlobalOptions struct {
DeleteID string // This flag is used to append to the artifacts created by the delete functionality
DeleteYaml string // This flag will use the contents of the indicated yaml as basis to delete the local cache and remote registry
CacheDir string // Path to the cache directory
IsTerminal bool // Whether we're running in a terminal console or not
}

type CopyOptions struct {
Expand Down
5 changes: 4 additions & 1 deletion v2/internal/pkg/operator/filtered_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ func (o *FilterCollector) OperatorImageCollector(ctx context.Context) (v2alpha1.
// download the operator index image
o.Log.Debug(collectorPrefix+"copying operator image %s", op.Catalog)

if !o.Opts.Global.IsTerminal {
o.Log.Info("Collecting catalog %s", op.Catalog)
}
// prepare spinner
p := mpb.New()
p := mpb.New(mpb.ContainerOptional(mpb.WithOutput(io.Discard), !o.Opts.Global.IsTerminal))
spinner := p.AddSpinner(
1, mpb.BarFillerMiddleware(spinners.PositionSpinnerLeft),
mpb.BarWidth(3),
Expand Down

0 comments on commit ec33f84

Please sign in to comment.