Skip to content

Commit

Permalink
feat: support excluding digest tags (oras-project#668)
Browse files Browse the repository at this point in the history
Making this PR draft since I found that it's important to write good
example and long description for the new flag.
Resolves oras-project#658
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah authored and Terry Howe committed Feb 2, 2023
1 parent b7d9cfb commit a9f0864
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions cmd/oras/repository/show-tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ package repository

import (
"fmt"
"strings"

"github.com/opencontainers/go-digest"
"github.com/spf13/cobra"
"oras.land/oras/cmd/oras/internal/option"
)

type showTagsOptions struct {
option.Remote
option.Common
targetRef string
last string
targetRef string
last string
excludeDigestTag bool
}

func showTagsCmd() *cobra.Command {
Expand All @@ -41,6 +44,9 @@ func showTagsCmd() *cobra.Command {
Example - Show tags of the target repository:
oras repository show-tags localhost:5000/hello
Example - Show tags in the target repository with digest-like tags hidden:
oras repository show-tags --exclude-digest-tag localhost:5000/hello
Example - Show tags of the target repository that include values lexically after last:
oras repository show-tags --last "last_tag" localhost:5000/hello
`,
Expand All @@ -52,6 +58,7 @@ Example - Show tags of the target repository that include values lexically after
},
}
cmd.Flags().StringVar(&opts.last, "last", "", "start after the tag specified by `last`")
cmd.Flags().BoolVar(&opts.excludeDigestTag, "exclude-digest-tags", false, "exclude all digest-like tags such as 'sha256-aaaa...'")
option.ApplyFlags(&opts, cmd.Flags())
return cmd
}
Expand All @@ -64,8 +71,17 @@ func showTags(opts showTagsOptions) error {
}
return repo.Tags(ctx, opts.last, func(tags []string) error {
for _, tag := range tags {
if opts.excludeDigestTag && isDigestTag(tag) {
continue
}
fmt.Println(tag)
}
return nil
})
}

func isDigestTag(tag string) bool {
dgst := strings.Replace(tag, "-", ":", 1)
_, err := digest.Parse(dgst)
return err == nil
}

0 comments on commit a9f0864

Please sign in to comment.