Skip to content

Commit

Permalink
registry: Add support for ListRepositoriesV2 and ListManifests comman…
Browse files Browse the repository at this point in the history
…ds. (#1069)

* registry: Add support for ListRepositoriesV2 and ListManifests commands.

* registry: hide `list` to promote `list-v2`

Co-authored-by: Andrew Starr-Bochicchio <[email protected]>

Co-authored-by: Collin Shoop <[email protected]>
Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2021
1 parent b1f1b24 commit 22869cb
Show file tree
Hide file tree
Showing 15 changed files with 847 additions and 4 deletions.
103 changes: 103 additions & 0 deletions commands/displayers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,61 @@ func (r *Repository) KV() []map[string]interface{} {
return out
}

type RepositoryV2 struct {
Repositories []do.RepositoryV2
}

var _ Displayable = &Repository{}

func (r *RepositoryV2) JSON(out io.Writer) error {
return writeJSON(r.Repositories, out)
}

func (r *RepositoryV2) Cols() []string {
return []string{
"Name",
"LatestManifest",
"LatestTag",
"TagCount",
"ManifestCount",
"UpdatedAt",
}
}

func (r *RepositoryV2) ColMap() map[string]string {
return map[string]string{
"Name": "Name",
"LatestManifest": "Latest Manifest",
"LatestTag": "Latest Tag",
"TagCount": "Tag Count",
"ManifestCount": "Manifest Count",
"UpdatedAt": "Updated At",
}
}

func (r *RepositoryV2) KV() []map[string]interface{} {
out := make([]map[string]interface{}, 0, len(r.Repositories))

for _, reg := range r.Repositories {
latestTag := "<none>" // default when latest manifest has no tags
if len(reg.LatestManifest.Tags) > 0 {
latestTag = reg.LatestManifest.Tags[0]
}
m := map[string]interface{}{
"Name": reg.Name,
"LatestManifest": reg.LatestManifest.Digest,
"LatestTag": latestTag,
"TagCount": reg.TagCount,
"ManifestCount": reg.ManifestCount,
"UpdatedAt": reg.LatestManifest.UpdatedAt,
}

out = append(out, m)
}

return out
}

type RepositoryTag struct {
Tags []do.RepositoryTag
}
Expand Down Expand Up @@ -149,6 +204,54 @@ func (r *RepositoryTag) KV() []map[string]interface{} {
return out
}

type RepositoryManifest struct {
Manifests []do.RepositoryManifest
}

var _ Displayable = &RepositoryManifest{}

func (r *RepositoryManifest) JSON(out io.Writer) error {
return writeJSON(r.Manifests, out)
}

func (r *RepositoryManifest) Cols() []string {
return []string{
"Digest",
"CompressedSizeBytes",
"SizeBytes",
"UpdatedAt",
"Tags",
}
}

func (r *RepositoryManifest) ColMap() map[string]string {
return map[string]string{
"Digest": "Manifest Digest",
"CompressedSizeBytes": "Compressed Size",
"SizeBytes": "Uncompressed Size",
"UpdatedAt": "Updated At",
"Tags": "Tags",
}
}

func (r *RepositoryManifest) KV() []map[string]interface{} {
out := make([]map[string]interface{}, 0, len(r.Manifests))

for _, manifest := range r.Manifests {
m := map[string]interface{}{
"Digest": manifest.Digest,
"CompressedSizeBytes": BytesToHumanReadableUnit(manifest.CompressedSizeBytes),
"SizeBytes": BytesToHumanReadableUnit(manifest.SizeBytes),
"UpdatedAt": manifest.UpdatedAt,
"Tags": manifest.Tags,
}

out = append(out, m)
}

return out
}

type GarbageCollection struct {
GarbageCollections []do.GarbageCollection
}
Expand Down
80 changes: 80 additions & 0 deletions commands/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ func Repository() *Command {
RunListRepositories, "list",
"List repositories for a container registry", listRepositoriesDesc,
Writer, aliasOpt("ls"), displayerType(&displayers.Repository{}),
hiddenCmd(),
)

listRepositoriesV2Desc := `This command retrieves information about repositories in a registry, including:
- The repository name
- The latest manifest of the repository
- The latest manifest's latest tag, if any
- The number of tags in the repository
- The number of manifests in the repository
`

CmdBuilder(
cmd,
RunListRepositoriesV2, "list-v2",
"List repositories for a container registry", listRepositoriesV2Desc,
Writer, aliasOpt("ls2"), displayerType(&displayers.Repository{}),
)

listRepositoryTagsDesc := `This command retrieves information about tags in a repository, including:
Expand Down Expand Up @@ -169,6 +185,21 @@ func Repository() *Command {
)
AddBoolFlag(cmdRunRepositoryDeleteTag, doctl.ArgForce, doctl.ArgShortForce, false, "Force tag deletion")

listRepositoryManifests := `This command retrieves information about manifests in a repository, including:
- The manifest digest
- The compressed size
- The uncompressed size
- The last updated timestamp
- The manifest tags
- The manifest blobs (available in detailed output only)
`
CmdBuilder(
cmd,
RunListRepositoryManifests, "list-manifests <repository>",
"List manifests for a repository in a container registry", listRepositoryManifests,
Writer, aliasOpt("lm"), displayerType(&displayers.RepositoryManifest{}),
)

deleteManifestDesc := "This command permanently deletes one or more repository manifests by digest."
cmdRunRepositoryDeleteManifest := CmdBuilder(
cmd,
Expand Down Expand Up @@ -537,6 +568,21 @@ func RunListRepositories(c *CmdConfig) error {
return displayRepositories(c, repositories...)
}

// RunListRepositoriesV2 lists repositories for the registry
func RunListRepositoriesV2(c *CmdConfig) error {
registry, err := c.Registry().Get()
if err != nil {
return fmt.Errorf("failed to get registry: %w", err)
}

repositories, err := c.Registry().ListRepositoriesV2(registry.Name)
if err != nil {
return err
}

return displayRepositoriesV2(c, repositories...)
}

// RunListRepositoryTags lists tags for the repository in a registry
func RunListRepositoryTags(c *CmdConfig) error {
err := ensureOneArg(c)
Expand All @@ -557,6 +603,26 @@ func RunListRepositoryTags(c *CmdConfig) error {
return displayRepositoryTags(c, tags...)
}

// RunListRepositoryManifests lists manifests for the repository in a registry
func RunListRepositoryManifests(c *CmdConfig) error {
err := ensureOneArg(c)
if err != nil {
return err
}

registry, err := c.Registry().Get()
if err != nil {
return fmt.Errorf("failed to get registry: %w", err)
}

manifests, err := c.Registry().ListRepositoryManifests(registry.Name, c.Args[0])
if err != nil {
return err
}

return displayRepositoryManifests(c, manifests...)
}

// RunRepositoryDeleteTag deletes one or more repository tags
func RunRepositoryDeleteTag(c *CmdConfig) error {
force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
Expand Down Expand Up @@ -645,13 +711,27 @@ func displayRepositories(c *CmdConfig, repositories ...do.Repository) error {
return c.Display(item)
}

func displayRepositoriesV2(c *CmdConfig, repositories ...do.RepositoryV2) error {
item := &displayers.RepositoryV2{
Repositories: repositories,
}
return c.Display(item)
}

func displayRepositoryTags(c *CmdConfig, tags ...do.RepositoryTag) error {
item := &displayers.RepositoryTag{
Tags: tags,
}
return c.Display(item)
}

func displayRepositoryManifests(c *CmdConfig, manifests ...do.RepositoryManifest) error {
item := &displayers.RepositoryManifest{
Manifests: manifests,
}
return c.Display(item)
}

// Garbage Collection run commands

// RunStartGarbageCollection starts a garbage collection for the specified
Expand Down
Loading

0 comments on commit 22869cb

Please sign in to comment.