Skip to content

Commit

Permalink
Add tags head command (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
felicianotech committed Mar 27, 2022
1 parent 23b073d commit a80edad
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ require (
github.com/spf13/viper v1.10.1
)

require github.com/docker/docker v20.10.12+incompatible
require (
github.com/docker/docker v20.10.12+incompatible
github.com/hashicorp/go-version v1.4.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down
83 changes: 83 additions & 0 deletions sonar/cmd/tags_head.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"fmt"

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

semver "github.com/hashicorp/go-version"
)

var (
method string

headCmd = &cobra.Command{
Use: "head <image-name>",
Short: "Returns the head (sequentially last) tag of the image",
Long: `Returns what is considered to be the most recent or latest tag for
an image, based on a criteria. By default, this criteria (or method) is date
but SemVer is also supported.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

dockerTags, err := docker.GetAllTags(args[0])
if err != nil {
return fmt.Errorf("Failed retrieving Docker tags: %s", err)
} else if len(dockerTags) == 0 {
return fmt.Errorf("The image %s doesn't have any tags.", args[0])
}

// start off with the first tag before we start comparing so that
// we're not comparing nil
headTag := dockerTags[0]

for _, tag := range dockerTags {

if method == "date" {

if tag.Date.After(headTag.Date) {
headTag = tag
}
} else if method == "semver" {

headTagV, err := semver.NewSemver(headTag.Name)
if err != nil {
// headTag isn't a semver tag so let's reset to the current
// tag in the loop and start over
headTag = tag
continue
}

tagV, err := semver.NewSemver(tag.Name)
if err != nil {
// the tag to compare isn't a semver tag so let's skip
continue
}

if headTagV.LessThan(tagV) {
headTag = tag
}
}
}

if method == "semver" {

_, err := semver.NewSemver(headTag.Name)
if err != nil {
return fmt.Errorf("The image %s doesn't contain at least 1 valid semver tag.", args[0])
}
}

fmt.Println(headTag.Name)

return nil
},
}
)

func init() {

headCmd.PersistentFlags().StringP("method", "m", "date", "Criteria to calculate the head tag. Supported values are 'date' (default) or 'semver'.")
tagsCmd.AddCommand(headCmd)
}

0 comments on commit a80edad

Please sign in to comment.