Skip to content

Commit

Permalink
refactor: use util.Wrap to wrap JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Dec 20, 2023
1 parent 7240902 commit 52a9b08
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 51 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/certificate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var CreateCmd = base.CreateCmd{
if err != nil {
return nil, nil, err
}
return cert, util.WrapSchema("certificate", hcloud.SchemaFromCertificate(cert)), nil
return cert, util.Wrap("certificate", hcloud.SchemaFromCertificate(cert)), nil
},
}

Expand Down
7 changes: 2 additions & 5 deletions internal/cmd/firewall/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand Down Expand Up @@ -89,10 +90,6 @@ var CreateCmd = base.CreateCmd{

cmd.Printf("Firewall %d created\n", res.Firewall.ID)

return res.Firewall, struct {
Firewall schema.Firewall `json:"firewall"`
}{
Firewall: hcloud.SchemaFromFirewall(res.Firewall),
}, err
return res.Firewall, util.Wrap("firewall", hcloud.SchemaFromFirewall(res.Firewall)), err
},
}
8 changes: 2 additions & 6 deletions internal/cmd/floatingip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -107,11 +107,7 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

return res.FloatingIP, struct {
FloatingIP schema.FloatingIP `json:"floating_ip"`
}{
FloatingIP: hcloud.SchemaFromFloatingIP(res.FloatingIP),
}, nil
return res.FloatingIP, util.Wrap("floating_ip", hcloud.SchemaFromFloatingIP(res.FloatingIP)), nil
},

PrintResource: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/loadbalancer/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -96,11 +96,7 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

return loadBalancer, struct {
LoadBalancer schema.LoadBalancer `json:"load_balancer"`
}{
LoadBalancer: hcloud.SchemaFromLoadBalancer(loadBalancer),
}, nil
return loadBalancer, util.Wrap("load_balancer", hcloud.SchemaFromLoadBalancer(loadBalancer)), nil
},

PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) {
Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -66,10 +66,6 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

return network, struct {
Network schema.Network `json:"network"`
}{
Network: hcloud.SchemaFromNetwork(network),
}, nil
return network, util.Wrap("network", hcloud.SchemaFromNetwork(network)), nil
},
}
8 changes: 2 additions & 6 deletions internal/cmd/placementgroup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -51,10 +51,6 @@ var CreateCmd = base.CreateCmd{

cmd.Printf("Placement group %d created\n", res.PlacementGroup.ID)

return res.PlacementGroup, struct {
PlacementGroup schema.PlacementGroup `json:"placement_group"`
}{
PlacementGroup: hcloud.SchemaFromPlacementGroup(res.PlacementGroup),
}, nil
return res.PlacementGroup, util.Wrap("placement_group", hcloud.SchemaFromPlacementGroup(res.PlacementGroup)), nil
},
}
8 changes: 2 additions & 6 deletions internal/cmd/primaryip/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -82,11 +82,7 @@ var CreateCmd = base.CreateCmd{
}
}

return res.PrimaryIP, struct {
PrimaryIP schema.PrimaryIP `json:"primary_ip"`
}{
PrimaryIP: hcloud.SchemaFromPrimaryIP(res.PrimaryIP),
}, nil
return res.PrimaryIP, util.Wrap("primary_ip", hcloud.SchemaFromPrimaryIP(res.PrimaryIP)), nil
},
PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) {
primaryIP := resource.(*hcloud.PrimaryIP)
Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/sshkey/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -65,10 +65,6 @@ var CreateCmd = base.CreateCmd{

cmd.Printf("SSH key %d created\n", sshKey.ID)

return sshKey, struct {
SSHKey schema.SSHKey `json:"ssh_key"`
}{
SSHKey: hcloud.SchemaFromSSHKey(sshKey),
}, nil
return sshKey, util.Wrap("ssh_key", hcloud.SchemaFromSSHKey(sshKey)), nil
},
}
6 changes: 3 additions & 3 deletions internal/cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ func DescribeYAML(object interface{}) error {
return enc.Encode(object)
}

// WrapSchema return a map wrapping the schema (value) in a dict (`{ key: value }`).
// Wrap wraps the passed value in a map with the passed key.
//
// Used to reduce boilerplate.
func WrapSchema(key string, value any) map[string]any {
// This is useful when working with JSON objects.
func Wrap(key string, value any) map[string]any {
return map[string]any{key: value}
}

Expand Down
8 changes: 2 additions & 6 deletions internal/cmd/volume/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var CreateCmd = base.CreateCmd{
Expand Down Expand Up @@ -111,10 +111,6 @@ var CreateCmd = base.CreateCmd{
return nil, nil, err
}

return res.Volume, struct {
Volume schema.Volume `json:"volume"`
}{
Volume: hcloud.SchemaFromVolume(res.Volume),
}, nil
return res.Volume, util.Wrap("volume", hcloud.SchemaFromVolume(res.Volume)), nil
},
}

0 comments on commit 52a9b08

Please sign in to comment.