Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create filtered names (regex) for tags head #138

Merged
merged 1 commit into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sonar/cmd/tags_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

var (
method string
filterNameFl string
method string

headCmd = &cobra.Command{
Use: "head <image-name>",
Expand All @@ -21,7 +22,7 @@ but SemVer is also supported.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

dockerTags, err := docker.GetAllTags(args[0])
dockerTags, err := docker.GetFilteredTags(args[0], filterNameFl)
if err != nil {
return fmt.Errorf("Failed retrieving Docker tags: %s", err)
} else if len(dockerTags) == 0 {
Expand Down Expand Up @@ -79,5 +80,6 @@ but SemVer is also supported.`,
func init() {

headCmd.PersistentFlags().StringP("method", "m", "date", "Criteria to calculate the head tag. Supported values are 'date' (default) or 'semver'.")
headCmd.PersistentFlags().StringVar(&filterNameFl, "filter-name", ".*", "a regex of which tag names to include")
tagsCmd.AddCommand(headCmd)
}
31 changes: 31 additions & 0 deletions sonar/docker/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"net/http"
"regexp"
"time"
)

Expand Down Expand Up @@ -80,3 +81,33 @@ func GetAllTags(imageStr string) ([]Tag, error) {

return results, nil
}

func GetFilteredTags(imageStr string, nameRegex string) ([]Tag, error) {

allTags, err := GetAllTags(imageStr)
if err != nil {
return nil, err
}

var filteredTags []Tag
var invert bool

if nameRegex[0] == '!' {
invert = true
nameRegex = nameRegex[1:]
}

for _, tag := range allTags {

matched, err := regexp.MatchString(nameRegex, tag.Name)
if err != nil {
return nil, err
}

if matched != invert {
filteredTags = append(filteredTags, tag)
}
}

return filteredTags, nil
}