Skip to content

Commit

Permalink
Add an aliases subcommand to tags. (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicianoTech authored Jan 22, 2021
1 parent 19464ef commit f1f872a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
51 changes: 51 additions & 0 deletions sonar/cmd/tags_aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"

"github.com/felicianotech/sonar/sonar/docker"

"github.com/spf13/cobra"
)

var (
tagsAliasCmd = &cobra.Command{
Use: "aliases <full-image-name>",
Short: "Display tags that point to the same image the given tag does",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

img, err := docker.ParseImageRef(args[0])
if err != nil {
return fmt.Errorf("Failed parsing image name: %s", err)
}

dockerTags, err := docker.GetAllTags(img.Namespace + "/" + img.Name)

var theDigest string

for _, tag := range dockerTags {

if tag.Name == img.Tag {
theDigest = tag.Digest
break
}
}

fmt.Printf("Listing aliases for the tag %s: \n\n", img.Tag)

for _, tag := range dockerTags {

if tag.Digest == theDigest && tag.Name != img.Tag {
fmt.Println(tag.Name)
}
}

return nil
},
}
)

func init() {
tagsCmd.AddCommand(tagsAliasCmd)
}
9 changes: 6 additions & 3 deletions sonar/docker/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

type Tag struct {
Name string
Size uint64
Date time.Time
Name string
Size uint64
Date time.Time
Digest string
}

func GetAllTags(image string) ([]Tag, error) {
Expand Down Expand Up @@ -41,6 +42,8 @@ func GetAllTags(image string) ([]Tag, error) {
aTag.Name = v.(map[string]interface{})["name"].(string)
aTag.Size = uint64(v.(map[string]interface{})["full_size"].(float64))
aTag.Date, err = time.Parse(time.RFC3339, v.(map[string]interface{})["last_updated"].(string))
anImage := v.(map[string]interface{})["images"].([]interface{})[0]
aTag.Digest = anImage.(map[string]interface{})["digest"].(string)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f1f872a

Please sign in to comment.