Skip to content

Commit

Permalink
Support quiet in push cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Dong <jindon@amazon.com>
  • Loading branch information
djdongjin committed Mar 25, 2023
1 parent a3415fb commit 86d1c2d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
8 changes: 8 additions & 0 deletions cmd/nerdctl/image_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func newPushCommand() *cobra.Command {
pushCommand.Flags().String("notation-key-name", "", "Signing key name for a key previously added to notation's key list for --sign=notation")
// #endregion

pushCommand.Flags().BoolP("quiet", "q", false, "Suppress verbose output")

pushCommand.Flags().Bool(allowNonDistFlag, false, "Allow pushing images with non-distributable blobs")

return pushCommand
Expand Down Expand Up @@ -99,6 +101,11 @@ func processImagePushOptions(cmd *cobra.Command) (types.ImagePushOptions, error)
if err != nil {
return types.ImagePushOptions{}, err
}

quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return types.ImagePushOptions{}, err
}
allowNonDist, err := cmd.Flags().GetBool(allowNonDistFlag)
if err != nil {
return types.ImagePushOptions{}, err
Expand All @@ -113,6 +120,7 @@ func processImagePushOptions(cmd *cobra.Command) (types.ImagePushOptions, error)
Sign: sign,
CosignKey: cosignKey,
NotationKeyName: notationKeyName,
Quiet: quiet,
AllowNondistributableArtifacts: allowNonDist,
Stdout: cmd.OutOrStdout(),
}, nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/types/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ type ImagePushOptions struct {
CosignKey string
// NotationKeyName Signing key name for a key previously added to notation's key list for --sign=notation
NotationKeyName string
// Suppress verbose output
Quiet bool
// AllowNondistributableArtifacts allow pushing non-distributable artifacts
AllowNondistributableArtifacts bool
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/image/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/sirupsen/logrus"
)

// Push pushes an image specified by `rawRef`.
func Push(ctx context.Context, client *containerd.Client, rawRef string, options types.ImagePushOptions) error {
if scheme, ref, err := referenceutil.ParseIPFSRefWithScheme(rawRef); err == nil {
if scheme != "ipfs" {
Expand Down Expand Up @@ -116,7 +117,7 @@ func Push(ctx context.Context, client *containerd.Client, rawRef string, options
}

pushFunc := func(r remotes.Resolver) error {
return push.Push(ctx, client, r, options.Stdout, pushRef, ref, platMC, options.AllowNondistributableArtifacts)
return push.Push(ctx, client, r, options.Stdout, pushRef, ref, platMC, options.AllowNondistributableArtifacts, options.Quiet)
}

var dOpts []dockerconfigresolver.Opt
Expand Down
53 changes: 28 additions & 25 deletions pkg/imgutil/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import (
"golang.org/x/sync/errgroup"
)

// Push pushes an image to a remote registry.
func Push(ctx context.Context, client *containerd.Client, resolver remotes.Resolver, stdout io.Writer,
localRef, remoteRef string, platform platforms.MatchComparer, allowNonDist bool) error {
localRef, remoteRef string, platform platforms.MatchComparer, allowNonDist, quiet bool) error {
img, err := client.ImageService().Get(ctx, localRef)
if err != nil {
return fmt.Errorf("unable to resolve image to manifest: %w", err)
Expand Down Expand Up @@ -77,37 +78,39 @@ func Push(ctx context.Context, client *containerd.Client, resolver remotes.Resol
)
})

eg.Go(func() error {
var (
ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(stdout)
start = time.Now()
done bool
)
if !quiet {
eg.Go(func() error {
var (
ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(stdout)
start = time.Now()
done bool
)

defer ticker.Stop()
defer ticker.Stop()

for {
select {
case <-ticker.C:
fw.Flush()
for {
select {
case <-ticker.C:
fw.Flush()

tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)
tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)

jobs.Display(tw, ongoing.status(), start)
tw.Flush()
jobs.Display(tw, ongoing.status(), start)
tw.Flush()

if done {
fw.Flush()
return nil
if done {
fw.Flush()
return nil
}
case <-doneCh:
done = true
case <-ctx.Done():
done = true // allow ui to update once more
}
case <-doneCh:
done = true
case <-ctx.Done():
done = true // allow ui to update once more
}
}
})
})
}
return eg.Wait()
}

Expand Down

0 comments on commit 86d1c2d

Please sign in to comment.