From 52a9b0895a5b6b65eeb0a972f40f33b804a37869 Mon Sep 17 00:00:00 2001 From: pauhull Date: Wed, 20 Dec 2023 13:42:51 +0100 Subject: [PATCH] refactor: use util.Wrap to wrap JSON --- internal/cmd/certificate/create.go | 2 +- internal/cmd/firewall/create.go | 7 ++----- internal/cmd/floatingip/create.go | 8 ++------ internal/cmd/loadbalancer/create.go | 8 ++------ internal/cmd/network/create.go | 8 ++------ internal/cmd/placementgroup/create.go | 8 ++------ internal/cmd/primaryip/create.go | 8 ++------ internal/cmd/sshkey/create.go | 8 ++------ internal/cmd/util/util.go | 6 +++--- internal/cmd/volume/create.go | 8 ++------ 10 files changed, 20 insertions(+), 51 deletions(-) diff --git a/internal/cmd/certificate/create.go b/internal/cmd/certificate/create.go index 10da3cee..d2531f39 100644 --- a/internal/cmd/certificate/create.go +++ b/internal/cmd/certificate/create.go @@ -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 }, } diff --git a/internal/cmd/firewall/create.go b/internal/cmd/firewall/create.go index a18de870..0eac98b4 100644 --- a/internal/cmd/firewall/create.go +++ b/internal/cmd/firewall/create.go @@ -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" @@ -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 }, } diff --git a/internal/cmd/floatingip/create.go b/internal/cmd/floatingip/create.go index f1ed5c97..d33802d3 100644 --- a/internal/cmd/floatingip/create.go +++ b/internal/cmd/floatingip/create.go @@ -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{ @@ -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) { diff --git a/internal/cmd/loadbalancer/create.go b/internal/cmd/loadbalancer/create.go index d52275bc..c7364695 100644 --- a/internal/cmd/loadbalancer/create.go +++ b/internal/cmd/loadbalancer/create.go @@ -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{ @@ -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) { diff --git a/internal/cmd/network/create.go b/internal/cmd/network/create.go index d91dcc6c..c22346f5 100644 --- a/internal/cmd/network/create.go +++ b/internal/cmd/network/create.go @@ -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{ @@ -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 }, } diff --git a/internal/cmd/placementgroup/create.go b/internal/cmd/placementgroup/create.go index 4b9bcdfa..707a47bf 100644 --- a/internal/cmd/placementgroup/create.go +++ b/internal/cmd/placementgroup/create.go @@ -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{ @@ -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 }, } diff --git a/internal/cmd/primaryip/create.go b/internal/cmd/primaryip/create.go index f99259b9..1599796c 100644 --- a/internal/cmd/primaryip/create.go +++ b/internal/cmd/primaryip/create.go @@ -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{ @@ -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) diff --git a/internal/cmd/sshkey/create.go b/internal/cmd/sshkey/create.go index fec2fb03..def45d30 100644 --- a/internal/cmd/sshkey/create.go +++ b/internal/cmd/sshkey/create.go @@ -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{ @@ -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 }, } diff --git a/internal/cmd/util/util.go b/internal/cmd/util/util.go index 1407feb2..08af39a4 100644 --- a/internal/cmd/util/util.go +++ b/internal/cmd/util/util.go @@ -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} } diff --git a/internal/cmd/volume/create.go b/internal/cmd/volume/create.go index 7508e1de..b65bff26 100644 --- a/internal/cmd/volume/create.go +++ b/internal/cmd/volume/create.go @@ -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{ @@ -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 }, }