-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23b073d
commit a80edad
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |