Skip to content

Commit

Permalink
Feat: delete just specified tags (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicianoTech committed Sep 15, 2021
1 parent f77553f commit 64aacae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sonar/cmd/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (

func init() {

tagsCmd.PersistentFlags().StringVar(&fieldFl, "field", "", "the field to filter what tags will be selected. Only 'date' is supported right now.")
tagsCmd.PersistentFlags().StringVar(&fieldFl, "field", "name", "the field to filter what tags will be selected.")
tagsCmd.PersistentFlags().StringVar(&gtFl, "gt", "", "filter tags 'greater than' this value - allowed values are based on the 'field' choosen. A relative time in seconds (s), minutes (m), hours (h), days (d), or weeks (w) is supported.")
tagsCmd.PersistentFlags().StringVar(&ltFl, "lt", "", "filter tags 'less than' this value - allowed values are based on the 'field' choosen. A relative time in seconds (s), minutes (m), hours (h), days (d), or weeks (w) is supported.")

Expand Down
69 changes: 45 additions & 24 deletions sonar/cmd/tags_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/felicianotech/sonar/sonar/docker"
Expand All @@ -22,52 +23,72 @@ var (
yesFl bool

tagsDeleteCmd = &cobra.Command{
Use: "delete <image-name>",
Use: `delete <image-name> <tag>[,<tag>[...]]
delete <image-name> --field`,
Short: "Deletes one or more tags based on a parameter",
Args: cobra.ExactArgs(1),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {

if fieldFl != "date" {
log.Fatal("'field' is a required field and must be 'date'.")
if fieldFl != "date" && fieldFl != "name" {
log.Fatal("'field' is not valid.")
os.Exit(1)
}

if gtFl == "" && ltFl == "" {
if gtFl == "" && ltFl == "" && fieldFl != "name" {
log.Fatal("Either the 'gt' or 'lt' flags need to be set.")
os.Exit(1)
}

gDuration, err := parseDuration(gtFl)
if err != nil {
return fmt.Errorf("Cannot parse duration from 'gt': %s", err)
}
gCutDate := time.Now().Add(-gDuration)

lDuration, err := parseDuration(ltFl)
if err != nil {
return fmt.Errorf("Cannot parse duration from 'lt': %s", err)
}
lCutDate := time.Now().Add(-lDuration)

dockerTags, err := docker.GetAllTags(args[0])
if err != nil {
return fmt.Errorf("Failed retrieving Docker tags: %s", err)
}
var tagsToDelete []docker.Tag

for _, tag := range dockerTags {
if fieldFl == "name" {

tagArgs := strings.Split(args[1], ",")

for _, tArg := range tagArgs {
for _, tag := range dockerTags {

if gtFl != "" && fieldFl == "date" {
if gCutDate.After(tag.Date) {
tagsToDelete = append(tagsToDelete, tag)
if tArg == tag.Name {
tagsToDelete = append(tagsToDelete, tag)
break
}
}
}
} else if fieldFl == "date" {

if ltFl != "" && fieldFl == "date" {
if lCutDate.Before(tag.Date) {
tagsToDelete = append(tagsToDelete, tag)
gDuration, err := parseDuration(gtFl)
if err != nil {
return fmt.Errorf("Cannot parse duration from 'gt': %s", err)
}
gCutDate := time.Now().Add(-gDuration)

lDuration, err := parseDuration(ltFl)
if err != nil {
return fmt.Errorf("Cannot parse duration from 'lt': %s", err)
}
lCutDate := time.Now().Add(-lDuration)

for _, tag := range dockerTags {

if gtFl != "" && fieldFl == "date" {
if gCutDate.After(tag.Date) {
tagsToDelete = append(tagsToDelete, tag)
}
}

if ltFl != "" && fieldFl == "date" {
if lCutDate.Before(tag.Date) {
tagsToDelete = append(tagsToDelete, tag)
}
}
}
} else {
log.Fatal("'field' is not valid.")
os.Exit(1)
}

if len(tagsToDelete) == 0 {
Expand Down

0 comments on commit 64aacae

Please sign in to comment.