Skip to content

Commit

Permalink
targets-tag: Add "--append" option
Browse files Browse the repository at this point in the history
Fixes foundriesio#127

Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac authored and detsch committed Sep 21, 2023
1 parent 67ffcf8 commit 8c95b53
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions subcommands/targets/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

var (
tagTags string
tagAppend bool
tagNoTail bool
tagByVersion bool
)
Expand All @@ -34,11 +35,27 @@ func init() {
}
cmd.AddCommand(tagCmd)
tagCmd.Flags().StringVarP(&tagTags, "tags", "T", "", "comma,separate,list")
tagCmd.Flags().BoolVarP(&tagAppend, "append", "", false, "Append the given tags rather than set them")
tagCmd.Flags().BoolVarP(&tagNoTail, "no-tail", "", false, "Don't tail output of CI Job")
tagCmd.Flags().BoolVarP(&tagByVersion, "by-version", "", false, "Apply tags to all targets matching the given version(s)")
tagCmd.Flags().BoolVarP(&dryRun, "dryrun", "", false, "Just show the changes that would be applied")
}

func Set(a, b []string) []string {
unique := make([]string, len(a), len(a)+len(b))
items := make(map[string]bool, len(a))
for i, item := range a {
unique[i] = item
items[item] = true
}
for _, item := range b {
if ok := items[item]; !ok {
unique = append(unique, item)
}
}
return unique
}

func doTag(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
tags := strings.Split(tagTags, ",")
Expand All @@ -55,10 +72,14 @@ func doTag(cmd *cobra.Command, args []string) {
fmt.Printf("ERROR: %s\n", err)
} else {
if intersectionInSlices([]string{custom.Version}, args) {
targetTags := tags
if tagAppend {
targetTags = Set(custom.Tags, tags)
}
updates[name] = client.UpdateTarget{
Custom: client.TufCustom{Tags: tags},
Custom: client.TufCustom{Tags: targetTags},
}
fmt.Printf("Changing tags of %s from %s -> %s\n", name, custom.Tags, tags)
fmt.Printf("Changing tags of %s from %s -> %s\n", name, custom.Tags, targetTags)
}
}
}
Expand All @@ -71,10 +92,14 @@ func doTag(cmd *cobra.Command, args []string) {
if target, ok := targets[name]; ok {
custom, err := api.TargetCustom(target)
subcommands.DieNotNil(err)
targetTags := tags
if tagAppend {
targetTags = Set(custom.Tags, tags)
}
updates[name] = client.UpdateTarget{
Custom: client.TufCustom{Tags: tags},
Custom: client.TufCustom{Tags: targetTags},
}
fmt.Printf("Changing tags of %s from %s -> %s\n", name, custom.Tags, tags)
fmt.Printf("Changing tags of %s from %s -> %s\n", name, custom.Tags, targetTags)
} else {
fmt.Printf("Target(%s) not found in targets.json\n", name)
os.Exit(1)
Expand Down

0 comments on commit 8c95b53

Please sign in to comment.