Skip to content

Commit

Permalink
Handle string or integer output from TF
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiol committed Feb 13, 2024
1 parent 8456f75 commit cdb1862
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion cmd/loadbalancer/loadbalancer_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"slices"
"strconv"
"strings"

"github.com/ans-group/cli/internal/pkg/factory"
Expand All @@ -16,6 +17,32 @@ import (

const clusterIDsOutputKey string = "loadbalancer_cluster_ids"

type clusterIDList []int

func (c *clusterIDList) UnmarshalJSON(data []byte) error {
var v []any
if err := json.Unmarshal(data, &v); err != nil {
return err
}

for _, item := range v {
switch v := item.(type) {
case float64:
*c = append(*c, int(v))
case string:
if intValue, err := strconv.Atoi(v); err == nil {
*c = append(*c, intValue)
} else {
return err
}
default:
return fmt.Errorf("unsupported type: %T", item)
}
}

return nil
}

func loadbalancerTerraformCmd(f factory.ClientFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "terraform",
Expand Down Expand Up @@ -118,7 +145,7 @@ func getClusterIDs(binPath string) ([]int, error) {
clusterIDsOutputKey, err)
}

var clusterIDs []int
var clusterIDs clusterIDList
err = json.Unmarshal(output, &clusterIDs)
if err != nil {
return nil, fmt.Errorf("ans: deployment failed: failed to unmarshal Terraform output from key '%s': %s",
Expand Down

0 comments on commit cdb1862

Please sign in to comment.