Skip to content

Commit

Permalink
targets-tag: Given caller more flexibility
Browse files Browse the repository at this point in the history
Move the changes struct public so that the caller can have more control
over how the tags should be done.

Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac authored and detsch committed Sep 21, 2023
1 parent 9a93428 commit 67ffcf8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
19 changes: 7 additions & 12 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,21 +1110,16 @@ func (a *Api) TargetsPut(factory string, data []byte) (string, string, error) {
return parseJobServResponse(resp, err, "UpdateTargets")
}

func (a *Api) TargetUpdateTags(factory string, target_names []string, tag_names []string) (string, string, error) {
type EmptyTarget struct {
Custom TufCustom `json:"custom"`
}
tags := EmptyTarget{TufCustom{Tags: tag_names}}
type UpdateTarget struct {
Custom TufCustom `json:"custom"`
}
type UpdateTargets map[string]UpdateTarget

func (a *Api) TargetUpdateTags(factory string, updates UpdateTargets) (string, string, error) {
type Update struct {
Targets map[string]EmptyTarget `json:"targets"`
}
update := Update{map[string]EmptyTarget{}}
for idx := range target_names {
update.Targets[target_names[idx]] = tags
Targets UpdateTargets `json:"targets"`
}

data, err := json.Marshal(update)
data, err := json.Marshal(Update{updates})
if err != nil {
return "", "", err
}
Expand Down
21 changes: 15 additions & 6 deletions subcommands/targets/tag.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package targets

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
)

Expand Down Expand Up @@ -44,21 +46,23 @@ func doTag(cmd *cobra.Command, args []string) {
targets, err := api.TargetsList(factory)
subcommands.DieNotNil(err)

var target_names []string
updates := make(client.UpdateTargets)

if tagByVersion {
target_names = make([]string, 0, 10)
for name, target := range targets {
custom, err := api.TargetCustom(target)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
} else {
if intersectionInSlices([]string{custom.Version}, args) {
target_names = append(target_names, name)
updates[name] = client.UpdateTarget{
Custom: client.TufCustom{Tags: tags},
}
fmt.Printf("Changing tags of %s from %s -> %s\n", name, custom.Tags, tags)
}
}
}
if len(target_names) == 0 {
if len(updates) == 0 {
fmt.Println("ERROR: no targets found matching the given versions")
os.Exit(1)
}
Expand All @@ -67,20 +71,25 @@ func doTag(cmd *cobra.Command, args []string) {
if target, ok := targets[name]; ok {
custom, err := api.TargetCustom(target)
subcommands.DieNotNil(err)
updates[name] = client.UpdateTarget{
Custom: client.TufCustom{Tags: tags},
}
fmt.Printf("Changing tags of %s from %s -> %s\n", name, custom.Tags, tags)
} else {
fmt.Printf("Target(%s) not found in targets.json\n", name)
os.Exit(1)
}
}
target_names = args
}

if dryRun {
data, err := json.MarshalIndent(updates, " ", " ")
subcommands.DieNotNil(err)
fmt.Println(string(data))
return
}

jobServUrl, webUrl, err := api.TargetUpdateTags(factory, target_names, tags)
jobServUrl, webUrl, err := api.TargetUpdateTags(factory, updates)
subcommands.DieNotNil(err)
fmt.Printf("CI URL: %s\n", webUrl)
if !tagNoTail {
Expand Down

0 comments on commit 67ffcf8

Please sign in to comment.