From d756bea528c6d0ef107c1d160534dd31e9410b85 Mon Sep 17 00:00:00 2001 From: pauhull Date: Wed, 3 Jan 2024 13:12:54 +0100 Subject: [PATCH 1/5] refactor: make state mockable --- cmd/hcloud/main.go | 22 +--- internal/cli/root.go | 43 ++++---- internal/cmd/all/all.go | 5 +- internal/cmd/all/list.go | 7 +- internal/cmd/all/list_test.go | 13 +-- internal/cmd/base/cmd.go | 16 +-- internal/cmd/base/create.go | 19 ++-- internal/cmd/base/delete.go | 21 ++-- internal/cmd/base/describe.go | 21 ++-- internal/cmd/base/labels.go | 41 +++---- internal/cmd/base/list.go | 19 ++-- internal/cmd/base/set_rdns.go | 21 ++-- internal/cmd/base/update.go | 21 ++-- internal/cmd/certificate/certificate.go | 17 ++- internal/cmd/certificate/create.go | 17 ++- internal/cmd/certificate/create_test.go | 25 +---- internal/cmd/certificate/delete.go | 10 +- internal/cmd/certificate/delete_test.go | 7 +- internal/cmd/certificate/describe.go | 11 +- internal/cmd/certificate/describe_test.go | 6 +- internal/cmd/certificate/labels.go | 10 +- internal/cmd/certificate/labels_test.go | 11 +- internal/cmd/certificate/list.go | 6 +- internal/cmd/certificate/list_test.go | 3 +- internal/cmd/certificate/update.go | 11 +- internal/cmd/certificate/update_test.go | 6 +- internal/cmd/completion/completion.go | 2 +- internal/cmd/context/active.go | 10 +- internal/cmd/context/context.go | 12 +- internal/cmd/context/create.go | 15 +-- internal/cmd/context/delete.go | 19 ++-- internal/cmd/context/list.go | 11 +- internal/cmd/context/use.go | 15 +-- internal/cmd/datacenter/datacenter.go | 7 +- internal/cmd/datacenter/describe.go | 13 +-- internal/cmd/datacenter/describe_test.go | 6 +- internal/cmd/datacenter/list.go | 7 +- internal/cmd/datacenter/list_test.go | 3 +- internal/cmd/firewall/add_rule.go | 9 +- internal/cmd/firewall/add_rule_test.go | 7 +- internal/cmd/firewall/apply_to_resource.go | 11 +- .../cmd/firewall/apply_to_resource_test.go | 13 +-- internal/cmd/firewall/create.go | 7 +- internal/cmd/firewall/create_test.go | 13 +-- internal/cmd/firewall/delete.go | 10 +- internal/cmd/firewall/delete_rule.go | 9 +- internal/cmd/firewall/delete_rule_test.go | 7 +- internal/cmd/firewall/delete_test.go | 7 +- internal/cmd/firewall/describe.go | 10 +- internal/cmd/firewall/describe_test.go | 6 +- internal/cmd/firewall/firewall.go | 27 +++-- internal/cmd/firewall/labels.go | 10 +- internal/cmd/firewall/labels_test.go | 11 +- internal/cmd/firewall/list.go | 6 +- internal/cmd/firewall/list_test.go | 3 +- internal/cmd/firewall/remove_from_resource.go | 11 +- .../cmd/firewall/remove_from_resource_test.go | 13 +-- internal/cmd/firewall/replace_rules.go | 9 +- internal/cmd/firewall/replace_rules_test.go | 7 +- internal/cmd/firewall/update.go | 11 +- internal/cmd/firewall/update_test.go | 6 +- internal/cmd/floatingip/assign.go | 11 +- internal/cmd/floatingip/assign_test.go | 7 +- internal/cmd/floatingip/create.go | 13 +-- internal/cmd/floatingip/create_test.go | 19 +--- internal/cmd/floatingip/delete.go | 10 +- internal/cmd/floatingip/delete_test.go | 7 +- internal/cmd/floatingip/describe.go | 11 +- internal/cmd/floatingip/describe_test.go | 6 +- internal/cmd/floatingip/disable_protection.go | 7 +- .../cmd/floatingip/disable_protection_test.go | 7 +- internal/cmd/floatingip/enable_protection.go | 13 +-- .../cmd/floatingip/enable_protection_test.go | 7 +- internal/cmd/floatingip/floatingip.go | 27 +++-- internal/cmd/floatingip/labels.go | 10 +- internal/cmd/floatingip/labels_test.go | 11 +- internal/cmd/floatingip/list.go | 6 +- internal/cmd/floatingip/list_test.go | 3 +- internal/cmd/floatingip/set_rdns.go | 6 +- internal/cmd/floatingip/set_rdns_test.go | 7 +- internal/cmd/floatingip/unassign.go | 9 +- internal/cmd/floatingip/unassign_test.go | 7 +- internal/cmd/floatingip/update.go | 11 +- internal/cmd/floatingip/update_test.go | 11 +- internal/cmd/image/delete.go | 10 +- internal/cmd/image/delete_test.go | 7 +- internal/cmd/image/describe.go | 8 +- internal/cmd/image/describe_test.go | 6 +- internal/cmd/image/disable_protection.go | 5 +- internal/cmd/image/disable_protection_test.go | 7 +- internal/cmd/image/enable_protection.go | 11 +- internal/cmd/image/enable_protection_test.go | 7 +- internal/cmd/image/image.go | 19 ++-- internal/cmd/image/labels.go | 10 +- internal/cmd/image/labels_test.go | 11 +- internal/cmd/image/list.go | 6 +- internal/cmd/image/list_test.go | 3 +- internal/cmd/image/update.go | 11 +- internal/cmd/image/update_test.go | 11 +- internal/cmd/iso/describe.go | 9 +- internal/cmd/iso/describe_test.go | 6 +- internal/cmd/iso/iso.go | 7 +- internal/cmd/iso/list.go | 6 +- internal/cmd/iso/list_test.go | 3 +- internal/cmd/loadbalancer/add_service.go | 9 +- internal/cmd/loadbalancer/add_service_test.go | 7 +- internal/cmd/loadbalancer/add_target.go | 15 ++- internal/cmd/loadbalancer/add_target_test.go | 19 +--- .../cmd/loadbalancer/attach_to_network.go | 11 +- .../loadbalancer/attach_to_network_test.go | 7 +- internal/cmd/loadbalancer/change_algorithm.go | 9 +- .../cmd/loadbalancer/change_algorithm_test.go | 7 +- internal/cmd/loadbalancer/change_type.go | 11 +- internal/cmd/loadbalancer/change_type_test.go | 7 +- internal/cmd/loadbalancer/create.go | 14 +-- internal/cmd/loadbalancer/create_test.go | 19 +--- internal/cmd/loadbalancer/delete.go | 10 +- internal/cmd/loadbalancer/delete_service.go | 7 +- .../cmd/loadbalancer/delete_service_test.go | 7 +- internal/cmd/loadbalancer/delete_test.go | 7 +- internal/cmd/loadbalancer/describe.go | 13 +-- internal/cmd/loadbalancer/describe_test.go | 6 +- .../cmd/loadbalancer/detach_from_network.go | 11 +- .../loadbalancer/detach_from_network_test.go | 7 +- .../cmd/loadbalancer/disable_protection.go | 7 +- .../loadbalancer/disable_protection_test.go | 7 +- .../loadbalancer/disable_public_interface.go | 9 +- .../disable_public_interface_test.go | 7 +- .../cmd/loadbalancer/enable_protection.go | 13 +-- .../loadbalancer/enable_protection_test.go | 7 +- .../loadbalancer/enable_public_interface.go | 9 +- .../enable_public_interface_test.go | 7 +- internal/cmd/loadbalancer/labels.go | 10 +- internal/cmd/loadbalancer/labels_test.go | 11 +- internal/cmd/loadbalancer/list.go | 6 +- internal/cmd/loadbalancer/load_balancer.go | 47 ++++---- internal/cmd/loadbalancer/metrics.go | 7 +- internal/cmd/loadbalancer/metrics_test.go | 7 +- internal/cmd/loadbalancer/remove_target.go | 15 ++- .../cmd/loadbalancer/remove_target_test.go | 19 +--- internal/cmd/loadbalancer/set_rdns.go | 6 +- internal/cmd/loadbalancer/set_rdns_test.go | 7 +- internal/cmd/loadbalancer/update.go | 11 +- internal/cmd/loadbalancer/update_service.go | 9 +- .../cmd/loadbalancer/update_service_test.go | 7 +- internal/cmd/loadbalancer/update_test.go | 6 +- internal/cmd/loadbalancertype/describe.go | 9 +- .../cmd/loadbalancertype/describe_test.go | 6 +- internal/cmd/loadbalancertype/list.go | 7 +- internal/cmd/loadbalancertype/list_test.go | 3 +- .../loadbalancertype/load_balancer_type.go | 7 +- internal/cmd/location/describe.go | 9 +- internal/cmd/location/describe_test.go | 6 +- internal/cmd/location/list.go | 7 +- internal/cmd/location/list_test.go | 3 +- internal/cmd/location/location.go | 7 +- internal/cmd/network/add_route.go | 9 +- internal/cmd/network/add_subnet.go | 9 +- internal/cmd/network/change_ip_range.go | 9 +- internal/cmd/network/create.go | 7 +- internal/cmd/network/create_test.go | 19 +--- internal/cmd/network/delete.go | 10 +- internal/cmd/network/delete_test.go | 7 +- internal/cmd/network/describe.go | 9 +- internal/cmd/network/describe_test.go | 6 +- internal/cmd/network/disable_protection.go | 7 +- internal/cmd/network/enable_protection.go | 13 +-- .../cmd/network/expose-routes-to-vswitch.go | 7 +- internal/cmd/network/labels.go | 10 +- internal/cmd/network/labels_test.go | 11 +- internal/cmd/network/list.go | 6 +- internal/cmd/network/list_test.go | 3 +- internal/cmd/network/network.go | 33 +++--- internal/cmd/network/remove_route.go | 9 +- internal/cmd/network/remove_subnet.go | 9 +- internal/cmd/network/update.go | 11 +- internal/cmd/network/update_test.go | 6 +- internal/cmd/placementgroup/create.go | 8 +- internal/cmd/placementgroup/create_test.go | 13 +-- internal/cmd/placementgroup/delete.go | 10 +- internal/cmd/placementgroup/delete_test.go | 7 +- internal/cmd/placementgroup/describe.go | 11 +- internal/cmd/placementgroup/describe_test.go | 6 +- internal/cmd/placementgroup/labels.go | 10 +- internal/cmd/placementgroup/labels_test.go | 11 +- internal/cmd/placementgroup/list.go | 6 +- internal/cmd/placementgroup/list_test.go | 6 +- internal/cmd/placementgroup/placementgroup.go | 17 ++- internal/cmd/placementgroup/update.go | 11 +- internal/cmd/placementgroup/update_test.go | 6 +- internal/cmd/primaryip/assign.go | 11 +- internal/cmd/primaryip/assign_test.go | 3 +- internal/cmd/primaryip/changedns.go | 9 +- internal/cmd/primaryip/changedns_test.go | 3 +- internal/cmd/primaryip/create.go | 12 +- internal/cmd/primaryip/create_test.go | 9 +- internal/cmd/primaryip/delete.go | 10 +- internal/cmd/primaryip/delete_test.go | 3 +- internal/cmd/primaryip/describe.go | 9 +- internal/cmd/primaryip/describe_test.go | 3 +- internal/cmd/primaryip/disable_protection.go | 7 +- .../cmd/primaryip/disable_protection_test.go | 3 +- internal/cmd/primaryip/enable_protection.go | 13 +-- .../cmd/primaryip/enable_protection_test.go | 3 +- internal/cmd/primaryip/labels.go | 10 +- internal/cmd/primaryip/labels_test.go | 11 +- internal/cmd/primaryip/list.go | 6 +- internal/cmd/primaryip/list_test.go | 3 +- internal/cmd/primaryip/primaryip.go | 27 +++-- internal/cmd/primaryip/unassign.go | 9 +- internal/cmd/primaryip/unassign_test.go | 3 +- internal/cmd/primaryip/update.go | 11 +- internal/cmd/primaryip/update_test.go | 11 +- internal/cmd/server/add_to_placement_group.go | 11 +- .../cmd/server/add_to_placement_group_test.go | 7 +- internal/cmd/server/attach_iso.go | 11 +- internal/cmd/server/attach_to_network.go | 11 +- internal/cmd/server/change_alias_ips.go | 11 +- internal/cmd/server/change_type.go | 11 +- internal/cmd/server/create.go | 49 ++++----- internal/cmd/server/create_image.go | 9 +- internal/cmd/server/create_test.go | 21 +--- internal/cmd/server/delete.go | 12 +- internal/cmd/server/delete_test.go | 7 +- internal/cmd/server/describe.go | 14 +-- internal/cmd/server/describe_test.go | 6 +- internal/cmd/server/detach_from_network.go | 11 +- internal/cmd/server/detach_iso.go | 9 +- internal/cmd/server/disable_backup.go | 9 +- internal/cmd/server/disable_protection.go | 7 +- internal/cmd/server/disable_rescue.go | 9 +- internal/cmd/server/enable_backup.go | 9 +- internal/cmd/server/enable_protection.go | 13 +-- internal/cmd/server/enable_rescue.go | 11 +- internal/cmd/server/ip.go | 5 +- internal/cmd/server/labels.go | 10 +- internal/cmd/server/labels_test.go | 11 +- internal/cmd/server/list.go | 6 +- internal/cmd/server/list_test.go | 3 +- internal/cmd/server/metrics.go | 7 +- internal/cmd/server/poweroff.go | 9 +- internal/cmd/server/poweron.go | 9 +- internal/cmd/server/reboot.go | 9 +- internal/cmd/server/rebuild.go | 11 +- .../cmd/server/remove_from_placement_group.go | 9 +- .../remove_from_placement_group_test.go | 7 +- internal/cmd/server/request_console.go | 9 +- internal/cmd/server/reset.go | 9 +- internal/cmd/server/reset_password.go | 9 +- internal/cmd/server/server.go | 71 ++++++------ internal/cmd/server/set_rdns.go | 6 +- internal/cmd/server/shutdown.go | 11 +- internal/cmd/server/shutdown_test.go | 13 +-- internal/cmd/server/ssh.go | 5 +- internal/cmd/server/update.go | 11 +- internal/cmd/server/update_test.go | 6 +- internal/cmd/servertype/describe.go | 9 +- internal/cmd/servertype/describe_test.go | 6 +- internal/cmd/servertype/list.go | 6 +- internal/cmd/servertype/list_test.go | 3 +- internal/cmd/servertype/server_type.go | 7 +- internal/cmd/sshkey/create.go | 5 +- internal/cmd/sshkey/create_test.go | 13 +-- internal/cmd/sshkey/delete.go | 10 +- internal/cmd/sshkey/delete_test.go | 7 +- internal/cmd/sshkey/describe.go | 8 +- internal/cmd/sshkey/describe_test.go | 6 +- internal/cmd/sshkey/labels.go | 10 +- internal/cmd/sshkey/labels_test.go | 11 +- internal/cmd/sshkey/list.go | 6 +- internal/cmd/sshkey/list_test.go | 3 +- internal/cmd/sshkey/sshkey.go | 17 ++- internal/cmd/sshkey/update.go | 11 +- internal/cmd/sshkey/update_test.go | 6 +- internal/cmd/version/version.go | 6 +- internal/cmd/volume/attach.go | 11 +- internal/cmd/volume/create.go | 13 +-- internal/cmd/volume/create_test.go | 19 +--- internal/cmd/volume/delete.go | 10 +- internal/cmd/volume/delete_test.go | 7 +- internal/cmd/volume/describe.go | 11 +- internal/cmd/volume/describe_test.go | 6 +- internal/cmd/volume/detach.go | 9 +- internal/cmd/volume/disable_protection.go | 7 +- internal/cmd/volume/enable_protection.go | 13 +-- internal/cmd/volume/labels.go | 10 +- internal/cmd/volume/labels_test.go | 11 +- internal/cmd/volume/list.go | 6 +- internal/cmd/volume/list_test.go | 3 +- internal/cmd/volume/resize.go | 9 +- internal/cmd/volume/update.go | 11 +- internal/cmd/volume/update_test.go | 6 +- internal/cmd/volume/volume.go | 27 +++-- internal/state/helpers.go | 46 ++------ internal/state/state.go | 104 +++++++++++++----- internal/testutil/fixture.go | 29 +++++ 296 files changed, 1237 insertions(+), 1948 deletions(-) diff --git a/cmd/hcloud/main.go b/cmd/hcloud/main.go index 2ab664b9..17afdf1c 100644 --- a/cmd/hcloud/main.go +++ b/cmd/hcloud/main.go @@ -5,7 +5,6 @@ import ( "os" "github.com/hetznercloud/cli/internal/cli" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) @@ -16,25 +15,12 @@ func init() { } func main() { - cliState := state.New() - - if cliState.ConfigPath != "" { - _, err := os.Stat(cliState.ConfigPath) - switch { - case err == nil: - if err := cliState.ReadConfig(); err != nil { - log.Fatalf("unable to read config file %q: %s\n", cliState.ConfigPath, err) - } - case os.IsNotExist(err): - break - default: - log.Fatalf("unable to read config file %q: %s\n", cliState.ConfigPath, err) - } + s, err := state.New() + if err != nil { + log.Fatalln(err) } - cliState.ReadEnv() - apiClient := hcapi2.NewClient(cliState.Client()) - rootCommand := cli.NewRootCommand(cliState, apiClient) + rootCommand := cli.NewRootCommand(s) if err := rootCommand.Execute(); err != nil { log.Fatalln(err) } diff --git a/internal/cli/root.go b/internal/cli/root.go index b3ca29c3..d0916bef 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -26,11 +26,10 @@ import ( "github.com/hetznercloud/cli/internal/cmd/sshkey" "github.com/hetznercloud/cli/internal/cmd/version" "github.com/hetznercloud/cli/internal/cmd/volume" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command { +func NewRootCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "hcloud", Short: "Hetzner Cloud CLI", @@ -41,26 +40,26 @@ func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - all.NewCommand(state, client), - floatingip.NewCommand(state, client), - image.NewCommand(state, client), - server.NewCommand(state, client), - sshkey.NewCommand(state, client), - version.NewCommand(state), - completion.NewCommand(state), - servertype.NewCommand(state, client), - context.NewCommand(state), - datacenter.NewCommand(state, client), - location.NewCommand(state, client), - iso.NewCommand(state, client), - volume.NewCommand(state, client), - network.NewCommand(state, client), - loadbalancer.NewCommand(state, client), - loadbalancertype.NewCommand(state, client), - certificate.NewCommand(state, client), - firewall.NewCommand(state, client), - placementgroup.NewCommand(state, client), - primaryip.NewCommand(state, client), + all.NewCommand(s), + floatingip.NewCommand(s), + image.NewCommand(s), + server.NewCommand(s), + sshkey.NewCommand(s), + version.NewCommand(s), + completion.NewCommand(s), + servertype.NewCommand(s), + context.NewCommand(s), + datacenter.NewCommand(s), + location.NewCommand(s), + iso.NewCommand(s), + volume.NewCommand(s), + network.NewCommand(s), + loadbalancer.NewCommand(s), + loadbalancertype.NewCommand(s), + certificate.NewCommand(s), + firewall.NewCommand(s), + placementgroup.NewCommand(s), + primaryip.NewCommand(s), ) cmd.PersistentFlags().Duration("poll-interval", 500*time.Millisecond, "Interval at which to poll information, for example action progress") cmd.SetOut(os.Stdout) diff --git a/internal/cmd/all/all.go b/internal/cmd/all/all.go index 0dba942f..83dc1743 100644 --- a/internal/cmd/all/all.go +++ b/internal/cmd/all/all.go @@ -3,11 +3,10 @@ package all import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "all", Short: "Commands that apply to all resources", @@ -16,7 +15,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - listCmd.CobraCommand(cli.Context, client, cli, cli), + listCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/all/list.go b/internal/cmd/all/list.go index 86dbf97c..cb3919fc 100644 --- a/internal/cmd/all/list.go +++ b/internal/cmd/all/list.go @@ -1,7 +1,6 @@ package all import ( - "context" "strings" "github.com/spf13/cobra" @@ -67,7 +66,7 @@ Listed resources are: return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { paid, _ := cmd.Flags().GetBool("paid") labelSelector, _ := cmd.Flags().GetString("selector") @@ -133,7 +132,7 @@ Listed resources are: // We pass an empty slice because we defined the flags earlier. _ = flagSet.Parse([]string{}) - result, err := lc.Fetch(ctx, client, flagSet, listOpts, []string{}) + result, err := lc.Fetch(s, flagSet, listOpts, []string{}) ch <- response{result, err} }() } @@ -162,7 +161,7 @@ Listed resources are: for i, lc := range cmds { cols := lc.DefaultColumns - table := lc.OutputTable(client) + table := lc.OutputTable(s) table.WriteHeader(cols) if len(resources[i]) == 0 { diff --git a/internal/cmd/all/list_test.go b/internal/cmd/all/list_test.go index 873e8d16..a28453b2 100644 --- a/internal/cmd/all/list_test.go +++ b/internal/cmd/all/list_test.go @@ -1,7 +1,6 @@ package all import ( - "context" _ "embed" "net" "testing" @@ -23,11 +22,7 @@ func TestListAll(t *testing.T) { time.Local = time.UTC - cmd := listCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := listCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerClient.EXPECT(). @@ -251,11 +246,7 @@ func TestListAllPaidJSON(t *testing.T) { time.Local = time.UTC - cmd := listCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := listCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerClient.EXPECT(). diff --git a/internal/cmd/base/cmd.go b/internal/cmd/base/cmd.go index 255e8bab..434976ee 100644 --- a/internal/cmd/base/cmd.go +++ b/internal/cmd/base/cmd.go @@ -1,8 +1,6 @@ package base import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/util" @@ -13,14 +11,12 @@ import ( // Cmd allows defining commands for generic resource-based commands type Cmd struct { BaseCobraCommand func(hcapi2.Client) *cobra.Command - Run func(context.Context, hcapi2.Client, state.ActionWaiter, *cobra.Command, []string) error + Run func(state.State, *cobra.Command, []string) error } // CobraCommand creates a command that can be registered with cobra. -func (gc *Cmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, -) *cobra.Command { - cmd := gc.BaseCobraCommand(client) +func (gc *Cmd) CobraCommand(s state.State) *cobra.Command { + cmd := gc.BaseCobraCommand(s) if cmd.Args == nil { cmd.Args = cobra.NoArgs @@ -30,13 +26,13 @@ func (gc *Cmd) CobraCommand( cmd.DisableFlagsInUseLine = true if cmd.PreRunE != nil { - cmd.PreRunE = util.ChainRunE(cmd.PreRunE, tokenEnsurer.EnsureToken) + cmd.PreRunE = util.ChainRunE(cmd.PreRunE, s.EnsureToken) } else { - cmd.PreRunE = tokenEnsurer.EnsureToken + cmd.PreRunE = s.EnsureToken } cmd.RunE = func(cmd *cobra.Command, args []string) error { - return gc.Run(ctx, client, actionWaiter, cmd, args) + return gc.Run(s, cmd, args) } return cmd diff --git a/internal/cmd/base/create.go b/internal/cmd/base/create.go index cbd75019..a863a761 100644 --- a/internal/cmd/base/create.go +++ b/internal/cmd/base/create.go @@ -1,7 +1,6 @@ package base import ( - "context" "os" "github.com/spf13/cobra" @@ -17,15 +16,13 @@ type CreateCmd struct { BaseCobraCommand func(hcapi2.Client) *cobra.Command // Run is the function that will be called when the command is executed. // It should return the created resource, the schema of the resource and an error. - Run func(context.Context, hcapi2.Client, state.ActionWaiter, *cobra.Command, []string) (any, any, error) - PrintResource func(context.Context, hcapi2.Client, *cobra.Command, any) + Run func(state.State, *cobra.Command, []string) (any, any, error) + PrintResource func(state.State, *cobra.Command, any) } // CobraCommand creates a command that can be registered with cobra. -func (cc *CreateCmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, -) *cobra.Command { - cmd := cc.BaseCobraCommand(client) +func (cc *CreateCmd) CobraCommand(s state.State) *cobra.Command { + cmd := cc.BaseCobraCommand(s) output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML()) @@ -37,9 +34,9 @@ func (cc *CreateCmd) CobraCommand( cmd.DisableFlagsInUseLine = true if cmd.PreRunE != nil { - cmd.PreRunE = util.ChainRunE(cmd.PreRunE, tokenEnsurer.EnsureToken) + cmd.PreRunE = util.ChainRunE(cmd.PreRunE, s.EnsureToken) } else { - cmd.PreRunE = tokenEnsurer.EnsureToken + cmd.PreRunE = s.EnsureToken } cmd.RunE = func(cmd *cobra.Command, args []string) error { @@ -52,7 +49,7 @@ func (cc *CreateCmd) CobraCommand( cmd.SetOut(os.Stdout) } - resource, schema, err := cc.Run(ctx, client, actionWaiter, cmd, args) + resource, schema, err := cc.Run(s, cmd, args) if err != nil { return err } @@ -64,7 +61,7 @@ func (cc *CreateCmd) CobraCommand( return util.DescribeYAML(schema) } } else if cc.PrintResource != nil && resource != nil { - cc.PrintResource(ctx, client, cmd, resource) + cc.PrintResource(s, cmd, resource) } return nil } diff --git a/internal/cmd/base/delete.go b/internal/cmd/base/delete.go index d2ef767c..bf4ee117 100644 --- a/internal/cmd/base/delete.go +++ b/internal/cmd/base/delete.go @@ -1,7 +1,6 @@ package base import ( - "context" "fmt" "reflect" "strings" @@ -21,24 +20,22 @@ type DeleteCmd struct { ShortDescription string NameSuggestions func(client hcapi2.Client) func() []string AdditionalFlags func(*cobra.Command) - Fetch func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) - Delete func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, resource interface{}) error + Fetch func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) + Delete func(s state.State, cmd *cobra.Command, resource interface{}) error } // CobraCommand creates a command that can be registered with cobra. -func (dc *DeleteCmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, -) *cobra.Command { +func (dc *DeleteCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("delete [FLAGS] %s", strings.ToUpper(dc.ResourceNameSingular)), Short: dc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(client))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s))), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(tokenEnsurer.EnsureToken), + PreRunE: util.ChainRunE(s.EnsureToken), RunE: func(cmd *cobra.Command, args []string) error { - return dc.Run(ctx, client, actionWaiter, cmd, args) + return dc.Run(s, cmd, args) }, } if dc.AdditionalFlags != nil { @@ -48,10 +45,10 @@ func (dc *DeleteCmd) CobraCommand( } // Run executes a describe command. -func (dc *DeleteCmd) Run(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { +func (dc *DeleteCmd) Run(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - resource, _, err := dc.Fetch(ctx, client, cmd, idOrName) + resource, _, err := dc.Fetch(s, cmd, idOrName) if err != nil { return err } @@ -62,7 +59,7 @@ func (dc *DeleteCmd) Run(ctx context.Context, client hcapi2.Client, actionWaiter return fmt.Errorf("%s not found: %s", dc.ResourceNameSingular, idOrName) } - if err := dc.Delete(ctx, client, actionWaiter, cmd, resource); err != nil { + if err := dc.Delete(s, cmd, resource); err != nil { return fmt.Errorf("deleting %s %s failed: %s", dc.ResourceNameSingular, idOrName, err) } cmd.Printf("%s %v deleted\n", dc.ResourceNameSingular, idOrName) diff --git a/internal/cmd/base/describe.go b/internal/cmd/base/describe.go index d15b757c..fe6cd527 100644 --- a/internal/cmd/base/describe.go +++ b/internal/cmd/base/describe.go @@ -1,7 +1,6 @@ package base import ( - "context" "fmt" "reflect" "strings" @@ -26,24 +25,22 @@ type DescribeCmd struct { AdditionalFlags func(*cobra.Command) // Fetch is called to fetch the resource to describe. // The first returned interface is the resource itself as a hcloud struct, the second is the schema for the resource. - Fetch func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) - PrintText func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error + Fetch func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) + PrintText func(s state.State, cmd *cobra.Command, resource interface{}) error } // CobraCommand creates a command that can be registered with cobra. -func (dc *DescribeCmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, -) *cobra.Command { +func (dc *DescribeCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("describe [FLAGS] %s", strings.ToUpper(dc.ResourceNameSingular)), Short: dc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(client))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s))), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(tokenEnsurer.EnsureToken), + PreRunE: util.ChainRunE(s.EnsureToken), RunE: func(cmd *cobra.Command, args []string) error { - return dc.Run(ctx, client, cmd, args) + return dc.Run(s, cmd, args) }, } output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML(), output.OptionFormat()) @@ -54,11 +51,11 @@ func (dc *DescribeCmd) CobraCommand( } // Run executes a describe command. -func (dc *DescribeCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, args []string) error { +func (dc *DescribeCmd) Run(s state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] - resource, schema, err := dc.Fetch(ctx, client, cmd, idOrName) + resource, schema, err := dc.Fetch(s, cmd, idOrName) if err != nil { return err } @@ -77,6 +74,6 @@ func (dc *DescribeCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra case outputFlags.IsSet("format"): return util.DescribeFormat(resource, outputFlags["format"][0]) default: - return dc.PrintText(ctx, client, cmd, resource) + return dc.PrintText(s, cmd, resource) } } diff --git a/internal/cmd/base/labels.go b/internal/cmd/base/labels.go index dcd95c20..88e31497 100644 --- a/internal/cmd/base/labels.go +++ b/internal/cmd/base/labels.go @@ -1,7 +1,6 @@ package base import ( - "context" "errors" "fmt" "strings" @@ -21,24 +20,22 @@ type LabelCmds struct { ShortDescriptionRemove string NameSuggestions func(client hcapi2.Client) func() []string LabelKeySuggestions func(client hcapi2.Client) func(idOrName string) []string - FetchLabels func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) - SetLabels func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error + FetchLabels func(s state.State, idOrName string) (map[string]string, int64, error) + SetLabels func(s state.State, id int64, labels map[string]string) error } // AddCobraCommand creates a command that can be registered with cobra. -func (lc *LabelCmds) AddCobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, -) *cobra.Command { +func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("add-label [FLAGS] %s LABEL", strings.ToUpper(lc.ResourceNameSingular)), Short: lc.ShortDescriptionAdd, Args: cobra.ExactArgs(2), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(lc.NameSuggestions(client))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(lc.NameSuggestions(s))), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateAddLabel, tokenEnsurer.EnsureToken), + PreRunE: util.ChainRunE(validateAddLabel, s.EnsureToken), RunE: func(cmd *cobra.Command, args []string) error { - return lc.RunAdd(ctx, client, cmd, args) + return lc.RunAdd(s, cmd, args) }, } cmd.Flags().BoolP("overwrite", "o", false, "Overwrite label if it exists already") @@ -46,11 +43,11 @@ func (lc *LabelCmds) AddCobraCommand( } // RunAdd executes an add label command -func (lc *LabelCmds) RunAdd(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, args []string) error { +func (lc *LabelCmds) RunAdd(s state.State, cmd *cobra.Command, args []string) error { overwrite, _ := cmd.Flags().GetBool("overwrite") idOrName := args[0] - labels, id, err := lc.FetchLabels(ctx, client, idOrName) + labels, id, err := lc.FetchLabels(s, idOrName) if err != nil { return err } @@ -67,7 +64,7 @@ func (lc *LabelCmds) RunAdd(ctx context.Context, client hcapi2.Client, cmd *cobr labels[key] = val - if err := lc.SetLabels(ctx, client, id, labels); err != nil { + if err := lc.SetLabels(s, id, labels); err != nil { return err } @@ -75,7 +72,7 @@ func (lc *LabelCmds) RunAdd(ctx context.Context, client hcapi2.Client, cmd *cobr return nil } -func validateAddLabel(cmd *cobra.Command, args []string) error { +func validateAddLabel(_ *cobra.Command, args []string) error { label := util.SplitLabel(args[1]) if len(label) != 2 { return fmt.Errorf("invalid label: %s", args[1]) @@ -85,27 +82,25 @@ func validateAddLabel(cmd *cobra.Command, args []string) error { } // RemoveCobraCommand creates a command that can be registered with cobra. -func (lc *LabelCmds) RemoveCobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, -) *cobra.Command { +func (lc *LabelCmds) RemoveCobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("remove-label [FLAGS] %s LABEL", strings.ToUpper(lc.ResourceNameSingular)), Short: lc.ShortDescriptionRemove, Args: cobra.RangeArgs(1, 2), ValidArgsFunction: cmpl.SuggestArgs( - cmpl.SuggestCandidatesF(lc.NameSuggestions(client)), + cmpl.SuggestCandidatesF(lc.NameSuggestions(s)), cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string { if len(args) != 1 { return nil } - return lc.LabelKeySuggestions(client)(args[0]) + return lc.LabelKeySuggestions(s)(args[0]) }), ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(validateRemoveLabel, tokenEnsurer.EnsureToken), + PreRunE: util.ChainRunE(validateRemoveLabel, s.EnsureToken), RunE: func(cmd *cobra.Command, args []string) error { - return lc.RunRemove(ctx, client, cmd, args) + return lc.RunRemove(s, cmd, args) }, } cmd.Flags().BoolP("all", "a", false, "Remove all labels") @@ -113,11 +108,11 @@ func (lc *LabelCmds) RemoveCobraCommand( } // RunRemove executes a remove label command -func (lc *LabelCmds) RunRemove(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, args []string) error { +func (lc *LabelCmds) RunRemove(s state.State, cmd *cobra.Command, args []string) error { all, _ := cmd.Flags().GetBool("all") idOrName := args[0] - labels, id, err := lc.FetchLabels(ctx, client, idOrName) + labels, id, err := lc.FetchLabels(s, idOrName) if err != nil { return err } @@ -132,7 +127,7 @@ func (lc *LabelCmds) RunRemove(ctx context.Context, client hcapi2.Client, cmd *c delete(labels, key) } - if err := lc.SetLabels(ctx, client, id, labels); err != nil { + if err := lc.SetLabels(s, id, labels); err != nil { return err } diff --git a/internal/cmd/base/list.go b/internal/cmd/base/list.go index 52a4b4c8..2f2d2230 100644 --- a/internal/cmd/base/list.go +++ b/internal/cmd/base/list.go @@ -1,7 +1,6 @@ package base import ( - "context" "fmt" "github.com/spf13/cobra" @@ -19,17 +18,15 @@ type ListCmd struct { ResourceNamePlural string // e.g. "servers" JSONKeyGetByName string // e.g. "servers" DefaultColumns []string - Fetch func(context.Context, hcapi2.Client, *pflag.FlagSet, hcloud.ListOpts, []string) ([]interface{}, error) + Fetch func(state.State, *pflag.FlagSet, hcloud.ListOpts, []string) ([]interface{}, error) AdditionalFlags func(*cobra.Command) OutputTable func(client hcapi2.Client) *output.Table Schema func([]interface{}) interface{} } // CobraCommand creates a command that can be registered with cobra. -func (lc *ListCmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, -) *cobra.Command { - outputColumns := lc.OutputTable(client).Columns() +func (lc *ListCmd) CobraCommand(s state.State) *cobra.Command { + outputColumns := lc.OutputTable(s).Columns() cmd := &cobra.Command{ Use: "list [FlAGS]", @@ -40,9 +37,9 @@ func (lc *ListCmd) CobraCommand( ), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: tokenEnsurer.EnsureToken, + PreRunE: s.EnsureToken, RunE: func(cmd *cobra.Command, args []string) error { - return lc.Run(ctx, client, cmd) + return lc.Run(s, cmd) }, } output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(outputColumns), output.OptionJSON(), output.OptionYAML()) @@ -55,7 +52,7 @@ func (lc *ListCmd) CobraCommand( } // Run executes a list command -func (lc *ListCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Command) error { +func (lc *ListCmd) Run(s state.State, cmd *cobra.Command) error { outOpts := output.FlagsForCommand(cmd) labelSelector, _ := cmd.Flags().GetString("selector") @@ -65,7 +62,7 @@ func (lc *ListCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Com } sorts, _ := cmd.Flags().GetStringSlice("sort") - resources, err := lc.Fetch(ctx, client, cmd.Flags(), listOpts, sorts) + resources, err := lc.Fetch(s, cmd.Flags(), listOpts, sorts) if err != nil { return err } @@ -84,7 +81,7 @@ func (lc *ListCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Com cols = outOpts["columns"] } - table := lc.OutputTable(client) + table := lc.OutputTable(s) if !outOpts.IsSet("noheader") { table.WriteHeader(cols) } diff --git a/internal/cmd/base/set_rdns.go b/internal/cmd/base/set_rdns.go index 676d3fdb..d4857df2 100644 --- a/internal/cmd/base/set_rdns.go +++ b/internal/cmd/base/set_rdns.go @@ -1,7 +1,6 @@ package base import ( - "context" "fmt" "net" "reflect" @@ -22,24 +21,22 @@ type SetRdnsCmd struct { ShortDescription string NameSuggestions func(client hcapi2.Client) func() []string AdditionalFlags func(*cobra.Command) - Fetch func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) + Fetch func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) GetDefaultIP func(resource interface{}) net.IP } // CobraCommand creates a command that can be registered with cobra. -func (rc *SetRdnsCmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, -) *cobra.Command { +func (rc *SetRdnsCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("set-rdns [FLAGS] %s", strings.ToUpper(rc.ResourceNameSingular)), Short: rc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(rc.NameSuggestions(client))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(rc.NameSuggestions(s))), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(tokenEnsurer.EnsureToken), + PreRunE: util.ChainRunE(s.EnsureToken), RunE: func(cmd *cobra.Command, args []string) error { - return rc.Run(ctx, client, cmd, actionWaiter, args) + return rc.Run(s, cmd, args) }, } cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry (required)") @@ -53,10 +50,10 @@ func (rc *SetRdnsCmd) CobraCommand( } // Run executes a setRDNS command. -func (rc *SetRdnsCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, actionWaiter state.ActionWaiter, args []string) error { +func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - resource, _, err := rc.Fetch(ctx, client, cmd, idOrName) + resource, _, err := rc.Fetch(s, cmd, idOrName) if err != nil { return err } @@ -72,12 +69,12 @@ func (rc *SetRdnsCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra. ip = rc.GetDefaultIP(resource) } hostname, _ := cmd.Flags().GetString("hostname") - action, _, err := client.RDNS().ChangeDNSPtr(ctx, resource.(hcloud.RDNSSupporter), ip, hcloud.String(hostname)) + action, _, err := s.RDNS().ChangeDNSPtr(s, resource.(hcloud.RDNSSupporter), ip, hcloud.String(hostname)) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/base/update.go b/internal/cmd/base/update.go index 829f6a6e..a2907953 100644 --- a/internal/cmd/base/update.go +++ b/internal/cmd/base/update.go @@ -1,7 +1,6 @@ package base import ( - "context" "fmt" "reflect" "strings" @@ -22,24 +21,22 @@ type UpdateCmd struct { ShortDescription string NameSuggestions func(client hcapi2.Client) func() []string DefineFlags func(*cobra.Command) - Fetch func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) - Update func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error + Fetch func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) + Update func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error } // CobraCommand creates a command that can be registered with cobra. -func (uc *UpdateCmd) CobraCommand( - ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, -) *cobra.Command { +func (uc *UpdateCmd) CobraCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: fmt.Sprintf("update [FLAGS] %s", strings.ToUpper(uc.ResourceNameSingular)), Short: uc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(uc.NameSuggestions(client))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(uc.NameSuggestions(s))), TraverseChildren: true, DisableFlagsInUseLine: true, - PreRunE: util.ChainRunE(tokenEnsurer.EnsureToken), + PreRunE: util.ChainRunE(s.EnsureToken), RunE: func(cmd *cobra.Command, args []string) error { - return uc.Run(ctx, client, cmd, args) + return uc.Run(s, cmd, args) }, } uc.DefineFlags(cmd) @@ -47,10 +44,10 @@ func (uc *UpdateCmd) CobraCommand( } // Run executes a update command. -func (uc *UpdateCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, args []string) error { +func (uc *UpdateCmd) Run(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - resource, _, err := uc.Fetch(ctx, client, cmd, idOrName) + resource, _, err := uc.Fetch(s, cmd, idOrName) if err != nil { return err } @@ -70,7 +67,7 @@ func (uc *UpdateCmd) Run(ctx context.Context, client hcapi2.Client, cmd *cobra.C flags[flag.Name] = flag.Value }) - if err := uc.Update(ctx, client, cmd, resource, flags); err != nil { + if err := uc.Update(s, cmd, resource, flags); err != nil { return fmt.Errorf("updating %s %s failed: %s", uc.ResourceNameSingular, idOrName, err) } diff --git a/internal/cmd/certificate/certificate.go b/internal/cmd/certificate/certificate.go index 550d3c45..b5519c59 100644 --- a/internal/cmd/certificate/certificate.go +++ b/internal/cmd/certificate/certificate.go @@ -3,11 +3,10 @@ package certificate import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "certificate", Short: "Manage certificates", @@ -16,13 +15,13 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), + DeleteCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), ) return cmd diff --git a/internal/cmd/certificate/create.go b/internal/cmd/certificate/create.go index 5e61f60f..173af176 100644 --- a/internal/cmd/certificate/create.go +++ b/internal/cmd/certificate/create.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "fmt" "os" @@ -41,7 +40,7 @@ var CreateCmd = base.CreateCmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, strings []string) (any, any, error) { certType, err := cmd.Flags().GetString("type") if err != nil { return nil, nil, err @@ -49,9 +48,9 @@ var CreateCmd = base.CreateCmd{ var cert *hcloud.Certificate switch hcloud.CertificateType(certType) { case hcloud.CertificateTypeManaged: - cert, err = createManaged(ctx, client, waiter, cmd) + cert, err = createManaged(s, cmd) default: // Uploaded - cert, err = createUploaded(ctx, client, cmd) + cert, err = createUploaded(s, cmd) } if err != nil { return nil, nil, err @@ -60,7 +59,7 @@ var CreateCmd = base.CreateCmd{ }, } -func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Command) (*hcloud.Certificate, error) { +func createUploaded(s state.State, cmd *cobra.Command) (*hcloud.Certificate, error) { var ( name string certFile, keyFile string @@ -96,7 +95,7 @@ func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Comman Certificate: string(certPEM), PrivateKey: string(keyPEM), } - cert, _, err = client.Certificate().Create(ctx, createOpts) + cert, _, err = s.Certificate().Create(s, createOpts) if err != nil { return nil, err } @@ -104,7 +103,7 @@ func createUploaded(ctx context.Context, client hcapi2.Client, cmd *cobra.Comman return cert, nil } -func createManaged(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command) (*hcloud.Certificate, error) { +func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, error) { var ( name string domains []string @@ -127,11 +126,11 @@ func createManaged(ctx context.Context, client hcapi2.Client, waiter state.Actio Type: hcloud.CertificateTypeManaged, DomainNames: domains, } - res, _, err = client.Certificate().CreateCertificate(ctx, createOpts) + res, _, err = s.Certificate().CreateCertificate(s, createOpts) if err != nil { return nil, err } - if err := waiter.ActionProgress(cmd, ctx, res.Action); err != nil { + if err := s.ActionProgress(cmd, s, res.Action); err != nil { return nil, err } cmd.Printf("Certificate %d created\n", res.Certificate.ID) diff --git a/internal/cmd/certificate/create_test.go b/internal/cmd/certificate/create_test.go index 1fc70903..b197e2c8 100644 --- a/internal/cmd/certificate/create_test.go +++ b/internal/cmd/certificate/create_test.go @@ -1,7 +1,6 @@ package certificate import ( - "context" _ "embed" "testing" "time" @@ -23,11 +22,7 @@ func TestCreateManaged(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). @@ -60,11 +55,7 @@ func TestCreateManagedJSON(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). @@ -113,11 +104,7 @@ func TestCreateUploaded(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). @@ -145,11 +132,7 @@ func TestCreateUploadedJSON(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). diff --git a/internal/cmd/certificate/delete.go b/internal/cmd/certificate/delete.go index a1d88bee..9298f1a8 100644 --- a/internal/cmd/certificate/delete.go +++ b/internal/cmd/certificate/delete.go @@ -1,8 +1,6 @@ package certificate import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "certificate", ShortDescription: "Delete a certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Certificate().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Certificate().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { certificate := resource.(*hcloud.Certificate) - if _, err := client.Certificate().Delete(ctx, certificate); err != nil { + if _, err := s.Certificate().Delete(s, certificate); err != nil { return err } return nil diff --git a/internal/cmd/certificate/delete_test.go b/internal/cmd/certificate/delete_test.go index 8cc168a8..7d66a6bf 100644 --- a/internal/cmd/certificate/delete_test.go +++ b/internal/cmd/certificate/delete_test.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() cert := &hcloud.Certificate{ diff --git a/internal/cmd/certificate/describe.go b/internal/cmd/certificate/describe.go index c675ec09..75cfedc8 100644 --- a/internal/cmd/certificate/describe.go +++ b/internal/cmd/certificate/describe.go @@ -1,14 +1,13 @@ package certificate import ( - "context" - "github.com/dustin/go-humanize" "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" ) @@ -18,14 +17,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "certificate", JSONKeyGetByName: "certificates", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Certificate().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - cert, _, err := client.Certificate().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + cert, _, err := s.Certificate().Get(s, idOrName) if err != nil { return nil, nil, err } return cert, hcloud.SchemaFromCertificate(cert), nil }, - PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { cert := resource.(*hcloud.Certificate) cmd.Printf("ID:\t\t\t%d\n", cert.ID) cmd.Printf("Name:\t\t\t%s\n", cert.Name) @@ -67,7 +66,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf(" - ID: %d\n", ub.ID) continue } - cmd.Printf(" - Name: %s\n", client.LoadBalancer().LoadBalancerName(ub.ID)) + cmd.Printf(" - Name: %s\n", s.LoadBalancer().LoadBalancerName(ub.ID)) } } return nil diff --git a/internal/cmd/certificate/describe_test.go b/internal/cmd/certificate/describe_test.go index b01a2904..6e7b0bee 100644 --- a/internal/cmd/certificate/describe_test.go +++ b/internal/cmd/certificate/describe_test.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "fmt" "testing" "time" @@ -21,10 +20,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() cert := &hcloud.Certificate{ diff --git a/internal/cmd/certificate/labels.go b/internal/cmd/certificate/labels.go index 7803ddc6..6080b00d 100644 --- a/internal/cmd/certificate/labels.go +++ b/internal/cmd/certificate/labels.go @@ -1,11 +1,11 @@ package certificate import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from an certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Certificate().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Certificate().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - certificate, _, err := client.Certificate().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + certificate, _, err := s.Certificate().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return certificate.Labels, certificate.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.CertificateUpdateOpts{ Labels: labels, } - _, _, err := client.Certificate().Update(ctx, &hcloud.Certificate{ID: id}, opts) + _, _, err := s.Certificate().Update(s, &hcloud.Certificate{ID: id}, opts) return err }, } diff --git a/internal/cmd/certificate/labels_test.go b/internal/cmd/certificate/labels_test.go index 78f25e26..d50cf046 100644 --- a/internal/cmd/certificate/labels_test.go +++ b/internal/cmd/certificate/labels_test.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). diff --git a/internal/cmd/certificate/list.go b/internal/cmd/certificate/list.go index 0660e9ed..d45c29a2 100644 --- a/internal/cmd/certificate/list.go +++ b/internal/cmd/certificate/list.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "strings" "time" @@ -11,6 +10,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -20,12 +20,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "certificates", DefaultColumns: []string{"id", "name", "type", "domain_names", "not_valid_after", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.CertificateListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - certificates, err := client.Certificate().AllWithOpts(ctx, opts) + certificates, err := s.Certificate().AllWithOpts(s, opts) var resources []interface{} for _, n := range certificates { diff --git a/internal/cmd/certificate/list_test.go b/internal/cmd/certificate/list_test.go index b9867869..fb7fe2ed 100644 --- a/internal/cmd/certificate/list_test.go +++ b/internal/cmd/certificate/list_test.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). diff --git a/internal/cmd/certificate/update.go b/internal/cmd/certificate/update.go index a793b2d3..55985934 100644 --- a/internal/cmd/certificate/update.go +++ b/internal/cmd/certificate/update.go @@ -1,13 +1,12 @@ package certificate import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "certificate", ShortDescription: "Update a certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Certificate().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Certificate().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Certificate Name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { certificate := resource.(*hcloud.Certificate) updOpts := hcloud.CertificateUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.Certificate().Update(ctx, certificate, updOpts) + _, _, err := s.Certificate().Update(s, certificate, updOpts) if err != nil { return err } diff --git a/internal/cmd/certificate/update_test.go b/internal/cmd/certificate/update_test.go index a5e5f79e..91a3928d 100644 --- a/internal/cmd/certificate/update_test.go +++ b/internal/cmd/certificate/update_test.go @@ -1,7 +1,6 @@ package certificate import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.CertificateClient.EXPECT(). diff --git a/internal/cmd/completion/completion.go b/internal/cmd/completion/completion.go index 791462fc..b691efd9 100644 --- a/internal/cmd/completion/completion.go +++ b/internal/cmd/completion/completion.go @@ -31,7 +31,7 @@ import ( // source <(hcloud completion zsh)` // ) -func NewCommand(cli *state.State) *cobra.Command { +func NewCommand(_ state.State) *cobra.Command { cmd := &cobra.Command{ Use: "completion [FLAGS] SHELL", Short: "Output shell completion code for the specified shell", diff --git a/internal/cmd/context/active.go b/internal/cmd/context/active.go index 7831de35..44608f45 100644 --- a/internal/cmd/context/active.go +++ b/internal/cmd/context/active.go @@ -9,24 +9,24 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -func newActiveCommand(cli *state.State) *cobra.Command { +func newActiveCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "active [FLAGS]", Short: "Show active context", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runActive), + RunE: state.Wrap(s, runActive), } return cmd } -func runActive(cli *state.State, cmd *cobra.Command, args []string) error { +func runActive(s state.State, cmd *cobra.Command, _ []string) error { if os.Getenv("HCLOUD_TOKEN") != "" { _, _ = fmt.Fprintln(os.Stderr, "Warning: HCLOUD_TOKEN is set. The active context will have no effect.") } - if cli.Config.ActiveContext != nil { - cmd.Println(cli.Config.ActiveContext.Name) + if cfg := s.Config(); cfg.ActiveContext != nil { + cmd.Println(cfg.ActiveContext.Name) } return nil } diff --git a/internal/cmd/context/context.go b/internal/cmd/context/context.go index 251d0a5b..b7138297 100644 --- a/internal/cmd/context/context.go +++ b/internal/cmd/context/context.go @@ -6,7 +6,7 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "context [FLAGS]", Short: "Manage contexts", @@ -15,11 +15,11 @@ func NewCommand(cli *state.State) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - newCreateCommand(cli), - newActiveCommand(cli), - newUseCommand(cli), - newDeleteCommand(cli), - newListCommand(cli), + newCreateCommand(s), + newActiveCommand(s), + newUseCommand(s), + newDeleteCommand(s), + newListCommand(s), ) return cmd } diff --git a/internal/cmd/context/create.go b/internal/cmd/context/create.go index 9bfb576b..4a33762f 100644 --- a/internal/cmd/context/create.go +++ b/internal/cmd/context/create.go @@ -14,19 +14,20 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -func newCreateCommand(cli *state.State) *cobra.Command { +func newCreateCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "create [FLAGS] NAME", Short: "Create a new context", Args: cobra.ExactArgs(1), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runCreate), + RunE: state.Wrap(s, runCreate), } return cmd } -func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { +func runCreate(s state.State, cmd *cobra.Command, args []string) error { + cfg := s.Config() if !state.StdoutIsTerminal() { return errors.New("context create is an interactive command") } @@ -35,7 +36,7 @@ func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { if name == "" { return errors.New("invalid name") } - if cli.Config.ContextByName(name) != nil { + if cfg.ContextByName(name) != nil { return errors.New("name already used") } @@ -81,10 +82,10 @@ func runCreate(cli *state.State, cmd *cobra.Command, args []string) error { context.Token = token - cli.Config.Contexts = append(cli.Config.Contexts, context) - cli.Config.ActiveContext = context + cfg.Contexts = append(cfg.Contexts, context) + cfg.ActiveContext = context - if err := cli.WriteConfig(); err != nil { + if err := s.WriteConfig(); err != nil { return err } diff --git a/internal/cmd/context/delete.go b/internal/cmd/context/delete.go index 8b9423a4..0c5f5467 100644 --- a/internal/cmd/context/delete.go +++ b/internal/cmd/context/delete.go @@ -10,29 +10,30 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -func newDeleteCommand(cli *state.State) *cobra.Command { +func newDeleteCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "delete [FLAGS] NAME", Short: "Delete a context", Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.Config.ContextNames)), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(s.Config().ContextNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runDelete), + RunE: state.Wrap(s, runDelete), } return cmd } -func runDelete(cli *state.State, cmd *cobra.Command, args []string) error { +func runDelete(s state.State, _ *cobra.Command, args []string) error { name := args[0] - context := cli.Config.ContextByName(name) + cfg := s.Config() + context := cfg.ContextByName(name) if context == nil { return fmt.Errorf("context not found: %v", name) } - if cli.Config.ActiveContext == context { + if cfg.ActiveContext == context { _, _ = fmt.Fprintln(os.Stderr, "Warning: You are deleting the currently active context. Please select a new active context.") - cli.Config.ActiveContext = nil + cfg.ActiveContext = nil } - cli.Config.RemoveContext(context) - return cli.WriteConfig() + cfg.RemoveContext(context) + return s.WriteConfig() } diff --git a/internal/cmd/context/list.go b/internal/cmd/context/list.go index 2c10cbc6..4d1ce092 100644 --- a/internal/cmd/context/list.go +++ b/internal/cmd/context/list.go @@ -22,7 +22,7 @@ func init() { RemoveAllowedField("token") } -func newListCommand(cli *state.State) *cobra.Command { +func newListCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "list [FLAGS]", Short: "List contexts", @@ -33,13 +33,13 @@ func newListCommand(cli *state.State) *cobra.Command { Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runList), + RunE: state.Wrap(s, runList), } output.AddFlag(cmd, output.OptionNoHeader(), output.OptionColumns(listTableOutput.Columns())) return cmd } -func runList(cli *state.State, cmd *cobra.Command, args []string) error { +func runList(s state.State, cmd *cobra.Command, _ []string) error { outOpts := output.FlagsForCommand(cmd) cols := []string{"active", "name"} @@ -55,13 +55,14 @@ func runList(cli *state.State, cmd *cobra.Command, args []string) error { if !outOpts.IsSet("noheader") { tw.WriteHeader(cols) } - for _, context := range cli.Config.Contexts { + cfg := s.Config() + for _, context := range cfg.Contexts { presentation := ContextPresentation{ Name: context.Name, Token: context.Token, Active: " ", } - if cli.Config.ActiveContext != nil && cli.Config.ActiveContext.Name == context.Name { + if ctx := cfg.ActiveContext; ctx != nil && ctx.Name == context.Name { presentation.Active = "*" } diff --git a/internal/cmd/context/use.go b/internal/cmd/context/use.go index cc5d4caa..e82c4f06 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -10,28 +10,29 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -func newUseCommand(cli *state.State) *cobra.Command { +func newUseCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "use [FLAGS] NAME", Short: "Use a context", Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.Config.ContextNames)), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(s.Config().ContextNames)), TraverseChildren: true, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runUse), + RunE: state.Wrap(s, runUse), } return cmd } -func runUse(cli *state.State, cmd *cobra.Command, args []string) error { +func runUse(s state.State, _ *cobra.Command, args []string) error { if os.Getenv("HCLOUD_TOKEN") != "" { _, _ = fmt.Fprintln(os.Stderr, "Warning: HCLOUD_TOKEN is set. The active context will have no effect.") } name := args[0] - context := cli.Config.ContextByName(name) + cfg := s.Config() + context := cfg.ContextByName(name) if context == nil { return fmt.Errorf("context not found: %v", name) } - cli.Config.ActiveContext = context - return cli.WriteConfig() + cfg.ActiveContext = context + return s.WriteConfig() } diff --git a/internal/cmd/datacenter/datacenter.go b/internal/cmd/datacenter/datacenter.go index 523da491..a5e55d7a 100644 --- a/internal/cmd/datacenter/datacenter.go +++ b/internal/cmd/datacenter/datacenter.go @@ -3,11 +3,10 @@ package datacenter import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "datacenter", Short: "Manage datacenters", @@ -16,8 +15,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index 70103586..7d73f88c 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -1,12 +1,11 @@ package datacenter import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -16,14 +15,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "datacenter", JSONKeyGetByName: "datacenters", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Datacenter().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - dc, _, err := client.Datacenter().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + dc, _, err := s.Datacenter().Get(s, idOrName) if err != nil { return nil, nil, err } return dc, hcloud.SchemaFromDatacenter(dc), nil }, - PrintText: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { datacenter := resource.(*hcloud.Datacenter) cmd.Printf("ID:\t\t%d\n", datacenter.ID) @@ -41,8 +40,8 @@ var DescribeCmd = base.DescribeCmd{ printServerTypes := func(list []*hcloud.ServerType) { for _, t := range list { cmd.Printf(" - ID:\t\t %d\n", t.ID) - cmd.Printf(" Name:\t %s\n", client.ServerType().ServerTypeName(t.ID)) - cmd.Printf(" Description: %s\n", client.ServerType().ServerTypeDescription(t.ID)) + cmd.Printf(" Name:\t %s\n", s.ServerType().ServerTypeName(t.ID)) + cmd.Printf(" Description: %s\n", s.ServerType().ServerTypeDescription(t.ID)) } } diff --git a/internal/cmd/datacenter/describe_test.go b/internal/cmd/datacenter/describe_test.go index ac60a916..47cc0dc5 100644 --- a/internal/cmd/datacenter/describe_test.go +++ b/internal/cmd/datacenter/describe_test.go @@ -1,7 +1,6 @@ package datacenter import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestDescribe(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.DatacenterClient.EXPECT(). diff --git a/internal/cmd/datacenter/list.go b/internal/cmd/datacenter/list.go index 654efb22..b74cece3 100644 --- a/internal/cmd/datacenter/list.go +++ b/internal/cmd/datacenter/list.go @@ -1,13 +1,12 @@ package datacenter import ( - "context" - "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -17,12 +16,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "datacenters", DefaultColumns: []string{"id", "name", "description", "location"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.DatacenterListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - datacenters, err := client.Datacenter().AllWithOpts(ctx, opts) + datacenters, err := s.Datacenter().AllWithOpts(s, opts) var resources []interface{} for _, n := range datacenters { resources = append(resources, n) diff --git a/internal/cmd/datacenter/list_test.go b/internal/cmd/datacenter/list_test.go index d19b3ab1..ca8d202d 100644 --- a/internal/cmd/datacenter/list_test.go +++ b/internal/cmd/datacenter/list_test.go @@ -1,7 +1,6 @@ package datacenter import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.DatacenterClient.EXPECT(). diff --git a/internal/cmd/firewall/add_rule.go b/internal/cmd/firewall/add_rule.go index 480f423a..b220e1f9 100644 --- a/internal/cmd/firewall/add_rule.go +++ b/internal/cmd/firewall/add_rule.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "fmt" "net" @@ -41,7 +40,7 @@ var AddRuleCmd = base.Cmd{ cmd.Flags().String("description", "", "Description of the firewall rule") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { direction, _ := cmd.Flags().GetString("direction") protocol, _ := cmd.Flags().GetString("protocol") sourceIPs, _ := cmd.Flags().GetStringArray("source-ips") @@ -50,7 +49,7 @@ var AddRuleCmd = base.Cmd{ description, _ := cmd.Flags().GetString("description") idOrName := args[0] - firewall, _, err := client.Firewall().Get(ctx, idOrName) + firewall, _, err := s.Firewall().Get(s, idOrName) if err != nil { return err } @@ -107,13 +106,13 @@ var AddRuleCmd = base.Cmd{ rules := append(firewall.Rules, rule) - actions, _, err := client.Firewall().SetRules(ctx, firewall, + actions, _, err := s.Firewall().SetRules(s, firewall, hcloud.FirewallSetRulesOpts{Rules: rules}, ) if err != nil { return err } - if err := waiter.WaitForActions(cmd, ctx, actions); err != nil { + if err := s.WaitForActions(cmd, s, actions); err != nil { return err } diff --git a/internal/cmd/firewall/add_rule_test.go b/internal/cmd/firewall/add_rule_test.go index e22ed9b8..10d3f256 100644 --- a/internal/cmd/firewall/add_rule_test.go +++ b/internal/cmd/firewall/add_rule_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestAddRule(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AddRuleCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AddRuleCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/apply_to_resource.go b/internal/cmd/firewall/apply_to_resource.go index 45c86da0..f501ad11 100644 --- a/internal/cmd/firewall/apply_to_resource.go +++ b/internal/cmd/firewall/apply_to_resource.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "fmt" "github.com/spf13/cobra" @@ -33,7 +32,7 @@ var ApplyToResourceCmd = base.Cmd{ cmd.Flags().StringP("label-selector", "l", "", "Label Selector") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { resourceType, _ := cmd.Flags().GetString("type") switch resourceType { @@ -54,7 +53,7 @@ var ApplyToResourceCmd = base.Cmd{ serverIdOrName, _ := cmd.Flags().GetString("server") labelSelector, _ := cmd.Flags().GetString("label-selector") idOrName := args[0] - firewall, _, err := client.Firewall().Get(ctx, idOrName) + firewall, _, err := s.Firewall().Get(s, idOrName) if err != nil { return err } @@ -65,7 +64,7 @@ var ApplyToResourceCmd = base.Cmd{ switch opts.Type { case hcloud.FirewallResourceTypeServer: - server, _, err := client.Server().Get(ctx, serverIdOrName) + server, _, err := s.Server().Get(s, serverIdOrName) if err != nil { return err } @@ -79,11 +78,11 @@ var ApplyToResourceCmd = base.Cmd{ return fmt.Errorf("unknown type %s", opts.Type) } - actions, _, err := client.Firewall().ApplyResources(ctx, firewall, []hcloud.FirewallResource{opts}) + actions, _, err := s.Firewall().ApplyResources(s, firewall, []hcloud.FirewallResource{opts}) if err != nil { return err } - if err := waiter.WaitForActions(cmd, ctx, actions); err != nil { + if err := s.WaitForActions(cmd, s, actions); err != nil { return err } cmd.Printf("Firewall %d applied\n", firewall.ID) diff --git a/internal/cmd/firewall/apply_to_resource_test.go b/internal/cmd/firewall/apply_to_resource_test.go index 3302bf42..67995c92 100644 --- a/internal/cmd/firewall/apply_to_resource_test.go +++ b/internal/cmd/firewall/apply_to_resource_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestApplyToServer(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ApplyToResourceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ApplyToResourceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ @@ -57,11 +52,7 @@ func TestApplyToLabelSelector(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ApplyToResourceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ApplyToResourceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/create.go b/internal/cmd/firewall/create.go index 70c12965..d8d024d4 100644 --- a/internal/cmd/firewall/create.go +++ b/internal/cmd/firewall/create.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -33,7 +32,7 @@ var CreateCmd = base.CreateCmd{ cmd.Flags().String("rules-file", "", "JSON file containing your routes (use - to read from stdin). The structure of the file needs to be the same as within the API: https://docs.hetzner.cloud/#firewalls-get-a-firewall ") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, strings []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, strings []string) (any, any, error) { name, _ := cmd.Flags().GetString("name") labels, _ := cmd.Flags().GetStringToString("label") @@ -79,12 +78,12 @@ var CreateCmd = base.CreateCmd{ } } - result, _, err := client.Firewall().Create(ctx, opts) + result, _, err := s.Firewall().Create(s, opts) if err != nil { return nil, nil, err } - if err := waiter.WaitForActions(cmd, ctx, result.Actions); err != nil { + if err := s.WaitForActions(cmd, s, result.Actions); err != nil { return nil, nil, err } diff --git a/internal/cmd/firewall/create_test.go b/internal/cmd/firewall/create_test.go index a5cf239a..90e4068d 100644 --- a/internal/cmd/firewall/create_test.go +++ b/internal/cmd/firewall/create_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" _ "embed" "net" "testing" @@ -21,11 +20,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FirewallClient.EXPECT(). @@ -55,11 +50,7 @@ func TestCreateJSON(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FirewallClient.EXPECT(). diff --git a/internal/cmd/firewall/delete.go b/internal/cmd/firewall/delete.go index b2f0edb3..ca4b0b49 100644 --- a/internal/cmd/firewall/delete.go +++ b/internal/cmd/firewall/delete.go @@ -1,8 +1,6 @@ package firewall import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "firewall", ShortDescription: "Delete a firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Firewall().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Firewall().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { firewall := resource.(*hcloud.Firewall) - if _, err := client.Firewall().Delete(ctx, firewall); err != nil { + if _, err := s.Firewall().Delete(s, firewall); err != nil { return err } return nil diff --git a/internal/cmd/firewall/delete_rule.go b/internal/cmd/firewall/delete_rule.go index 78c2a369..804e16e7 100644 --- a/internal/cmd/firewall/delete_rule.go +++ b/internal/cmd/firewall/delete_rule.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "fmt" "net" "reflect" @@ -42,7 +41,7 @@ var DeleteRuleCmd = base.Cmd{ cmd.Flags().String("description", "", "Description of the firewall rule") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { direction, _ := cmd.Flags().GetString("direction") protocol, _ := cmd.Flags().GetString("protocol") sourceIPs, _ := cmd.Flags().GetStringArray("source-ips") @@ -51,7 +50,7 @@ var DeleteRuleCmd = base.Cmd{ description, _ := cmd.Flags().GetString("description") idOrName := args[0] - firewall, _, err := client.Firewall().Get(ctx, idOrName) + firewall, _, err := s.Firewall().Get(s, idOrName) if err != nil { return err } @@ -115,13 +114,13 @@ var DeleteRuleCmd = base.Cmd{ if len(rules) == len(firewall.Rules) { return fmt.Errorf("the specified rule was not found in the ruleset of Firewall %d", firewall.ID) } - actions, _, err := client.Firewall().SetRules(ctx, firewall, + actions, _, err := s.Firewall().SetRules(s, firewall, hcloud.FirewallSetRulesOpts{Rules: rules}, ) if err != nil { return err } - if err := waiter.WaitForActions(cmd, ctx, actions); err != nil { + if err := s.WaitForActions(cmd, s, actions); err != nil { return err } cmd.Printf("Firewall Rules for Firewall %d updated\n", firewall.ID) diff --git a/internal/cmd/firewall/delete_rule_test.go b/internal/cmd/firewall/delete_rule_test.go index 9b818c7e..47d8f9e2 100644 --- a/internal/cmd/firewall/delete_rule_test.go +++ b/internal/cmd/firewall/delete_rule_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestDeleteRule(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteRuleCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteRuleCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/delete_test.go b/internal/cmd/firewall/delete_test.go index 5bf36320..ebc7c4da 100644 --- a/internal/cmd/firewall/delete_test.go +++ b/internal/cmd/firewall/delete_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/describe.go b/internal/cmd/firewall/describe.go index 45367df7..b2567541 100644 --- a/internal/cmd/firewall/describe.go +++ b/internal/cmd/firewall/describe.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "net" "github.com/dustin/go-humanize" @@ -10,6 +9,7 @@ import ( "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" ) @@ -19,14 +19,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "firewall", JSONKeyGetByName: "firewalls", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - fw, _, err := client.Firewall().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + fw, _, err := s.Firewall().Get(s, idOrName) if err != nil { return nil, nil, err } return fw, hcloud.SchemaFromFirewall(fw), nil }, - PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { firewall := resource.(*hcloud.Firewall) cmd.Printf("ID:\t\t%d\n", firewall.ID) @@ -80,7 +80,7 @@ var DescribeCmd = base.DescribeCmd{ switch resource.Type { case hcloud.FirewallResourceTypeServer: cmd.Printf(" Server ID:\t\t%d\n", resource.Server.ID) - cmd.Printf(" Server Name:\t%s\n", client.Server().ServerName(resource.Server.ID)) + cmd.Printf(" Server Name:\t%s\n", s.Server().ServerName(resource.Server.ID)) case hcloud.FirewallResourceTypeLabelSelector: cmd.Printf(" Label Selector:\t%s\n", resource.LabelSelector.Selector) } diff --git a/internal/cmd/firewall/describe_test.go b/internal/cmd/firewall/describe_test.go index 4cda263d..aef61a0f 100644 --- a/internal/cmd/firewall/describe_test.go +++ b/internal/cmd/firewall/describe_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "fmt" "testing" "time" @@ -21,10 +20,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/firewall.go b/internal/cmd/firewall/firewall.go index c04b7f59..45b6cd42 100644 --- a/internal/cmd/firewall/firewall.go +++ b/internal/cmd/firewall/firewall.go @@ -3,11 +3,10 @@ package firewall import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "firewall", Short: "Manage Firewalls", @@ -16,18 +15,18 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - ReplaceRulesCmd.CobraCommand(cli.Context, client, cli, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - AddRuleCmd.CobraCommand(cli.Context, client, cli, cli), - DeleteRuleCmd.CobraCommand(cli.Context, client, cli, cli), - ApplyToResourceCmd.CobraCommand(cli.Context, client, cli, cli), - RemoveFromResourceCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + ReplaceRulesCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + AddRuleCmd.CobraCommand(s), + DeleteRuleCmd.CobraCommand(s), + ApplyToResourceCmd.CobraCommand(s), + RemoveFromResourceCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), ) return cmd } diff --git a/internal/cmd/firewall/labels.go b/internal/cmd/firewall/labels.go index 0df33515..46902535 100644 --- a/internal/cmd/firewall/labels.go +++ b/internal/cmd/firewall/labels.go @@ -1,11 +1,11 @@ package firewall import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from an firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Firewall().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - firewall, _, err := client.Firewall().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + firewall, _, err := s.Firewall().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return firewall.Labels, firewall.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.FirewallUpdateOpts{ Labels: labels, } - _, _, err := client.Firewall().Update(ctx, &hcloud.Firewall{ID: id}, opts) + _, _, err := s.Firewall().Update(s, &hcloud.Firewall{ID: id}, opts) return err }, } diff --git a/internal/cmd/firewall/labels_test.go b/internal/cmd/firewall/labels_test.go index fc952527..9e55df3f 100644 --- a/internal/cmd/firewall/labels_test.go +++ b/internal/cmd/firewall/labels_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FirewallClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FirewallClient.EXPECT(). diff --git a/internal/cmd/firewall/list.go b/internal/cmd/firewall/list.go index b1ea73c7..919c65ec 100644 --- a/internal/cmd/firewall/list.go +++ b/internal/cmd/firewall/list.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "fmt" "github.com/spf13/pflag" @@ -9,6 +8,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -18,12 +18,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "firewalls", DefaultColumns: []string{"id", "name", "rules_count", "applied_to_count"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.FirewallListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - firewalls, err := client.Firewall().AllWithOpts(ctx, opts) + firewalls, err := s.Firewall().AllWithOpts(s, opts) var resources []interface{} for _, n := range firewalls { diff --git a/internal/cmd/firewall/list_test.go b/internal/cmd/firewall/list_test.go index a808b6e5..640cf74b 100644 --- a/internal/cmd/firewall/list_test.go +++ b/internal/cmd/firewall/list_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FirewallClient.EXPECT(). diff --git a/internal/cmd/firewall/remove_from_resource.go b/internal/cmd/firewall/remove_from_resource.go index e3b0b2cd..c4211acf 100644 --- a/internal/cmd/firewall/remove_from_resource.go +++ b/internal/cmd/firewall/remove_from_resource.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "fmt" "github.com/spf13/cobra" @@ -33,7 +32,7 @@ var RemoveFromResourceCmd = base.Cmd{ cmd.Flags().StringP("label-selector", "l", "", "Label Selector") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { resourceType, _ := cmd.Flags().GetString("type") switch resourceType { @@ -54,7 +53,7 @@ var RemoveFromResourceCmd = base.Cmd{ labelSelector, _ := cmd.Flags().GetString("label-selector") idOrName := args[0] - firewall, _, err := client.Firewall().Get(ctx, idOrName) + firewall, _, err := s.Firewall().Get(s, idOrName) if err != nil { return err } @@ -65,7 +64,7 @@ var RemoveFromResourceCmd = base.Cmd{ switch opts.Type { case hcloud.FirewallResourceTypeServer: - server, _, err := client.Server().Get(ctx, serverIdOrName) + server, _, err := s.Server().Get(s, serverIdOrName) if err != nil { return err } @@ -78,11 +77,11 @@ var RemoveFromResourceCmd = base.Cmd{ default: return fmt.Errorf("unknown type %s", opts.Type) } - actions, _, err := client.Firewall().RemoveResources(ctx, firewall, []hcloud.FirewallResource{opts}) + actions, _, err := s.Firewall().RemoveResources(s, firewall, []hcloud.FirewallResource{opts}) if err != nil { return err } - if err := waiter.WaitForActions(cmd, ctx, actions); err != nil { + if err := s.WaitForActions(cmd, s, actions); err != nil { return err } cmd.Printf("Firewall %d applied\n", firewall.ID) diff --git a/internal/cmd/firewall/remove_from_resource_test.go b/internal/cmd/firewall/remove_from_resource_test.go index d0932374..26825eea 100644 --- a/internal/cmd/firewall/remove_from_resource_test.go +++ b/internal/cmd/firewall/remove_from_resource_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestRemoveFromServer(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := RemoveFromResourceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := RemoveFromResourceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ @@ -57,11 +52,7 @@ func TestRemoveFromLabelSelector(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := RemoveFromResourceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := RemoveFromResourceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/replace_rules.go b/internal/cmd/firewall/replace_rules.go index 3b23dbd7..3ea90aca 100644 --- a/internal/cmd/firewall/replace_rules.go +++ b/internal/cmd/firewall/replace_rules.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "encoding/json" "fmt" "io/ioutil" @@ -32,9 +31,9 @@ var ReplaceRulesCmd = base.Cmd{ cmd.MarkFlagRequired("rules-file") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - firewall, _, err := client.Firewall().Get(ctx, idOrName) + firewall, _, err := s.Firewall().Get(s, idOrName) if err != nil { return err } @@ -91,11 +90,11 @@ var ReplaceRulesCmd = base.Cmd{ opts.Rules = append(opts.Rules, r) } - actions, _, err := client.Firewall().SetRules(ctx, firewall, opts) + actions, _, err := s.Firewall().SetRules(s, firewall, opts) if err != nil { return err } - if err := waiter.WaitForActions(cmd, ctx, actions); err != nil { + if err := s.WaitForActions(cmd, s, actions); err != nil { return err } cmd.Printf("Firewall Rules for Firewall %d updated\n", firewall.ID) diff --git a/internal/cmd/firewall/replace_rules_test.go b/internal/cmd/firewall/replace_rules_test.go index 9637493f..da53139e 100644 --- a/internal/cmd/firewall/replace_rules_test.go +++ b/internal/cmd/firewall/replace_rules_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestReplaceRules(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ReplaceRulesCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ReplaceRulesCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() firewall := &hcloud.Firewall{ diff --git a/internal/cmd/firewall/update.go b/internal/cmd/firewall/update.go index fc53a6a8..8767b80e 100644 --- a/internal/cmd/firewall/update.go +++ b/internal/cmd/firewall/update.go @@ -1,13 +1,12 @@ package firewall import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Firewall", ShortDescription: "Update a firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Firewall().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Firewall().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Firewall name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { firewall := resource.(*hcloud.Firewall) updOpts := hcloud.FirewallUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.Firewall().Update(ctx, firewall, updOpts) + _, _, err := s.Firewall().Update(s, firewall, updOpts) if err != nil { return err } diff --git a/internal/cmd/firewall/update_test.go b/internal/cmd/firewall/update_test.go index 77013a17..b71029a9 100644 --- a/internal/cmd/firewall/update_test.go +++ b/internal/cmd/firewall/update_test.go @@ -1,7 +1,6 @@ package firewall import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FirewallClient.EXPECT(). diff --git a/internal/cmd/floatingip/assign.go b/internal/cmd/floatingip/assign.go index 5b3fef37..f0b6b2c2 100644 --- a/internal/cmd/floatingip/assign.go +++ b/internal/cmd/floatingip/assign.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,9 +25,9 @@ var AssignCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := client.FloatingIP().Get(ctx, idOrName) + floatingIP, _, err := s.FloatingIP().Get(s, idOrName) if err != nil { return err } @@ -37,7 +36,7 @@ var AssignCmd = base.Cmd{ } serverIDOrName := args[1] - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return err } @@ -45,12 +44,12 @@ var AssignCmd = base.Cmd{ return fmt.Errorf("server not found: %s", serverIDOrName) } - action, _, err := client.FloatingIP().Assign(ctx, floatingIP, server) + action, _, err := s.FloatingIP().Assign(s, floatingIP, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/floatingip/assign_test.go b/internal/cmd/floatingip/assign_test.go index fda45555..8f6871b4 100644 --- a/internal/cmd/floatingip/assign_test.go +++ b/internal/cmd/floatingip/assign_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestAssign(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AssignCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AssignCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/floatingip/create.go b/internal/cmd/floatingip/create.go index b977479c..dadd3ecd 100644 --- a/internal/cmd/floatingip/create.go +++ b/internal/cmd/floatingip/create.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "errors" "fmt" @@ -45,7 +44,7 @@ var CreateCmd = base.CreateCmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { typ, _ := cmd.Flags().GetString("type") if typ == "" { return nil, nil, errors.New("type is required") @@ -80,7 +79,7 @@ var CreateCmd = base.CreateCmd{ createOpts.HomeLocation = &hcloud.Location{Name: homeLocation} } if serverNameOrID != "" { - server, _, err := client.Server().Get(ctx, serverNameOrID) + server, _, err := s.Server().Get(s, serverNameOrID) if err != nil { return nil, nil, err } @@ -90,27 +89,27 @@ var CreateCmd = base.CreateCmd{ createOpts.Server = server } - result, _, err := client.FloatingIP().Create(ctx, createOpts) + result, _, err := s.FloatingIP().Create(s, createOpts) if err != nil { return nil, nil, err } if result.Action != nil { - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } } cmd.Printf("Floating IP %d created\n", result.FloatingIP.ID) - if err := changeProtection(ctx, client, waiter, cmd, result.FloatingIP, true, protectionOps); err != nil { + if err := changeProtection(s, cmd, result.FloatingIP, true, protectionOps); err != nil { return nil, nil, err } return result.FloatingIP, util.Wrap("floating_ip", hcloud.SchemaFromFloatingIP(result.FloatingIP)), nil }, - PrintResource: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource any) { + PrintResource: func(s state.State, cmd *cobra.Command, resource any) { floatingIP := resource.(*hcloud.FloatingIP) cmd.Printf("IP%s: %s\n", floatingIP.Type[2:], floatingIP.IP) }, diff --git a/internal/cmd/floatingip/create_test.go b/internal/cmd/floatingip/create_test.go index f17acd0f..608d3bf0 100644 --- a/internal/cmd/floatingip/create_test.go +++ b/internal/cmd/floatingip/create_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" _ "embed" "net" "testing" @@ -20,11 +19,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). @@ -59,11 +54,7 @@ func TestCreateJSON(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). @@ -99,11 +90,7 @@ func TestCreateProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() floatingIp := &hcloud.FloatingIP{ diff --git a/internal/cmd/floatingip/delete.go b/internal/cmd/floatingip/delete.go index 334b03a9..66139746 100644 --- a/internal/cmd/floatingip/delete.go +++ b/internal/cmd/floatingip/delete.go @@ -1,8 +1,6 @@ package floatingip import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Delete a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.FloatingIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.FloatingIP().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { floatingIP := resource.(*hcloud.FloatingIP) - if _, err := client.FloatingIP().Delete(ctx, floatingIP); err != nil { + if _, err := s.FloatingIP().Delete(s, floatingIP); err != nil { return err } return nil diff --git a/internal/cmd/floatingip/delete_test.go b/internal/cmd/floatingip/delete_test.go index 12291c20..6e3bfcce 100644 --- a/internal/cmd/floatingip/delete_test.go +++ b/internal/cmd/floatingip/delete_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() floatingIP := &hcloud.FloatingIP{ diff --git a/internal/cmd/floatingip/describe.go b/internal/cmd/floatingip/describe.go index 3f5b492b..8111abfa 100644 --- a/internal/cmd/floatingip/describe.go +++ b/internal/cmd/floatingip/describe.go @@ -1,14 +1,13 @@ package floatingip import ( - "context" - "github.com/dustin/go-humanize" "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" ) @@ -18,14 +17,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "floating_ip", JSONKeyGetByName: "floating_ips", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - ip, _, err := client.FloatingIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + ip, _, err := s.FloatingIP().Get(s, idOrName) if err != nil { return nil, nil, err } return ip, hcloud.SchemaFromFloatingIP(ip), nil }, - PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { floatingIP := resource.(*hcloud.FloatingIP) cmd.Printf("ID:\t\t%d\n", floatingIP.ID) @@ -43,7 +42,7 @@ var DescribeCmd = base.DescribeCmd{ if floatingIP.Server != nil { cmd.Printf("Server:\n") cmd.Printf(" ID:\t%d\n", floatingIP.Server.ID) - cmd.Printf(" Name:\t%s\n", client.Server().ServerName(floatingIP.Server.ID)) + cmd.Printf(" Name:\t%s\n", s.Server().ServerName(floatingIP.Server.ID)) } else { cmd.Print("Server:\n Not assigned\n") } diff --git a/internal/cmd/floatingip/describe_test.go b/internal/cmd/floatingip/describe_test.go index 9f6e80d8..2dae550f 100644 --- a/internal/cmd/floatingip/describe_test.go +++ b/internal/cmd/floatingip/describe_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "fmt" "net" "testing" @@ -22,10 +21,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() floatingIP := &hcloud.FloatingIP{ diff --git a/internal/cmd/floatingip/disable_protection.go b/internal/cmd/floatingip/disable_protection.go index c651510a..5274f0cb 100644 --- a/internal/cmd/floatingip/disable_protection.go +++ b/internal/cmd/floatingip/disable_protection.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,9 +25,9 @@ var DisableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := client.FloatingIP().Get(ctx, idOrName) + floatingIP, _, err := s.FloatingIP().Get(s, idOrName) if err != nil { return err } @@ -41,6 +40,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, floatingIP, false, opts) + return changeProtection(s, cmd, floatingIP, false, opts) }, } diff --git a/internal/cmd/floatingip/disable_protection_test.go b/internal/cmd/floatingip/disable_protection_test.go index 42579853..82a7ee25 100644 --- a/internal/cmd/floatingip/disable_protection_test.go +++ b/internal/cmd/floatingip/disable_protection_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDisableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DisableProtectionCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DisableProtectionCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/floatingip/enable_protection.go b/internal/cmd/floatingip/enable_protection.go index db259e1b..484ccc60 100644 --- a/internal/cmd/floatingip/enable_protection.go +++ b/internal/cmd/floatingip/enable_protection.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "fmt" "strings" @@ -34,19 +33,19 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.FloatingIPChan return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, floatingIP *hcloud.FloatingIP, enable bool, opts hcloud.FloatingIPChangeProtectionOpts) error { if opts.Delete == nil { return nil } - action, _, err := client.FloatingIP().ChangeProtection(ctx, floatingIP, opts) + action, _, err := s.FloatingIP().ChangeProtection(s, floatingIP, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -72,10 +71,10 @@ var EnableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := client.FloatingIP().Get(ctx, idOrName) + floatingIP, _, err := s.FloatingIP().Get(s, idOrName) if err != nil { return err } @@ -88,6 +87,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, floatingIP, true, opts) + return changeProtection(s, cmd, floatingIP, true, opts) }, } diff --git a/internal/cmd/floatingip/enable_protection_test.go b/internal/cmd/floatingip/enable_protection_test.go index 6feef007..c0de258b 100644 --- a/internal/cmd/floatingip/enable_protection_test.go +++ b/internal/cmd/floatingip/enable_protection_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestEnableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := EnableProtectionCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := EnableProtectionCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/floatingip/floatingip.go b/internal/cmd/floatingip/floatingip.go index 464c38d0..1d0dd0b0 100644 --- a/internal/cmd/floatingip/floatingip.go +++ b/internal/cmd/floatingip/floatingip.go @@ -3,11 +3,10 @@ package floatingip import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "floating-ip", Short: "Manage Floating IPs", @@ -16,18 +15,18 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - UpdateCmd.CobraCommand(cli.Context, client, cli), - ListCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - AssignCmd.CobraCommand(cli.Context, client, cli, cli), - UnassignCmd.CobraCommand(cli.Context, client, cli, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), - SetRDNSCmd.CobraCommand(cli.Context, client, cli, cli), + UpdateCmd.CobraCommand(s), + ListCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + AssignCmd.CobraCommand(s), + UnassignCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), + SetRDNSCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/floatingip/labels.go b/internal/cmd/floatingip/labels.go index 824e5ac5..6526ab68 100644 --- a/internal/cmd/floatingip/labels.go +++ b/internal/cmd/floatingip/labels.go @@ -1,11 +1,11 @@ package floatingip import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from an Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.FloatingIP().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - floatingIP, _, err := client.FloatingIP().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + floatingIP, _, err := s.FloatingIP().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return floatingIP.Labels, floatingIP.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.FloatingIPUpdateOpts{ Labels: labels, } - _, _, err := client.FloatingIP().Update(ctx, &hcloud.FloatingIP{ID: id}, opts) + _, _, err := s.FloatingIP().Update(s, &hcloud.FloatingIP{ID: id}, opts) return err }, } diff --git a/internal/cmd/floatingip/labels_test.go b/internal/cmd/floatingip/labels_test.go index 2c82af5c..1fc1a980 100644 --- a/internal/cmd/floatingip/labels_test.go +++ b/internal/cmd/floatingip/labels_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/floatingip/list.go b/internal/cmd/floatingip/list.go index cff80f37..9ec312c5 100644 --- a/internal/cmd/floatingip/list.go +++ b/internal/cmd/floatingip/list.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "fmt" "strings" "time" @@ -12,6 +11,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -21,12 +21,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "floating_ips", DefaultColumns: []string{"id", "type", "name", "description", "ip", "home", "server", "dns", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.FloatingIPListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - floatingIPs, err := client.FloatingIP().AllWithOpts(ctx, opts) + floatingIPs, err := s.FloatingIP().AllWithOpts(s, opts) var resources []interface{} for _, n := range floatingIPs { diff --git a/internal/cmd/floatingip/list_test.go b/internal/cmd/floatingip/list_test.go index cc78513f..723a2d2a 100644 --- a/internal/cmd/floatingip/list_test.go +++ b/internal/cmd/floatingip/list_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "net" "testing" "time" @@ -19,7 +18,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/floatingip/set_rdns.go b/internal/cmd/floatingip/set_rdns.go index 1cd1cf6e..d910621d 100644 --- a/internal/cmd/floatingip/set_rdns.go +++ b/internal/cmd/floatingip/set_rdns.go @@ -1,13 +1,13 @@ package floatingip import ( - "context" "net" "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var SetRDNSCmd = base.SetRdnsCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Change reverse DNS of a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.FloatingIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.FloatingIP().Get(s, idOrName) }, GetDefaultIP: func(resource interface{}) net.IP { floatingIP := resource.(*hcloud.FloatingIP) diff --git a/internal/cmd/floatingip/set_rdns_test.go b/internal/cmd/floatingip/set_rdns_test.go index 10e76632..7ffec61b 100644 --- a/internal/cmd/floatingip/set_rdns_test.go +++ b/internal/cmd/floatingip/set_rdns_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestSetRDNS(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := SetRDNSCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := SetRDNSCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() floatingIP := &hcloud.FloatingIP{ diff --git a/internal/cmd/floatingip/unassign.go b/internal/cmd/floatingip/unassign.go index cffc0816..a3972dd5 100644 --- a/internal/cmd/floatingip/unassign.go +++ b/internal/cmd/floatingip/unassign.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var UnassignCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := client.FloatingIP().Get(ctx, idOrName) + floatingIP, _, err := s.FloatingIP().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var UnassignCmd = base.Cmd{ return fmt.Errorf("Floating IP not found: %v", idOrName) } - action, _, err := client.FloatingIP().Unassign(ctx, floatingIP) + action, _, err := s.FloatingIP().Unassign(s, floatingIP) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/floatingip/unassign_test.go b/internal/cmd/floatingip/unassign_test.go index feef4e78..d3f07355 100644 --- a/internal/cmd/floatingip/unassign_test.go +++ b/internal/cmd/floatingip/unassign_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestUnassign(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UnassignCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := UnassignCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/floatingip/update.go b/internal/cmd/floatingip/update.go index 94078395..56a0b2f9 100644 --- a/internal/cmd/floatingip/update.go +++ b/internal/cmd/floatingip/update.go @@ -1,13 +1,12 @@ package floatingip import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,20 +14,20 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Floating IP", ShortDescription: "Update a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.FloatingIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.FloatingIP().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Floating IP name") cmd.Flags().String("description", "", "Floating IP description") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { floatingIP := resource.(*hcloud.FloatingIP) updOpts := hcloud.FloatingIPUpdateOpts{ Name: flags["name"].String(), Description: flags["description"].String(), } - _, _, err := client.FloatingIP().Update(ctx, floatingIP, updOpts) + _, _, err := s.FloatingIP().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/floatingip/update_test.go b/internal/cmd/floatingip/update_test.go index df5a480f..9f7c3eea 100644 --- a/internal/cmd/floatingip/update_test.go +++ b/internal/cmd/floatingip/update_test.go @@ -1,7 +1,6 @@ package floatingip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). @@ -41,10 +37,7 @@ func TestUpdateDescription(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.FloatingIPClient.EXPECT(). diff --git a/internal/cmd/image/delete.go b/internal/cmd/image/delete.go index 3a19e52f..55f79ab9 100644 --- a/internal/cmd/image/delete.go +++ b/internal/cmd/image/delete.go @@ -1,8 +1,6 @@ package image import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "image", ShortDescription: "Delete an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Image().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Image().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { image := resource.(*hcloud.Image) - if _, err := client.Image().Delete(ctx, image); err != nil { + if _, err := s.Image().Delete(s, image); err != nil { return err } return nil diff --git a/internal/cmd/image/delete_test.go b/internal/cmd/image/delete_test.go index 533d9dc8..62af9a30 100644 --- a/internal/cmd/image/delete_test.go +++ b/internal/cmd/image/delete_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() image := &hcloud.Image{ diff --git a/internal/cmd/image/describe.go b/internal/cmd/image/describe.go index 49554517..a2142b6b 100644 --- a/internal/cmd/image/describe.go +++ b/internal/cmd/image/describe.go @@ -1,7 +1,6 @@ package image import ( - "context" "fmt" "os" @@ -12,6 +11,7 @@ import ( "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" ) @@ -26,7 +26,7 @@ var DescribeCmd = base.DescribeCmd{ }, NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { arch, err := cmd.Flags().GetString("architecture") if err != nil { return nil, nil, err @@ -34,13 +34,13 @@ var DescribeCmd = base.DescribeCmd{ if !cmd.Flags().Changed("architecture") { _, _ = fmt.Fprintln(os.Stderr, "INFO: This command only returns x86 images by default. Explicitly set the --architecture=x86|arm flag to hide this message.") } - img, _, err := client.Image().GetForArchitecture(ctx, idOrName, hcloud.Architecture(arch)) + img, _, err := s.Image().GetForArchitecture(s, idOrName, hcloud.Architecture(arch)) if err != nil { return nil, nil, err } return img, hcloud.SchemaFromImage(img), nil }, - PrintText: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(_ state.State, cmd *cobra.Command, resource interface{}) error { image := resource.(*hcloud.Image) cmd.Printf("ID:\t\t%d\n", image.ID) diff --git a/internal/cmd/image/describe_test.go b/internal/cmd/image/describe_test.go index f9586f64..2e41d9d1 100644 --- a/internal/cmd/image/describe_test.go +++ b/internal/cmd/image/describe_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "fmt" "testing" "time" @@ -21,10 +20,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() img := &hcloud.Image{ diff --git a/internal/cmd/image/disable_protection.go b/internal/cmd/image/disable_protection.go index 5df0e1b6..00073c0a 100644 --- a/internal/cmd/image/disable_protection.go +++ b/internal/cmd/image/disable_protection.go @@ -1,7 +1,6 @@ package image import ( - "context" "errors" "strconv" @@ -28,7 +27,7 @@ var DisableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { imageID, err := strconv.ParseInt(args[0], 10, 64) if err != nil { return errors.New("invalid image ID") @@ -40,6 +39,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, image, false, opts) + return changeProtection(s, cmd, image, false, opts) }, } diff --git a/internal/cmd/image/disable_protection_test.go b/internal/cmd/image/disable_protection_test.go index 79a5e080..86e93efc 100644 --- a/internal/cmd/image/disable_protection_test.go +++ b/internal/cmd/image/disable_protection_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDisableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DisableProtectionCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DisableProtectionCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). diff --git a/internal/cmd/image/enable_protection.go b/internal/cmd/image/enable_protection.go index e0b77175..0d5f9a67 100644 --- a/internal/cmd/image/enable_protection.go +++ b/internal/cmd/image/enable_protection.go @@ -1,7 +1,6 @@ package image import ( - "context" "errors" "fmt" "strconv" @@ -36,19 +35,19 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.ImageChangePro return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, image *hcloud.Image, enable bool, opts hcloud.ImageChangeProtectionOpts) error { if opts.Delete == nil { return nil } - action, _, err := client.Image().ChangeProtection(ctx, image, opts) + action, _, err := s.Image().ChangeProtection(s, image, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -75,7 +74,7 @@ var EnableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { imageID, err := strconv.ParseInt(args[0], 10, 64) if err != nil { return errors.New("invalid image ID") @@ -87,6 +86,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, image, true, opts) + return changeProtection(s, cmd, image, true, opts) }, } diff --git a/internal/cmd/image/enable_protection_test.go b/internal/cmd/image/enable_protection_test.go index aae2e0cc..d084e176 100644 --- a/internal/cmd/image/enable_protection_test.go +++ b/internal/cmd/image/enable_protection_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestEnableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := EnableProtectionCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := EnableProtectionCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). diff --git a/internal/cmd/image/image.go b/internal/cmd/image/image.go index a1c1214d..e4c0a3bd 100644 --- a/internal/cmd/image/image.go +++ b/internal/cmd/image/image.go @@ -3,11 +3,10 @@ package image import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "image", Short: "Manage images", @@ -16,14 +15,14 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), ) return cmd } diff --git a/internal/cmd/image/labels.go b/internal/cmd/image/labels.go index 7fcce19a..851c0da4 100644 --- a/internal/cmd/image/labels.go +++ b/internal/cmd/image/labels.go @@ -1,11 +1,11 @@ package image import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Image().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - image, _, err := client.Image().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + image, _, err := s.Image().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return image.Labels, image.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.ImageUpdateOpts{ Labels: labels, } - _, _, err := client.Image().Update(ctx, &hcloud.Image{ID: id}, opts) + _, _, err := s.Image().Update(s, &hcloud.Image{ID: id}, opts) return err }, } diff --git a/internal/cmd/image/labels_test.go b/internal/cmd/image/labels_test.go index e1ab7091..d7c70c70 100644 --- a/internal/cmd/image/labels_test.go +++ b/internal/cmd/image/labels_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). diff --git a/internal/cmd/image/list.go b/internal/cmd/image/list.go index 0424e4ce..7ef3dccc 100644 --- a/internal/cmd/image/list.go +++ b/internal/cmd/image/list.go @@ -1,7 +1,6 @@ package image import ( - "context" "fmt" "strings" "time" @@ -15,6 +14,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -30,7 +30,7 @@ var ListCmd = base.ListCmd{ cmd.Flags().StringSliceP("architecture", "a", []string{}, "Only show images of given architecture: x86|arm") cmd.RegisterFlagCompletionFunc("architecture", cmpl.SuggestCandidates(string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM))) }, - Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.ImageListOpts{ListOpts: listOpts, IncludeDeprecated: true} types, _ := flags.GetStringSlice("type") @@ -60,7 +60,7 @@ var ListCmd = base.ListCmd{ opts.Sort = sorts } - images, err := client.Image().AllWithOpts(ctx, opts) + images, err := s.Image().AllWithOpts(s, opts) var resources []interface{} for _, n := range images { diff --git a/internal/cmd/image/list_test.go b/internal/cmd/image/list_test.go index a5938175..b3b97f1b 100644 --- a/internal/cmd/image/list_test.go +++ b/internal/cmd/image/list_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). diff --git a/internal/cmd/image/update.go b/internal/cmd/image/update.go index 3d6391f9..1292802f 100644 --- a/internal/cmd/image/update.go +++ b/internal/cmd/image/update.go @@ -1,14 +1,13 @@ package image import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/cmpl" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -16,21 +15,21 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Image", ShortDescription: "Update an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Image().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Image().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("description", "", "Image description") cmd.Flags().String("type", "", "Image type") cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("snapshot")) }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { image := resource.(*hcloud.Image) updOpts := hcloud.ImageUpdateOpts{ Description: hcloud.String(flags["description"].String()), Type: hcloud.ImageType(flags["type"].String()), } - _, _, err := client.Image().Update(ctx, image, updOpts) + _, _, err := s.Image().Update(s, image, updOpts) if err != nil { return err } diff --git a/internal/cmd/image/update_test.go b/internal/cmd/image/update_test.go index 8885888b..24573105 100644 --- a/internal/cmd/image/update_test.go +++ b/internal/cmd/image/update_test.go @@ -1,7 +1,6 @@ package image import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateDescription(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). @@ -41,10 +37,7 @@ func TestUpdateType(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ImageClient.EXPECT(). diff --git a/internal/cmd/iso/describe.go b/internal/cmd/iso/describe.go index 591e8700..6b5c5c60 100644 --- a/internal/cmd/iso/describe.go +++ b/internal/cmd/iso/describe.go @@ -1,13 +1,12 @@ package iso import ( - "context" - "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" ) @@ -18,14 +17,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "iso", JSONKeyGetByName: "isos", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Location().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - iso, _, err := client.ISO().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + iso, _, err := s.ISO().Get(s, idOrName) if err != nil { return nil, nil, err } return iso, hcloud.SchemaFromISO(iso), nil }, - PrintText: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { iso := resource.(*hcloud.ISO) cmd.Printf("ID:\t\t%d\n", iso.ID) diff --git a/internal/cmd/iso/describe_test.go b/internal/cmd/iso/describe_test.go index 348576ca..0b5ac71e 100644 --- a/internal/cmd/iso/describe_test.go +++ b/internal/cmd/iso/describe_test.go @@ -1,7 +1,6 @@ package iso import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestDescribe(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ISOClient.EXPECT(). diff --git a/internal/cmd/iso/iso.go b/internal/cmd/iso/iso.go index f649f1e2..9f115fce 100644 --- a/internal/cmd/iso/iso.go +++ b/internal/cmd/iso/iso.go @@ -3,11 +3,10 @@ package iso import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "iso", Short: "Manage ISOs", @@ -16,8 +15,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/iso/list.go b/internal/cmd/iso/list.go index 2e960bb3..056da67f 100644 --- a/internal/cmd/iso/list.go +++ b/internal/cmd/iso/list.go @@ -1,7 +1,6 @@ package iso import ( - "context" "fmt" "slices" "strings" @@ -13,6 +12,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/cmpl" "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -31,7 +31,7 @@ var ListCmd = base.ListCmd{ cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("public", "private")) }, - Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.ISOListOpts{ListOpts: listOpts} types, _ := flags.GetStringSlice("type") @@ -65,7 +65,7 @@ var ListCmd = base.ListCmd{ opts.Sort = sorts } - isos, err := client.ISO().AllWithOpts(ctx, opts) + isos, err := s.ISO().AllWithOpts(s, opts) var resources []interface{} for _, iso := range isos { diff --git a/internal/cmd/iso/list_test.go b/internal/cmd/iso/list_test.go index 69fd43f5..fa0c400d 100644 --- a/internal/cmd/iso/list_test.go +++ b/internal/cmd/iso/list_test.go @@ -1,7 +1,6 @@ package iso import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ISOClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/add_service.go b/internal/cmd/loadbalancer/add_service.go index c73a2798..772b85be 100644 --- a/internal/cmd/loadbalancer/add_service.go +++ b/internal/cmd/loadbalancer/add_service.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -38,7 +37,7 @@ var AddServiceCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { protocol, _ := cmd.Flags().GetString("protocol") listenPort, _ := cmd.Flags().GetInt("listen-port") destinationPort, _ := cmd.Flags().GetInt("destination-port") @@ -83,7 +82,7 @@ var AddServiceCmd = base.Cmd{ httpCookieLifetime, _ := cmd.Flags().GetDuration("http-cookie-lifetime") httpRedirect, _ := cmd.Flags().GetBool("http-redirect-http") - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -118,11 +117,11 @@ var AddServiceCmd = base.Cmd{ opts.HTTP.Certificates = append(opts.HTTP.Certificates, &hcloud.Certificate{ID: certificateID}) } } - action, _, err := client.LoadBalancer().AddService(ctx, loadBalancer, opts) + action, _, err := s.LoadBalancer().AddService(s, loadBalancer, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Service was added to Load Balancer %d\n", loadBalancer.ID) diff --git a/internal/cmd/loadbalancer/add_service_test.go b/internal/cmd/loadbalancer/add_service_test.go index 8e1d4462..4f281ce8 100644 --- a/internal/cmd/loadbalancer/add_service_test.go +++ b/internal/cmd/loadbalancer/add_service_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestAddService(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AddServiceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AddServiceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/add_target.go b/internal/cmd/loadbalancer/add_target.go index 9f34a1a1..cf0aed66 100644 --- a/internal/cmd/loadbalancer/add_target.go +++ b/internal/cmd/loadbalancer/add_target.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "net" @@ -35,7 +34,7 @@ var AddTargetCmd = base.Cmd{ cmd.Flags().String("ip", "", "Use the passed IP address as target") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { var ( action *hcloud.Action loadBalancer *hcloud.LoadBalancer @@ -51,7 +50,7 @@ var AddTargetCmd = base.Cmd{ if !util.ExactlyOneSet(serverIDOrName, labelSelector, ipAddr) { return fmt.Errorf("--server, --label-selector, and --ip are mutually exclusive") } - if loadBalancer, _, err = client.LoadBalancer().Get(ctx, idOrName); err != nil { + if loadBalancer, _, err = s.LoadBalancer().Get(s, idOrName); err != nil { return err } if loadBalancer == nil { @@ -60,14 +59,14 @@ var AddTargetCmd = base.Cmd{ switch { case serverIDOrName != "": - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return err } if server == nil { return fmt.Errorf("server not found: %s", serverIDOrName) } - action, _, err = client.LoadBalancer().AddServerTarget(ctx, loadBalancer, hcloud.LoadBalancerAddServerTargetOpts{ + action, _, err = s.LoadBalancer().AddServerTarget(s, loadBalancer, hcloud.LoadBalancerAddServerTargetOpts{ Server: server, UsePrivateIP: hcloud.Bool(usePrivateIP), }) @@ -75,7 +74,7 @@ var AddTargetCmd = base.Cmd{ return err } case labelSelector != "": - action, _, err = client.LoadBalancer().AddLabelSelectorTarget(ctx, loadBalancer, hcloud.LoadBalancerAddLabelSelectorTargetOpts{ + action, _, err = s.LoadBalancer().AddLabelSelectorTarget(s, loadBalancer, hcloud.LoadBalancerAddLabelSelectorTargetOpts{ Selector: labelSelector, UsePrivateIP: hcloud.Bool(usePrivateIP), }) @@ -87,7 +86,7 @@ var AddTargetCmd = base.Cmd{ if ip == nil { return fmt.Errorf("invalid ip provided") } - action, _, err = client.LoadBalancer().AddIPTarget(ctx, loadBalancer, hcloud.LoadBalancerAddIPTargetOpts{ + action, _, err = s.LoadBalancer().AddIPTarget(s, loadBalancer, hcloud.LoadBalancerAddIPTargetOpts{ IP: ip, }) if err != nil { @@ -97,7 +96,7 @@ var AddTargetCmd = base.Cmd{ return fmt.Errorf("specify one of --server, --label-selector, or --ip") } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Target added to Load Balancer %d\n", loadBalancer.ID) diff --git a/internal/cmd/loadbalancer/add_target_test.go b/internal/cmd/loadbalancer/add_target_test.go index bdf4cd38..50b69d92 100644 --- a/internal/cmd/loadbalancer/add_target_test.go +++ b/internal/cmd/loadbalancer/add_target_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestAddTargetServer(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AddTargetCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AddTargetCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). @@ -51,11 +46,7 @@ func TestAddTargetLabelSelector(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AddTargetCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AddTargetCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). @@ -83,11 +74,7 @@ func TestAddTargetIP(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AddTargetCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AddTargetCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/attach_to_network.go b/internal/cmd/loadbalancer/attach_to_network.go index 1471892b..c400defc 100644 --- a/internal/cmd/loadbalancer/attach_to_network.go +++ b/internal/cmd/loadbalancer/attach_to_network.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -32,9 +31,9 @@ var AttachToNetworkCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -43,7 +42,7 @@ var AttachToNetworkCmd = base.Cmd{ } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := client.Network().Get(ctx, networkIDOrName) + network, _, err := s.Network().Get(s, networkIDOrName) if err != nil { return err } @@ -57,13 +56,13 @@ var AttachToNetworkCmd = base.Cmd{ Network: network, IP: ip, } - action, _, err := client.LoadBalancer().AttachToNetwork(ctx, loadBalancer, opts) + action, _, err := s.LoadBalancer().AttachToNetwork(s, loadBalancer, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/loadbalancer/attach_to_network_test.go b/internal/cmd/loadbalancer/attach_to_network_test.go index d776da3d..e9a7a90f 100644 --- a/internal/cmd/loadbalancer/attach_to_network_test.go +++ b/internal/cmd/loadbalancer/attach_to_network_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestAttachToNetwork(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AttachToNetworkCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := AttachToNetworkCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/change_algorithm.go b/internal/cmd/loadbalancer/change_algorithm.go index e1ef85df..89223024 100644 --- a/internal/cmd/loadbalancer/change_algorithm.go +++ b/internal/cmd/loadbalancer/change_algorithm.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -33,10 +32,10 @@ var ChangeAlgorithmCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] algorithm, _ := cmd.Flags().GetString("algorithm-type") - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -44,11 +43,11 @@ var ChangeAlgorithmCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - action, _, err := client.LoadBalancer().ChangeAlgorithm(ctx, loadBalancer, hcloud.LoadBalancerChangeAlgorithmOpts{Type: hcloud.LoadBalancerAlgorithmType(algorithm)}) + action, _, err := s.LoadBalancer().ChangeAlgorithm(s, loadBalancer, hcloud.LoadBalancerChangeAlgorithmOpts{Type: hcloud.LoadBalancerAlgorithmType(algorithm)}) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Algorithm for Load Balancer %d was changed\n", loadBalancer.ID) diff --git a/internal/cmd/loadbalancer/change_algorithm_test.go b/internal/cmd/loadbalancer/change_algorithm_test.go index 88813f7d..4ba64ca5 100644 --- a/internal/cmd/loadbalancer/change_algorithm_test.go +++ b/internal/cmd/loadbalancer/change_algorithm_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestChangeAlgorithm(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ChangeAlgorithmCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ChangeAlgorithmCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/change_type.go b/internal/cmd/loadbalancer/change_type.go index 3cc17bf8..b32b8fa9 100644 --- a/internal/cmd/loadbalancer/change_type.go +++ b/internal/cmd/loadbalancer/change_type.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -27,9 +26,9 @@ var ChangeTypeCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -38,7 +37,7 @@ var ChangeTypeCmd = base.Cmd{ } loadBalancerTypeIDOrName := args[1] - loadBalancerType, _, err := client.LoadBalancerType().Get(ctx, loadBalancerTypeIDOrName) + loadBalancerType, _, err := s.LoadBalancerType().Get(s, loadBalancerTypeIDOrName) if err != nil { return err } @@ -49,12 +48,12 @@ var ChangeTypeCmd = base.Cmd{ opts := hcloud.LoadBalancerChangeTypeOpts{ LoadBalancerType: loadBalancerType, } - action, _, err := client.LoadBalancer().ChangeType(ctx, loadBalancer, opts) + action, _, err := s.LoadBalancer().ChangeType(s, loadBalancer, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/loadbalancer/change_type_test.go b/internal/cmd/loadbalancer/change_type_test.go index 65b94bde..a0c8f550 100644 --- a/internal/cmd/loadbalancer/change_type_test.go +++ b/internal/cmd/loadbalancer/change_type_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestChangeType(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ChangeTypeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ChangeTypeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() lbType := &hcloud.LoadBalancerType{ID: 321, Name: "lb21"} diff --git a/internal/cmd/loadbalancer/create.go b/internal/cmd/loadbalancer/create.go index 086a08e4..924f981a 100644 --- a/internal/cmd/loadbalancer/create.go +++ b/internal/cmd/loadbalancer/create.go @@ -1,8 +1,6 @@ package loadbalancer import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -48,7 +46,7 @@ var CreateCmd = base.CreateCmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { name, _ := cmd.Flags().GetString("name") serverType, _ := cmd.Flags().GetString("type") algorithmType, _ := cmd.Flags().GetString("algorithm-type") @@ -78,28 +76,28 @@ var CreateCmd = base.CreateCmd{ if location != "" { createOpts.Location = &hcloud.Location{Name: location} } - result, _, err := client.LoadBalancer().Create(ctx, createOpts) + result, _, err := s.LoadBalancer().Create(s, createOpts) if err != nil { return nil, nil, err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } - loadBalancer, _, err := client.LoadBalancer().GetByID(ctx, result.LoadBalancer.ID) + loadBalancer, _, err := s.LoadBalancer().GetByID(s, result.LoadBalancer.ID) if err != nil { return nil, nil, err } cmd.Printf("Load Balancer %d created\n", loadBalancer.ID) - if err := changeProtection(ctx, client, waiter, cmd, loadBalancer, true, protectionOpts); err != nil { + if err := changeProtection(s, cmd, loadBalancer, true, protectionOpts); err != nil { return nil, nil, err } return loadBalancer, util.Wrap("load_balancer", hcloud.SchemaFromLoadBalancer(loadBalancer)), nil }, - PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) { + PrintResource: func(_ state.State, cmd *cobra.Command, resource any) { loadBalancer := resource.(*hcloud.LoadBalancer) cmd.Printf("IPv4: %s\n", loadBalancer.PublicNet.IPv4.IP.String()) cmd.Printf("IPv6: %s\n", loadBalancer.PublicNet.IPv6.IP.String()) diff --git a/internal/cmd/loadbalancer/create_test.go b/internal/cmd/loadbalancer/create_test.go index 4494088d..91c424f2 100644 --- a/internal/cmd/loadbalancer/create_test.go +++ b/internal/cmd/loadbalancer/create_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" _ "embed" "net" "testing" @@ -21,11 +20,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). @@ -69,11 +64,7 @@ func TestCreateJSON(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() lb := &hcloud.LoadBalancer{ @@ -123,11 +114,7 @@ func TestCreateProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() loadBalancer := &hcloud.LoadBalancer{ diff --git a/internal/cmd/loadbalancer/delete.go b/internal/cmd/loadbalancer/delete.go index cdf886fc..6f665739 100644 --- a/internal/cmd/loadbalancer/delete.go +++ b/internal/cmd/loadbalancer/delete.go @@ -1,8 +1,6 @@ package loadbalancer import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Load Balancer", ShortDescription: "Delete a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.LoadBalancer().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.LoadBalancer().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { loadBalancer := resource.(*hcloud.LoadBalancer) - if _, err := client.LoadBalancer().Delete(ctx, loadBalancer); err != nil { + if _, err := s.LoadBalancer().Delete(s, loadBalancer); err != nil { return err } return nil diff --git a/internal/cmd/loadbalancer/delete_service.go b/internal/cmd/loadbalancer/delete_service.go index d70fd99b..5cd15965 100644 --- a/internal/cmd/loadbalancer/delete_service.go +++ b/internal/cmd/loadbalancer/delete_service.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -27,17 +26,17 @@ var DeleteServiceCmd = base.Cmd{ cmd.MarkFlagRequired("listen-port") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { listenPort, _ := cmd.Flags().GetInt("listen-port") idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } if loadBalancer == nil { return fmt.Errorf("Load Balancer not found: %s", idOrName) } - _, _, err = client.LoadBalancer().DeleteService(ctx, loadBalancer, listenPort) + _, _, err = s.LoadBalancer().DeleteService(s, loadBalancer, listenPort) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/delete_service_test.go b/internal/cmd/loadbalancer/delete_service_test.go index bb264fe4..9f075a9e 100644 --- a/internal/cmd/loadbalancer/delete_service_test.go +++ b/internal/cmd/loadbalancer/delete_service_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDeleteService(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteServiceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteServiceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/delete_test.go b/internal/cmd/loadbalancer/delete_test.go index b63c9f91..3088652f 100644 --- a/internal/cmd/loadbalancer/delete_test.go +++ b/internal/cmd/loadbalancer/delete_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() loadBalancer := &hcloud.LoadBalancer{ diff --git a/internal/cmd/loadbalancer/describe.go b/internal/cmd/loadbalancer/describe.go index d488dd21..ecd30312 100644 --- a/internal/cmd/loadbalancer/describe.go +++ b/internal/cmd/loadbalancer/describe.go @@ -1,14 +1,13 @@ package loadbalancer import ( - "context" - humanize "github.com/dustin/go-humanize" "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" ) @@ -19,8 +18,8 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "load_balancer", JSONKeyGetByName: "load_balancers", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - lb, _, err := client.LoadBalancer().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + lb, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -29,7 +28,7 @@ var DescribeCmd = base.DescribeCmd{ AdditionalFlags: func(cmd *cobra.Command) { cmd.Flags().Bool("expand-targets", false, "Expand all label_selector targets") }, - PrintText: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { withLabelSelectorTargets, _ := cmd.Flags().GetBool("expand-targets") loadBalancer := resource.(*hcloud.LoadBalancer) cmd.Printf("ID:\t\t\t\t%d\n", loadBalancer.ID) @@ -46,7 +45,7 @@ var DescribeCmd = base.DescribeCmd{ if len(loadBalancer.PrivateNet) > 0 { for _, n := range loadBalancer.PrivateNet { cmd.Printf(" - ID:\t\t\t%d\n", n.Network.ID) - cmd.Printf(" Name:\t\t%s\n", client.Network().Name(n.Network.ID)) + cmd.Printf(" Name:\t\t%s\n", s.Network().Name(n.Network.ID)) cmd.Printf(" IP:\t\t\t%s\n", n.IP.String()) } } else { @@ -111,7 +110,7 @@ var DescribeCmd = base.DescribeCmd{ case hcloud.LoadBalancerTargetTypeServer: cmd.Printf(" Server:\n") cmd.Printf(" ID:\t\t\t%d\n", target.Server.Server.ID) - cmd.Printf(" Name:\t\t\t%s\n", client.Server().ServerName(target.Server.Server.ID)) + cmd.Printf(" Name:\t\t\t%s\n", s.Server().ServerName(target.Server.Server.ID)) cmd.Printf(" Use Private IP:\t\t%s\n", util.YesNo(target.UsePrivateIP)) cmd.Printf(" Status:\n") for _, healthStatus := range target.HealthStatus { diff --git a/internal/cmd/loadbalancer/describe_test.go b/internal/cmd/loadbalancer/describe_test.go index 8e75aabb..d4ac947e 100644 --- a/internal/cmd/loadbalancer/describe_test.go +++ b/internal/cmd/loadbalancer/describe_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "net" "testing" @@ -22,10 +21,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() lb := &hcloud.LoadBalancer{ diff --git a/internal/cmd/loadbalancer/detach_from_network.go b/internal/cmd/loadbalancer/detach_from_network.go index 2588dc25..1249c12e 100644 --- a/internal/cmd/loadbalancer/detach_from_network.go +++ b/internal/cmd/loadbalancer/detach_from_network.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -28,9 +27,9 @@ var DetachFromNetworkCmd = base.Cmd{ cmd.MarkFlagRequired("network") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -38,7 +37,7 @@ var DetachFromNetworkCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := client.Network().Get(ctx, networkIDOrName) + network, _, err := s.Network().Get(s, networkIDOrName) if err != nil { return err } @@ -49,12 +48,12 @@ var DetachFromNetworkCmd = base.Cmd{ opts := hcloud.LoadBalancerDetachFromNetworkOpts{ Network: network, } - action, _, err := client.LoadBalancer().DetachFromNetwork(ctx, loadBalancer, opts) + action, _, err := s.LoadBalancer().DetachFromNetwork(s, loadBalancer, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/loadbalancer/detach_from_network_test.go b/internal/cmd/loadbalancer/detach_from_network_test.go index 07f5f173..48483168 100644 --- a/internal/cmd/loadbalancer/detach_from_network_test.go +++ b/internal/cmd/loadbalancer/detach_from_network_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDetachFromNetwork(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DetachFromNetworkCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DetachFromNetworkCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/disable_protection.go b/internal/cmd/loadbalancer/disable_protection.go index 0aa61bee..5de5c68a 100644 --- a/internal/cmd/loadbalancer/disable_protection.go +++ b/internal/cmd/loadbalancer/disable_protection.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,9 +25,9 @@ var DisableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -41,6 +40,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, loadBalancer, false, opts) + return changeProtection(s, cmd, loadBalancer, false, opts) }, } diff --git a/internal/cmd/loadbalancer/disable_protection_test.go b/internal/cmd/loadbalancer/disable_protection_test.go index 4a7598f9..b2591aa9 100644 --- a/internal/cmd/loadbalancer/disable_protection_test.go +++ b/internal/cmd/loadbalancer/disable_protection_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDisableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DisableProtectionCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DisableProtectionCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/disable_public_interface.go b/internal/cmd/loadbalancer/disable_public_interface.go index bbdb2402..ff4d87de 100644 --- a/internal/cmd/loadbalancer/disable_public_interface.go +++ b/internal/cmd/loadbalancer/disable_public_interface.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var DisablePublicInterfaceCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var DisablePublicInterfaceCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - action, _, err := client.LoadBalancer().DisablePublicInterface(ctx, loadBalancer) + action, _, err := s.LoadBalancer().DisablePublicInterface(s, loadBalancer) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/loadbalancer/disable_public_interface_test.go b/internal/cmd/loadbalancer/disable_public_interface_test.go index 4d854a47..22bcd699 100644 --- a/internal/cmd/loadbalancer/disable_public_interface_test.go +++ b/internal/cmd/loadbalancer/disable_public_interface_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDisablePublicInterface(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DisablePublicInterfaceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DisablePublicInterfaceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/enable_protection.go b/internal/cmd/loadbalancer/enable_protection.go index 1fcb1c79..a436315c 100644 --- a/internal/cmd/loadbalancer/enable_protection.go +++ b/internal/cmd/loadbalancer/enable_protection.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "strings" @@ -34,19 +33,19 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.LoadBalancerCh return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, loadBalancer *hcloud.LoadBalancer, enable bool, opts hcloud.LoadBalancerChangeProtectionOpts) error { if opts.Delete == nil { return nil } - action, _, err := client.LoadBalancer().ChangeProtection(ctx, loadBalancer, opts) + action, _, err := s.LoadBalancer().ChangeProtection(s, loadBalancer, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -72,9 +71,9 @@ var EnableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -87,6 +86,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, loadBalancer, true, opts) + return changeProtection(s, cmd, loadBalancer, true, opts) }, } diff --git a/internal/cmd/loadbalancer/enable_protection_test.go b/internal/cmd/loadbalancer/enable_protection_test.go index 9ad85f85..62e0dc6b 100644 --- a/internal/cmd/loadbalancer/enable_protection_test.go +++ b/internal/cmd/loadbalancer/enable_protection_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestEnableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := EnableProtectionCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := EnableProtectionCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/enable_public_interface.go b/internal/cmd/loadbalancer/enable_public_interface.go index 714eb049..580051ed 100644 --- a/internal/cmd/loadbalancer/enable_public_interface.go +++ b/internal/cmd/loadbalancer/enable_public_interface.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var EnablePublicInterfaceCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var EnablePublicInterfaceCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - action, _, err := client.LoadBalancer().EnablePublicInterface(ctx, loadBalancer) + action, _, err := s.LoadBalancer().EnablePublicInterface(s, loadBalancer) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/loadbalancer/enable_public_interface_test.go b/internal/cmd/loadbalancer/enable_public_interface_test.go index 071cc4b7..254aa352 100644 --- a/internal/cmd/loadbalancer/enable_public_interface_test.go +++ b/internal/cmd/loadbalancer/enable_public_interface_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestEnablePublicInterface(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := EnablePublicInterfaceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := EnablePublicInterfaceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/labels.go b/internal/cmd/loadbalancer/labels.go index d57fb71e..10b6e9ca 100644 --- a/internal/cmd/loadbalancer/labels.go +++ b/internal/cmd/loadbalancer/labels.go @@ -1,11 +1,11 @@ package loadbalancer import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.LoadBalancer().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return loadBalancer.Labels, loadBalancer.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.LoadBalancerUpdateOpts{ Labels: labels, } - _, _, err := client.LoadBalancer().Update(ctx, &hcloud.LoadBalancer{ID: id}, opts) + _, _, err := s.LoadBalancer().Update(s, &hcloud.LoadBalancer{ID: id}, opts) return err }, } diff --git a/internal/cmd/loadbalancer/labels_test.go b/internal/cmd/loadbalancer/labels_test.go index d38b3990..9ea8dc80 100644 --- a/internal/cmd/loadbalancer/labels_test.go +++ b/internal/cmd/loadbalancer/labels_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/list.go b/internal/cmd/loadbalancer/list.go index bdf2e9ba..058f2a4e 100644 --- a/internal/cmd/loadbalancer/list.go +++ b/internal/cmd/loadbalancer/list.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "strings" "time" @@ -11,6 +10,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -19,12 +19,12 @@ var ListCmd = base.ListCmd{ ResourceNamePlural: "Load Balancer", JSONKeyGetByName: "load_balancers", DefaultColumns: []string{"id", "name", "health", "ipv4", "ipv6", "type", "location", "network_zone", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.LoadBalancerListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - loadBalancers, err := client.LoadBalancer().AllWithOpts(ctx, opts) + loadBalancers, err := s.LoadBalancer().AllWithOpts(s, opts) var resources []interface{} for _, r := range loadBalancers { diff --git a/internal/cmd/loadbalancer/load_balancer.go b/internal/cmd/loadbalancer/load_balancer.go index d4d8bf57..2ed342a5 100644 --- a/internal/cmd/loadbalancer/load_balancer.go +++ b/internal/cmd/loadbalancer/load_balancer.go @@ -3,11 +3,10 @@ package loadbalancer import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer", Short: "Manage Load Balancers", @@ -17,28 +16,28 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), - AddTargetCmd.CobraCommand(cli.Context, client, cli, cli), - RemoveTargetCmd.CobraCommand(cli.Context, client, cli, cli), - ChangeAlgorithmCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateServiceCmd.CobraCommand(cli.Context, client, cli, cli), - DeleteServiceCmd.CobraCommand(cli.Context, client, cli, cli), - AddServiceCmd.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - AttachToNetworkCmd.CobraCommand(cli.Context, client, cli, cli), - DetachFromNetworkCmd.CobraCommand(cli.Context, client, cli, cli), - EnablePublicInterfaceCmd.CobraCommand(cli.Context, client, cli, cli), - DisablePublicInterfaceCmd.CobraCommand(cli.Context, client, cli, cli), - ChangeTypeCmd.CobraCommand(cli.Context, client, cli, cli), - MetricsCmd.CobraCommand(cli.Context, client, cli, cli), - SetRDNSCmd.CobraCommand(cli.Context, client, cli, cli), + CreateCmd.CobraCommand(s), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), + AddTargetCmd.CobraCommand(s), + RemoveTargetCmd.CobraCommand(s), + ChangeAlgorithmCmd.CobraCommand(s), + UpdateServiceCmd.CobraCommand(s), + DeleteServiceCmd.CobraCommand(s), + AddServiceCmd.CobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + AttachToNetworkCmd.CobraCommand(s), + DetachFromNetworkCmd.CobraCommand(s), + EnablePublicInterfaceCmd.CobraCommand(s), + DisablePublicInterfaceCmd.CobraCommand(s), + ChangeTypeCmd.CobraCommand(s), + MetricsCmd.CobraCommand(s), + SetRDNSCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/loadbalancer/metrics.go b/internal/cmd/loadbalancer/metrics.go index 52225fd4..f6986d02 100644 --- a/internal/cmd/loadbalancer/metrics.go +++ b/internal/cmd/loadbalancer/metrics.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "encoding/json" "fmt" "slices" @@ -49,11 +48,11 @@ var MetricsCmd = base.Cmd{ output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML()) return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] - LoadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + LoadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -89,7 +88,7 @@ var MetricsCmd = base.Cmd{ } } - m, resp, err := client.LoadBalancer().GetMetrics(ctx, LoadBalancer, hcloud.LoadBalancerGetMetricsOpts{ + m, resp, err := s.LoadBalancer().GetMetrics(s, LoadBalancer, hcloud.LoadBalancerGetMetricsOpts{ Types: metricTypes, Start: startTime, End: endTime, diff --git a/internal/cmd/loadbalancer/metrics_test.go b/internal/cmd/loadbalancer/metrics_test.go index b6d86a60..21c5c880 100644 --- a/internal/cmd/loadbalancer/metrics_test.go +++ b/internal/cmd/loadbalancer/metrics_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "time" @@ -16,11 +15,7 @@ func TestMetrics(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := MetricsCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := MetricsCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() start := time.Date(2022, 11, 1, 0, 0, 0, 0, time.UTC) diff --git a/internal/cmd/loadbalancer/remove_target.go b/internal/cmd/loadbalancer/remove_target.go index 8bf29182..34a4dcdb 100644 --- a/internal/cmd/loadbalancer/remove_target.go +++ b/internal/cmd/loadbalancer/remove_target.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "net" @@ -35,7 +34,7 @@ var RemoveTargetCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { var ( action *hcloud.Action loadBalancer *hcloud.LoadBalancer @@ -48,7 +47,7 @@ var RemoveTargetCmd = base.Cmd{ idOrName := args[0] - loadBalancer, _, err = client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err = s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -61,19 +60,19 @@ var RemoveTargetCmd = base.Cmd{ } switch { case serverIDOrName != "": - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return err } if server == nil { return fmt.Errorf("server not found: %s", serverIDOrName) } - action, _, err = client.LoadBalancer().RemoveServerTarget(ctx, loadBalancer, server) + action, _, err = s.LoadBalancer().RemoveServerTarget(s, loadBalancer, server) if err != nil { return err } case labelSelector != "": - action, _, err = client.LoadBalancer().RemoveLabelSelectorTarget(ctx, loadBalancer, labelSelector) + action, _, err = s.LoadBalancer().RemoveLabelSelectorTarget(s, loadBalancer, labelSelector) if err != nil { return err } @@ -82,14 +81,14 @@ var RemoveTargetCmd = base.Cmd{ if ip == nil { return fmt.Errorf("invalid ip provided") } - if action, _, err = client.LoadBalancer().RemoveIPTarget(ctx, loadBalancer, ip); err != nil { + if action, _, err = s.LoadBalancer().RemoveIPTarget(s, loadBalancer, ip); err != nil { return err } default: return fmt.Errorf("specify one of --server, --label-selector, or --ip") } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Target removed from Load Balancer %d\n", loadBalancer.ID) diff --git a/internal/cmd/loadbalancer/remove_target_test.go b/internal/cmd/loadbalancer/remove_target_test.go index 932655e4..2a832650 100644 --- a/internal/cmd/loadbalancer/remove_target_test.go +++ b/internal/cmd/loadbalancer/remove_target_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestRemoveTargetServer(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := RemoveTargetCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := RemoveTargetCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). @@ -48,11 +43,7 @@ func TestRemoveTargetLabelSelector(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := RemoveTargetCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := RemoveTargetCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). @@ -77,11 +68,7 @@ func TestRemoveTargetIP(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := RemoveTargetCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := RemoveTargetCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/set_rdns.go b/internal/cmd/loadbalancer/set_rdns.go index 7c032fc2..69ab5ff7 100644 --- a/internal/cmd/loadbalancer/set_rdns.go +++ b/internal/cmd/loadbalancer/set_rdns.go @@ -1,13 +1,13 @@ package loadbalancer import ( - "context" "net" "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var SetRDNSCmd = base.SetRdnsCmd{ ResourceNameSingular: "Load Balancer", ShortDescription: "Change reverse DNS of a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.LoadBalancer().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.LoadBalancer().Get(s, idOrName) }, GetDefaultIP: func(resource interface{}) net.IP { loadBalancer := resource.(*hcloud.LoadBalancer) diff --git a/internal/cmd/loadbalancer/set_rdns_test.go b/internal/cmd/loadbalancer/set_rdns_test.go index ffece31a..3fbf1761 100644 --- a/internal/cmd/loadbalancer/set_rdns_test.go +++ b/internal/cmd/loadbalancer/set_rdns_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "net" "testing" @@ -16,11 +15,7 @@ func TestSetRDNS(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := SetRDNSCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := SetRDNSCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() loadBalancer := &hcloud.LoadBalancer{ diff --git a/internal/cmd/loadbalancer/update.go b/internal/cmd/loadbalancer/update.go index 0137843f..9ce08e03 100644 --- a/internal/cmd/loadbalancer/update.go +++ b/internal/cmd/loadbalancer/update.go @@ -1,13 +1,12 @@ package loadbalancer import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Load Balancer", ShortDescription: "Update a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.LoadBalancer().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.LoadBalancer().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "LoadBalancer name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { floatingIP := resource.(*hcloud.LoadBalancer) updOpts := hcloud.LoadBalancerUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.LoadBalancer().Update(ctx, floatingIP, updOpts) + _, _, err := s.LoadBalancer().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/update_service.go b/internal/cmd/loadbalancer/update_service.go index 63b94615..b5b622fd 100644 --- a/internal/cmd/loadbalancer/update_service.go +++ b/internal/cmd/loadbalancer/update_service.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "fmt" "time" @@ -53,11 +52,11 @@ var UpdateServiceCmd = base.Cmd{ cmd.Flags().Bool("health-check-http-tls", false, "Determine if the health check should verify if the target answers with a valid TLS certificate") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] listenPort, _ := cmd.Flags().GetInt("listen-port") - loadBalancer, _, err := client.LoadBalancer().Get(ctx, idOrName) + loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -160,11 +159,11 @@ var UpdateServiceCmd = base.Cmd{ } } - action, _, err := client.LoadBalancer().UpdateService(ctx, loadBalancer, listenPort, opts) + action, _, err := s.LoadBalancer().UpdateService(s, loadBalancer, listenPort, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Service %d on Load Balancer %d was updated\n", listenPort, loadBalancer.ID) diff --git a/internal/cmd/loadbalancer/update_service_test.go b/internal/cmd/loadbalancer/update_service_test.go index 2f90cb8e..544491c4 100644 --- a/internal/cmd/loadbalancer/update_service_test.go +++ b/internal/cmd/loadbalancer/update_service_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "time" @@ -16,11 +15,7 @@ func TestUpdateService(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateServiceCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := UpdateServiceCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancer/update_test.go b/internal/cmd/loadbalancer/update_test.go index 12a2bcf8..cd1d6b2b 100644 --- a/internal/cmd/loadbalancer/update_test.go +++ b/internal/cmd/loadbalancer/update_test.go @@ -1,7 +1,6 @@ package loadbalancer import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerClient.EXPECT(). diff --git a/internal/cmd/loadbalancertype/describe.go b/internal/cmd/loadbalancertype/describe.go index 30a126a9..1123d8cf 100644 --- a/internal/cmd/loadbalancertype/describe.go +++ b/internal/cmd/loadbalancertype/describe.go @@ -1,12 +1,11 @@ package loadbalancertype import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -16,14 +15,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "load_balancer_type", JSONKeyGetByName: "load_balancer_types", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancerType().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - lbt, _, err := client.LoadBalancerType().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + lbt, _, err := s.LoadBalancerType().Get(s, idOrName) if err != nil { return nil, nil, err } return lbt, hcloud.SchemaFromLoadBalancerType(lbt), nil }, - PrintText: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(_ state.State, cmd *cobra.Command, resource interface{}) error { loadBalancerType := resource.(*hcloud.LoadBalancerType) cmd.Printf("ID:\t\t\t\t%d\n", loadBalancerType.ID) diff --git a/internal/cmd/loadbalancertype/describe_test.go b/internal/cmd/loadbalancertype/describe_test.go index 6b150480..460bfff0 100644 --- a/internal/cmd/loadbalancertype/describe_test.go +++ b/internal/cmd/loadbalancertype/describe_test.go @@ -1,7 +1,6 @@ package loadbalancertype import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestDescribe(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerTypeClient.EXPECT(). diff --git a/internal/cmd/loadbalancertype/list.go b/internal/cmd/loadbalancertype/list.go index 405c8ebb..a6ccc6b7 100644 --- a/internal/cmd/loadbalancertype/list.go +++ b/internal/cmd/loadbalancertype/list.go @@ -1,13 +1,12 @@ package loadbalancertype import ( - "context" - "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -18,12 +17,12 @@ var ListCmd = base.ListCmd{ DefaultColumns: []string{"id", "name", "description", "max_services", "max_connections", "max_targets"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.LoadBalancerTypeListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - loadBalancerTypes, err := client.LoadBalancerType().AllWithOpts(ctx, opts) + loadBalancerTypes, err := s.LoadBalancerType().AllWithOpts(s, opts) var resources []interface{} for _, r := range loadBalancerTypes { diff --git a/internal/cmd/loadbalancertype/list_test.go b/internal/cmd/loadbalancertype/list_test.go index 892b1b6e..5c8c7c66 100644 --- a/internal/cmd/loadbalancertype/list_test.go +++ b/internal/cmd/loadbalancertype/list_test.go @@ -1,7 +1,6 @@ package loadbalancertype import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LoadBalancerTypeClient.EXPECT(). diff --git a/internal/cmd/loadbalancertype/load_balancer_type.go b/internal/cmd/loadbalancertype/load_balancer_type.go index 223544f1..a83884ba 100644 --- a/internal/cmd/loadbalancertype/load_balancer_type.go +++ b/internal/cmd/loadbalancertype/load_balancer_type.go @@ -3,11 +3,10 @@ package loadbalancertype import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer-type", Short: "Manage Load Balancer types", @@ -16,8 +15,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - DescribeCmd.CobraCommand(cli.Context, client, cli), - ListCmd.CobraCommand(cli.Context, client, cli), + DescribeCmd.CobraCommand(s), + ListCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/location/describe.go b/internal/cmd/location/describe.go index 2cff3899..643620f2 100644 --- a/internal/cmd/location/describe.go +++ b/internal/cmd/location/describe.go @@ -1,12 +1,11 @@ package location import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -17,14 +16,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "location", JSONKeyGetByName: "locations", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Location().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - l, _, err := client.Location().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + l, _, err := s.Location().Get(s, idOrName) if err != nil { return nil, nil, err } return l, hcloud.SchemaFromLocation(l), nil }, - PrintText: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { location := resource.(*hcloud.Location) cmd.Printf("ID:\t\t%d\n", location.ID) diff --git a/internal/cmd/location/describe_test.go b/internal/cmd/location/describe_test.go index 28f3c4dc..1bd22b1f 100644 --- a/internal/cmd/location/describe_test.go +++ b/internal/cmd/location/describe_test.go @@ -1,7 +1,6 @@ package location_test import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -16,10 +15,7 @@ func TestDescribe(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := location.DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := location.DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LocationClient.EXPECT(). diff --git a/internal/cmd/location/list.go b/internal/cmd/location/list.go index 6f1fd3e2..4f34c695 100644 --- a/internal/cmd/location/list.go +++ b/internal/cmd/location/list.go @@ -1,13 +1,12 @@ package location import ( - "context" - "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -17,12 +16,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "locations", DefaultColumns: []string{"id", "name", "description", "network_zone", "country", "city"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.LocationListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - locations, err := client.Location().AllWithOpts(ctx, opts) + locations, err := s.Location().AllWithOpts(s, opts) var resources []interface{} for _, n := range locations { diff --git a/internal/cmd/location/list_test.go b/internal/cmd/location/list_test.go index bbc01a7a..117f21b0 100644 --- a/internal/cmd/location/list_test.go +++ b/internal/cmd/location/list_test.go @@ -1,7 +1,6 @@ package location import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.LocationClient.EXPECT(). diff --git a/internal/cmd/location/location.go b/internal/cmd/location/location.go index 5df72111..826ed45e 100644 --- a/internal/cmd/location/location.go +++ b/internal/cmd/location/location.go @@ -3,11 +3,10 @@ package location import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "location", Short: "Manage locations", @@ -16,8 +15,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/network/add_route.go b/internal/cmd/network/add_route.go index 5a02e056..cc2bf236 100644 --- a/internal/cmd/network/add_route.go +++ b/internal/cmd/network/add_route.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "net" @@ -33,12 +32,12 @@ var AddRouteCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -52,11 +51,11 @@ var AddRouteCmd = base.Cmd{ Destination: &destination, }, } - action, _, err := client.Network().AddRoute(ctx, network, opts) + action, _, err := s.Network().AddRoute(s, network, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Route added to network %d\n", network.ID) diff --git a/internal/cmd/network/add_subnet.go b/internal/cmd/network/add_subnet.go index 35c475fa..dc057ead 100644 --- a/internal/cmd/network/add_subnet.go +++ b/internal/cmd/network/add_subnet.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "net" @@ -38,14 +37,14 @@ var AddSubnetCmd = base.Cmd{ cmd.Flags().Int64("vswitch-id", 0, "ID of the vSwitch") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { subnetType, _ := cmd.Flags().GetString("type") networkZone, _ := cmd.Flags().GetString("network-zone") ipRange, _ := cmd.Flags().GetIPNet("ip-range") vSwitchID, _ := cmd.Flags().GetInt64("vswitch-id") idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -67,11 +66,11 @@ var AddSubnetCmd = base.Cmd{ opts := hcloud.NetworkAddSubnetOpts{ Subnet: subnet, } - action, _, err := client.Network().AddSubnet(ctx, network, opts) + action, _, err := s.Network().AddSubnet(s, network, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Subnet added to network %d\n", network.ID) diff --git a/internal/cmd/network/change_ip_range.go b/internal/cmd/network/change_ip_range.go index ab5c9309..3db9ac7d 100644 --- a/internal/cmd/network/change_ip_range.go +++ b/internal/cmd/network/change_ip_range.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "net" @@ -30,9 +29,9 @@ var ChangeIPRangeCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -45,12 +44,12 @@ var ChangeIPRangeCmd = base.Cmd{ IPRange: &ipRange, } - action, _, err := client.Network().ChangeIPRange(ctx, network, opts) + action, _, err := s.Network().ChangeIPRange(s, network, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("IP range of network %d changed\n", network.ID) diff --git a/internal/cmd/network/create.go b/internal/cmd/network/create.go index c22346f5..40f825d8 100644 --- a/internal/cmd/network/create.go +++ b/internal/cmd/network/create.go @@ -1,7 +1,6 @@ package network import ( - "context" "net" "github.com/spf13/cobra" @@ -36,7 +35,7 @@ var CreateCmd = base.CreateCmd{ cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete")) return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { name, _ := cmd.Flags().GetString("name") ipRange, _ := cmd.Flags().GetIPNet("ip-range") labels, _ := cmd.Flags().GetStringToString("label") @@ -55,14 +54,14 @@ var CreateCmd = base.CreateCmd{ ExposeRoutesToVSwitch: exposeRoutesToVSwitch, } - network, _, err := client.Network().Create(ctx, createOpts) + network, _, err := s.Network().Create(s, createOpts) if err != nil { return nil, nil, err } cmd.Printf("Network %d created\n", network.ID) - if err := changeProtection(ctx, client, waiter, cmd, network, true, protectionOpts); err != nil { + if err := changeProtection(s, cmd, network, true, protectionOpts); err != nil { return nil, nil, err } diff --git a/internal/cmd/network/create_test.go b/internal/cmd/network/create_test.go index 014a8268..e714fc66 100644 --- a/internal/cmd/network/create_test.go +++ b/internal/cmd/network/create_test.go @@ -1,7 +1,6 @@ package network import ( - "context" _ "embed" "net" "testing" @@ -21,11 +20,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() _, ipRange, _ := net.ParseCIDR("10.0.0.0/24") @@ -55,11 +50,7 @@ func TestCreateJSON(t *testing.T) { time.Local = time.UTC - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() _, ipRange, _ := net.ParseCIDR("10.0.0.0/24") @@ -93,11 +84,7 @@ func TestCreateProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() _, ipRange, _ := net.ParseCIDR("10.0.0.0/24") diff --git a/internal/cmd/network/delete.go b/internal/cmd/network/delete.go index 40bb61b5..e565219d 100644 --- a/internal/cmd/network/delete.go +++ b/internal/cmd/network/delete.go @@ -1,8 +1,6 @@ package network import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Network", ShortDescription: "Delete a network", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Network().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Network().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { network := resource.(*hcloud.Network) - if _, err := client.Network().Delete(ctx, network); err != nil { + if _, err := s.Network().Delete(s, network); err != nil { return err } return nil diff --git a/internal/cmd/network/delete_test.go b/internal/cmd/network/delete_test.go index 55c0892d..bd0bf729 100644 --- a/internal/cmd/network/delete_test.go +++ b/internal/cmd/network/delete_test.go @@ -1,7 +1,6 @@ package network import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() network := &hcloud.Network{ diff --git a/internal/cmd/network/describe.go b/internal/cmd/network/describe.go index cf35774a..0e075ec1 100644 --- a/internal/cmd/network/describe.go +++ b/internal/cmd/network/describe.go @@ -1,14 +1,13 @@ package network import ( - "context" - humanize "github.com/dustin/go-humanize" "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" ) @@ -19,14 +18,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "network", JSONKeyGetByName: "networks", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - n, _, err := client.Network().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + n, _, err := s.Network().Get(s, idOrName) if err != nil { return nil, nil, err } return n, hcloud.SchemaFromNetwork(n), nil }, - PrintText: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { network := resource.(*hcloud.Network) cmd.Printf("ID:\t\t%d\n", network.ID) diff --git a/internal/cmd/network/describe_test.go b/internal/cmd/network/describe_test.go index 3169c260..f6e3627a 100644 --- a/internal/cmd/network/describe_test.go +++ b/internal/cmd/network/describe_test.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "net" "testing" @@ -22,10 +21,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() network := &hcloud.Network{ diff --git a/internal/cmd/network/disable_protection.go b/internal/cmd/network/disable_protection.go index 5bbe4904..ffe3f2ae 100644 --- a/internal/cmd/network/disable_protection.go +++ b/internal/cmd/network/disable_protection.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,9 +25,9 @@ var DisableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -41,6 +40,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, network, false, opts) + return changeProtection(s, cmd, network, false, opts) }, } diff --git a/internal/cmd/network/enable_protection.go b/internal/cmd/network/enable_protection.go index 4bb35af2..67c4b53e 100644 --- a/internal/cmd/network/enable_protection.go +++ b/internal/cmd/network/enable_protection.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "strings" @@ -34,19 +33,19 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.NetworkChangeP return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, network *hcloud.Network, enable bool, opts hcloud.NetworkChangeProtectionOpts) error { if opts.Delete == nil { return nil } - action, _, err := client.Network().ChangeProtection(ctx, network, opts) + action, _, err := s.Network().ChangeProtection(s, network, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -72,9 +71,9 @@ var EnableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -87,6 +86,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, network, true, opts) + return changeProtection(s, cmd, network, true, opts) }, } diff --git a/internal/cmd/network/expose-routes-to-vswitch.go b/internal/cmd/network/expose-routes-to-vswitch.go index 16e36468..e9635322 100644 --- a/internal/cmd/network/expose-routes-to-vswitch.go +++ b/internal/cmd/network/expose-routes-to-vswitch.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "github.com/spf13/cobra" @@ -29,9 +28,9 @@ var ExposeRoutesToVSwitchCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -44,7 +43,7 @@ var ExposeRoutesToVSwitchCmd = base.Cmd{ ExposeRoutesToVSwitch: hcloud.Ptr(!disable), } - _, _, err = client.Network().Update(ctx, network, opts) + _, _, err = s.Network().Update(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/labels.go b/internal/cmd/network/labels.go index 0331d420..f7f7d493 100644 --- a/internal/cmd/network/labels.go +++ b/internal/cmd/network/labels.go @@ -1,11 +1,11 @@ package network import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a Network", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Network().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - network, _, err := client.Network().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + network, _, err := s.Network().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return network.Labels, network.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.NetworkUpdateOpts{ Labels: labels, } - _, _, err := client.Network().Update(ctx, &hcloud.Network{ID: id}, opts) + _, _, err := s.Network().Update(s, &hcloud.Network{ID: id}, opts) return err }, } diff --git a/internal/cmd/network/labels_test.go b/internal/cmd/network/labels_test.go index c714e411..52058b22 100644 --- a/internal/cmd/network/labels_test.go +++ b/internal/cmd/network/labels_test.go @@ -1,7 +1,6 @@ package network import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.NetworkClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.NetworkClient.EXPECT(). diff --git a/internal/cmd/network/list.go b/internal/cmd/network/list.go index dc6d41e2..ee01ea6d 100644 --- a/internal/cmd/network/list.go +++ b/internal/cmd/network/list.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "strings" "time" @@ -12,6 +11,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -21,12 +21,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "networks", DefaultColumns: []string{"id", "name", "ip_range", "servers", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.NetworkListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - networks, err := client.Network().AllWithOpts(ctx, opts) + networks, err := s.Network().AllWithOpts(s, opts) var resources []interface{} for _, n := range networks { diff --git a/internal/cmd/network/list_test.go b/internal/cmd/network/list_test.go index 64426a0c..b68368e7 100644 --- a/internal/cmd/network/list_test.go +++ b/internal/cmd/network/list_test.go @@ -1,7 +1,6 @@ package network_test import ( - "context" "net" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := network.ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := network.ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.NetworkClient.EXPECT(). diff --git a/internal/cmd/network/network.go b/internal/cmd/network/network.go index 6a2cb149..014b227c 100644 --- a/internal/cmd/network/network.go +++ b/internal/cmd/network/network.go @@ -3,11 +3,10 @@ package network import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "network", Short: "Manage networks", @@ -16,21 +15,21 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - ChangeIPRangeCmd.CobraCommand(cli.Context, client, cli, cli), - AddRouteCmd.CobraCommand(cli.Context, client, cli, cli), - RemoveRouteCmd.CobraCommand(cli.Context, client, cli, cli), - AddSubnetCmd.CobraCommand(cli.Context, client, cli, cli), - RemoveSubnetCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - ExposeRoutesToVSwitchCmd.CobraCommand(cli.Context, client, cli, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + ChangeIPRangeCmd.CobraCommand(s), + AddRouteCmd.CobraCommand(s), + RemoveRouteCmd.CobraCommand(s), + AddSubnetCmd.CobraCommand(s), + RemoveSubnetCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + ExposeRoutesToVSwitchCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/network/remove_route.go b/internal/cmd/network/remove_route.go index 0fc2b61a..282bd38b 100644 --- a/internal/cmd/network/remove_route.go +++ b/internal/cmd/network/remove_route.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "net" @@ -33,11 +32,11 @@ var RemoveRouteCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -51,11 +50,11 @@ var RemoveRouteCmd = base.Cmd{ Destination: &destination, }, } - action, _, err := client.Network().DeleteRoute(ctx, network, opts) + action, _, err := s.Network().DeleteRoute(s, network, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Route removed from network %d\n", network.ID) diff --git a/internal/cmd/network/remove_subnet.go b/internal/cmd/network/remove_subnet.go index 4548b3f2..2110716a 100644 --- a/internal/cmd/network/remove_subnet.go +++ b/internal/cmd/network/remove_subnet.go @@ -1,7 +1,6 @@ package network import ( - "context" "fmt" "net" @@ -28,10 +27,10 @@ var RemoveSubnetCmd = base.Cmd{ cmd.MarkFlagRequired("ip-range") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { ipRange, _ := cmd.Flags().GetIPNet("ip-range") idOrName := args[0] - network, _, err := client.Network().Get(ctx, idOrName) + network, _, err := s.Network().Get(s, idOrName) if err != nil { return err } @@ -44,11 +43,11 @@ var RemoveSubnetCmd = base.Cmd{ IPRange: &ipRange, }, } - action, _, err := client.Network().DeleteSubnet(ctx, network, opts) + action, _, err := s.Network().DeleteSubnet(s, network, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } cmd.Printf("Subnet %s removed from network %d\n", ipRange.String(), network.ID) diff --git a/internal/cmd/network/update.go b/internal/cmd/network/update.go index 3f5015cb..01533110 100644 --- a/internal/cmd/network/update.go +++ b/internal/cmd/network/update.go @@ -1,13 +1,12 @@ package network import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Network", ShortDescription: "Update a Network.\n\nTo enable or disable exposing routes to the vSwitch connection you can use the subcommand \"hcloud network expose-routes-to-vswitch\".", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Network().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Network().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Network name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { floatingIP := resource.(*hcloud.Network) updOpts := hcloud.NetworkUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.Network().Update(ctx, floatingIP, updOpts) + _, _, err := s.Network().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/network/update_test.go b/internal/cmd/network/update_test.go index f1b3fb31..48e8f5ce 100644 --- a/internal/cmd/network/update_test.go +++ b/internal/cmd/network/update_test.go @@ -1,7 +1,6 @@ package network import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.NetworkClient.EXPECT(). diff --git a/internal/cmd/placementgroup/create.go b/internal/cmd/placementgroup/create.go index 2acf753f..3af6e5ce 100644 --- a/internal/cmd/placementgroup/create.go +++ b/internal/cmd/placementgroup/create.go @@ -1,8 +1,6 @@ package placementgroup import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -27,7 +25,7 @@ var CreateCmd = base.CreateCmd{ cmd.MarkFlagRequired("type") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { name, _ := cmd.Flags().GetString("name") labels, _ := cmd.Flags().GetStringToString("label") placementGroupType, _ := cmd.Flags().GetString("type") @@ -38,13 +36,13 @@ var CreateCmd = base.CreateCmd{ Type: hcloud.PlacementGroupType(placementGroupType), } - result, _, err := client.PlacementGroup().Create(ctx, opts) + result, _, err := s.PlacementGroup().Create(s, opts) if err != nil { return nil, nil, err } if result.Action != nil { - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } } diff --git a/internal/cmd/placementgroup/create_test.go b/internal/cmd/placementgroup/create_test.go index df210415..69e494a2 100644 --- a/internal/cmd/placementgroup/create_test.go +++ b/internal/cmd/placementgroup/create_test.go @@ -1,7 +1,6 @@ package placementgroup_test import ( - "context" _ "embed" "testing" "time" @@ -21,11 +20,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := placementgroup.CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := placementgroup.CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() opts := hcloud.PlacementGroupCreateOpts{ @@ -64,11 +59,7 @@ func TestCreateJSON(t *testing.T) { time.Local = time.UTC - cmd := placementgroup.CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := placementgroup.CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() opts := hcloud.PlacementGroupCreateOpts{ diff --git a/internal/cmd/placementgroup/delete.go b/internal/cmd/placementgroup/delete.go index 9f92b137..77734369 100644 --- a/internal/cmd/placementgroup/delete.go +++ b/internal/cmd/placementgroup/delete.go @@ -1,8 +1,6 @@ package placementgroup import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "placement group", ShortDescription: "Delete a placement group", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.PlacementGroup().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.PlacementGroup().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { placementGroup := resource.(*hcloud.PlacementGroup) - if _, err := client.PlacementGroup().Delete(ctx, placementGroup); err != nil { + if _, err := s.PlacementGroup().Delete(s, placementGroup); err != nil { return err } return nil diff --git a/internal/cmd/placementgroup/delete_test.go b/internal/cmd/placementgroup/delete_test.go index db493e77..1699e3aa 100644 --- a/internal/cmd/placementgroup/delete_test.go +++ b/internal/cmd/placementgroup/delete_test.go @@ -1,7 +1,6 @@ package placementgroup_test import ( - "context" "testing" "time" @@ -17,11 +16,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := placementgroup.DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := placementgroup.DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() placementGroup := hcloud.PlacementGroup{ diff --git a/internal/cmd/placementgroup/describe.go b/internal/cmd/placementgroup/describe.go index 4f581cb6..e4b6239a 100644 --- a/internal/cmd/placementgroup/describe.go +++ b/internal/cmd/placementgroup/describe.go @@ -1,14 +1,13 @@ package placementgroup import ( - "context" - "github.com/dustin/go-humanize" "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" ) @@ -18,14 +17,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "placement_group", JSONKeyGetByName: "placement_groups", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - pg, _, err := client.PlacementGroup().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + pg, _, err := s.PlacementGroup().Get(s, idOrName) if err != nil { return nil, nil, err } return pg, hcloud.SchemaFromPlacementGroup(pg), nil }, - PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { placementGroup := resource.(*hcloud.PlacementGroup) cmd.Printf("ID:\t\t%d\n", placementGroup.ID) @@ -44,7 +43,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Print("Servers:\n") for _, serverID := range placementGroup.Servers { cmd.Printf(" - Server ID:\t\t%d\n", serverID) - cmd.Printf(" Server Name:\t%s\n", client.Server().ServerName(serverID)) + cmd.Printf(" Server Name:\t%s\n", s.Server().ServerName(serverID)) } cmd.Printf("Type:\t\t%s\n", placementGroup.Type) diff --git a/internal/cmd/placementgroup/describe_test.go b/internal/cmd/placementgroup/describe_test.go index 816b9a8a..a9b07022 100644 --- a/internal/cmd/placementgroup/describe_test.go +++ b/internal/cmd/placementgroup/describe_test.go @@ -1,7 +1,6 @@ package placementgroup_test import ( - "context" "fmt" "testing" "time" @@ -22,10 +21,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := placementgroup.DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := placementgroup.DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() placementGroup := hcloud.PlacementGroup{ diff --git a/internal/cmd/placementgroup/labels.go b/internal/cmd/placementgroup/labels.go index 6b5be9dc..919f2eda 100644 --- a/internal/cmd/placementgroup/labels.go +++ b/internal/cmd/placementgroup/labels.go @@ -1,11 +1,11 @@ package placementgroup import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a placement group", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PlacementGroup().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - placementGroup, _, err := client.PlacementGroup().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + placementGroup, _, err := s.PlacementGroup().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return placementGroup.Labels, placementGroup.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.PlacementGroupUpdateOpts{ Labels: labels, } - _, _, err := client.PlacementGroup().Update(ctx, &hcloud.PlacementGroup{ID: id}, opts) + _, _, err := s.PlacementGroup().Update(s, &hcloud.PlacementGroup{ID: id}, opts) return err }, } diff --git a/internal/cmd/placementgroup/labels_test.go b/internal/cmd/placementgroup/labels_test.go index 076605d9..83c3f53f 100644 --- a/internal/cmd/placementgroup/labels_test.go +++ b/internal/cmd/placementgroup/labels_test.go @@ -1,7 +1,6 @@ package placementgroup import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PlacementGroupClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PlacementGroupClient.EXPECT(). diff --git a/internal/cmd/placementgroup/list.go b/internal/cmd/placementgroup/list.go index b364dde6..f2114cc8 100644 --- a/internal/cmd/placementgroup/list.go +++ b/internal/cmd/placementgroup/list.go @@ -1,7 +1,6 @@ package placementgroup import ( - "context" "fmt" "time" @@ -11,6 +10,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -20,12 +20,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "placement_groups", DefaultColumns: []string{"id", "name", "servers", "type", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.PlacementGroupListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - placementGroups, err := client.PlacementGroup().AllWithOpts(ctx, opts) + placementGroups, err := s.PlacementGroup().AllWithOpts(s, opts) var resources []interface{} for _, n := range placementGroups { diff --git a/internal/cmd/placementgroup/list_test.go b/internal/cmd/placementgroup/list_test.go index fa82ddad..60f22419 100644 --- a/internal/cmd/placementgroup/list_test.go +++ b/internal/cmd/placementgroup/list_test.go @@ -1,7 +1,6 @@ package placementgroup_test import ( - "context" "testing" "time" @@ -17,10 +16,7 @@ func TestList(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := placementgroup.ListCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := placementgroup.ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PlacementGroupClient.EXPECT(). diff --git a/internal/cmd/placementgroup/placementgroup.go b/internal/cmd/placementgroup/placementgroup.go index ded36331..2b739e29 100644 --- a/internal/cmd/placementgroup/placementgroup.go +++ b/internal/cmd/placementgroup/placementgroup.go @@ -3,11 +3,10 @@ package placementgroup import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "placement-group", Short: "Manage Placement Groups", @@ -16,13 +15,13 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + CreateCmd.CobraCommand(s), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), ) return cmd } diff --git a/internal/cmd/placementgroup/update.go b/internal/cmd/placementgroup/update.go index 277ffea4..515b02b6 100644 --- a/internal/cmd/placementgroup/update.go +++ b/internal/cmd/placementgroup/update.go @@ -1,13 +1,12 @@ package placementgroup import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "placement group", ShortDescription: "Update a placement group", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.PlacementGroup().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.PlacementGroup().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Placement group name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { placementGroup := resource.(*hcloud.PlacementGroup) updOpts := hcloud.PlacementGroupUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.PlacementGroup().Update(ctx, placementGroup, updOpts) + _, _, err := s.PlacementGroup().Update(s, placementGroup, updOpts) if err != nil { return err } diff --git a/internal/cmd/placementgroup/update_test.go b/internal/cmd/placementgroup/update_test.go index b050f2c3..d3a5574c 100644 --- a/internal/cmd/placementgroup/update_test.go +++ b/internal/cmd/placementgroup/update_test.go @@ -1,7 +1,6 @@ package placementgroup import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PlacementGroupClient.EXPECT(). diff --git a/internal/cmd/primaryip/assign.go b/internal/cmd/primaryip/assign.go index e9816732..6169af33 100644 --- a/internal/cmd/primaryip/assign.go +++ b/internal/cmd/primaryip/assign.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -30,9 +29,9 @@ var AssignCmd = base.Cmd{ cmd.MarkFlagRequired("server") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName) + primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -42,7 +41,7 @@ var AssignCmd = base.Cmd{ serverIDOrName, _ := cmd.Flags().GetString("server") - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return err } @@ -55,12 +54,12 @@ var AssignCmd = base.Cmd{ AssigneeID: server.ID, } - action, _, err := client.PrimaryIP().Assign(ctx, opts) + action, _, err := s.PrimaryIP().Assign(s, opts) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/primaryip/assign_test.go b/internal/cmd/primaryip/assign_test.go index acc8b649..54332f27 100644 --- a/internal/cmd/primaryip/assign_test.go +++ b/internal/cmd/primaryip/assign_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "testing" @@ -16,7 +15,7 @@ func TestAssign(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := AssignCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := AssignCmd.CobraCommand(fx.State()) action := &hcloud.Action{ID: 1} fx.ExpectEnsureToken() var ( diff --git a/internal/cmd/primaryip/changedns.go b/internal/cmd/primaryip/changedns.go index 5b744ad8..af2a38f7 100644 --- a/internal/cmd/primaryip/changedns.go +++ b/internal/cmd/primaryip/changedns.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -31,9 +30,9 @@ var ChangeDNSCmd = base.Cmd{ cmd.MarkFlagRequired("ip") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName) + primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -49,12 +48,12 @@ var ChangeDNSCmd = base.Cmd{ IP: ip, } - action, _, err := client.PrimaryIP().ChangeDNSPtr(ctx, opts) + action, _, err := s.PrimaryIP().ChangeDNSPtr(s, opts) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/primaryip/changedns_test.go b/internal/cmd/primaryip/changedns_test.go index a2099d27..8d79107c 100644 --- a/internal/cmd/primaryip/changedns_test.go +++ b/internal/cmd/primaryip/changedns_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,7 +14,7 @@ func TestChangeDNS(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ChangeDNSCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := ChangeDNSCmd.CobraCommand(fx.State()) action := &hcloud.Action{ID: 1} fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/create.go b/internal/cmd/primaryip/create.go index 61a789ce..35f68953 100644 --- a/internal/cmd/primaryip/create.go +++ b/internal/cmd/primaryip/create.go @@ -1,8 +1,6 @@ package primaryip import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -41,7 +39,7 @@ var CreateCmd = base.CreateCmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { typ, _ := cmd.Flags().GetString("type") name, _ := cmd.Flags().GetString("name") assigneeID, _ := cmd.Flags().GetInt64("assignee-id") @@ -63,13 +61,13 @@ var CreateCmd = base.CreateCmd{ createOpts.AssigneeID = &assigneeID } - result, _, err := client.PrimaryIP().Create(ctx, createOpts) + result, _, err := s.PrimaryIP().Create(s, createOpts) if err != nil { return nil, nil, err } if result.Action != nil { - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } } @@ -77,14 +75,14 @@ var CreateCmd = base.CreateCmd{ cmd.Printf("Primary IP %d created\n", result.PrimaryIP.ID) if len(protection) > 0 { - if err := changeProtection(ctx, client, waiter, cmd, result.PrimaryIP, true, protectionOpts); err != nil { + if err := changeProtection(s, cmd, result.PrimaryIP, true, protectionOpts); err != nil { return nil, nil, err } } return result.PrimaryIP, util.Wrap("primary_ip", hcloud.SchemaFromPrimaryIP(result.PrimaryIP)), nil }, - PrintResource: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource any) { + PrintResource: func(_ state.State, cmd *cobra.Command, resource any) { primaryIP := resource.(*hcloud.PrimaryIP) cmd.Printf("IP%s: %s\n", primaryIP.Type[2:], primaryIP.IP) }, diff --git a/internal/cmd/primaryip/create_test.go b/internal/cmd/primaryip/create_test.go index 505cd6b8..9512b100 100644 --- a/internal/cmd/primaryip/create_test.go +++ b/internal/cmd/primaryip/create_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" _ "embed" "net" "testing" @@ -21,7 +20,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). Create( @@ -65,11 +64,7 @@ func TestCreateJSON(t *testing.T) { time.Local = time.UTC - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/delete.go b/internal/cmd/primaryip/delete.go index aca42a98..e8102745 100644 --- a/internal/cmd/primaryip/delete.go +++ b/internal/cmd/primaryip/delete.go @@ -1,8 +1,6 @@ package primaryip import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Primary IP", ShortDescription: "Delete a Primary IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.PrimaryIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.PrimaryIP().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { primaryIP := resource.(*hcloud.PrimaryIP) - if _, err := client.PrimaryIP().Delete(ctx, primaryIP); err != nil { + if _, err := s.PrimaryIP().Delete(s, primaryIP); err != nil { return err } return nil diff --git a/internal/cmd/primaryip/delete_test.go b/internal/cmd/primaryip/delete_test.go index b948cb3a..567fa758 100644 --- a/internal/cmd/primaryip/delete_test.go +++ b/internal/cmd/primaryip/delete_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,7 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) primaryip := &hcloud.PrimaryIP{ID: 13} fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/describe.go b/internal/cmd/primaryip/describe.go index 26150063..fd689178 100644 --- a/internal/cmd/primaryip/describe.go +++ b/internal/cmd/primaryip/describe.go @@ -1,14 +1,13 @@ package primaryip import ( - "context" - "github.com/dustin/go-humanize" "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" ) @@ -18,14 +17,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "primary_ip", JSONKeyGetByName: "primary_ips", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - ip, _, err := client.PrimaryIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + ip, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return nil, nil, err } return ip, hcloud.SchemaFromPrimaryIP(ip), nil }, - PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { primaryIP := resource.(*hcloud.PrimaryIP) cmd.Printf("ID:\t\t%d\n", primaryIP.ID) diff --git a/internal/cmd/primaryip/describe_test.go b/internal/cmd/primaryip/describe_test.go index 5fe33784..4f16923c 100644 --- a/internal/cmd/primaryip/describe_test.go +++ b/internal/cmd/primaryip/describe_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "net" "testing" @@ -22,7 +21,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() primaryIP := &hcloud.PrimaryIP{ diff --git a/internal/cmd/primaryip/disable_protection.go b/internal/cmd/primaryip/disable_protection.go index 520f0b8a..de8678fb 100644 --- a/internal/cmd/primaryip/disable_protection.go +++ b/internal/cmd/primaryip/disable_protection.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -27,9 +26,9 @@ var DisableProtectionCmd = base.Cmd{ } return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName) + primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -48,6 +47,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, actionWaiter, cmd, primaryIP, false, opts) + return changeProtection(s, cmd, primaryIP, false, opts) }, } diff --git a/internal/cmd/primaryip/disable_protection_test.go b/internal/cmd/primaryip/disable_protection_test.go index adc03f30..bab9f567 100644 --- a/internal/cmd/primaryip/disable_protection_test.go +++ b/internal/cmd/primaryip/disable_protection_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,7 +14,7 @@ func TestEnable(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DisableProtectionCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := DisableProtectionCmd.CobraCommand(fx.State()) action := &hcloud.Action{ID: 1} primaryip := &hcloud.PrimaryIP{ID: 13} fx.ExpectEnsureToken() diff --git a/internal/cmd/primaryip/enable_protection.go b/internal/cmd/primaryip/enable_protection.go index 0c108de6..592a9c51 100644 --- a/internal/cmd/primaryip/enable_protection.go +++ b/internal/cmd/primaryip/enable_protection.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "strings" @@ -34,17 +33,17 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.PrimaryIPChang return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, primaryIp *hcloud.PrimaryIP, enable bool, opts hcloud.PrimaryIPChangeProtectionOpts) error { opts.ID = primaryIp.ID - action, _, err := client.PrimaryIP().ChangeProtection(ctx, opts) + action, _, err := s.PrimaryIP().ChangeProtection(s, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -71,9 +70,9 @@ var EnableProtectionCmd = base.Cmd{ } return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName) + primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -92,6 +91,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, actionWaiter, cmd, primaryIP, true, opts) + return changeProtection(s, cmd, primaryIP, true, opts) }, } diff --git a/internal/cmd/primaryip/enable_protection_test.go b/internal/cmd/primaryip/enable_protection_test.go index bd6df783..141f9811 100644 --- a/internal/cmd/primaryip/enable_protection_test.go +++ b/internal/cmd/primaryip/enable_protection_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,7 +14,7 @@ func TestEnableProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := EnableProtectionCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := EnableProtectionCmd.CobraCommand(fx.State()) action := &hcloud.Action{ID: 1} primaryip := &hcloud.PrimaryIP{ID: 13} fx.ExpectEnsureToken() diff --git a/internal/cmd/primaryip/labels.go b/internal/cmd/primaryip/labels.go index 6a4a7eaa..fb8adaec 100644 --- a/internal/cmd/primaryip/labels.go +++ b/internal/cmd/primaryip/labels.go @@ -1,11 +1,11 @@ package primaryip import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a Primary IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PrimaryIP().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return primaryIP.Labels, primaryIP.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.PrimaryIPUpdateOpts{ Labels: &labels, } - _, _, err := client.PrimaryIP().Update(ctx, &hcloud.PrimaryIP{ID: id}, opts) + _, _, err := s.PrimaryIP().Update(s, &hcloud.PrimaryIP{ID: id}, opts) return err }, } diff --git a/internal/cmd/primaryip/labels_test.go b/internal/cmd/primaryip/labels_test.go index 4d72ac84..4aa5ab77 100644 --- a/internal/cmd/primaryip/labels_test.go +++ b/internal/cmd/primaryip/labels_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/list.go b/internal/cmd/primaryip/list.go index 70159b44..55427791 100644 --- a/internal/cmd/primaryip/list.go +++ b/internal/cmd/primaryip/list.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "strings" "time" @@ -12,6 +11,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -21,12 +21,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "primary_ips", DefaultColumns: []string{"id", "type", "name", "ip", "assignee", "dns", "auto_delete", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.PrimaryIPListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - primaryips, err := client.PrimaryIP().AllWithOpts(ctx, opts) + primaryips, err := s.PrimaryIP().AllWithOpts(s, opts) var resources []interface{} for _, n := range primaryips { diff --git a/internal/cmd/primaryip/list_test.go b/internal/cmd/primaryip/list_test.go index a9e8cb89..31dde821 100644 --- a/internal/cmd/primaryip/list_test.go +++ b/internal/cmd/primaryip/list_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "net" "testing" "time" @@ -17,7 +16,7 @@ func TestList(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/primaryip.go b/internal/cmd/primaryip/primaryip.go index ed80ea16..21db646a 100644 --- a/internal/cmd/primaryip/primaryip.go +++ b/internal/cmd/primaryip/primaryip.go @@ -3,11 +3,10 @@ package primaryip import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "primary-ip", Short: "Manage Primary IPs", @@ -16,18 +15,18 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - updateCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - AssignCmd.CobraCommand(cli.Context, client, cli, cli), - UnAssignCmd.CobraCommand(cli.Context, client, cli, cli), - ChangeDNSCmd.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + updateCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + AssignCmd.CobraCommand(s), + UnAssignCmd.CobraCommand(s), + ChangeDNSCmd.CobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), ) return cmd } diff --git a/internal/cmd/primaryip/unassign.go b/internal/cmd/primaryip/unassign.go index 6c1260f9..d0a88281 100644 --- a/internal/cmd/primaryip/unassign.go +++ b/internal/cmd/primaryip/unassign.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,9 +25,9 @@ var UnAssignCmd = base.Cmd{ } return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := client.PrimaryIP().Get(ctx, idOrName) + primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -36,12 +35,12 @@ var UnAssignCmd = base.Cmd{ return fmt.Errorf("Primary IP not found: %v", idOrName) } - action, _, err := client.PrimaryIP().Unassign(ctx, primaryIP.ID) + action, _, err := s.PrimaryIP().Unassign(s, primaryIP.ID) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/primaryip/unassign_test.go b/internal/cmd/primaryip/unassign_test.go index 776ae500..8dd4feae 100644 --- a/internal/cmd/primaryip/unassign_test.go +++ b/internal/cmd/primaryip/unassign_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,7 +14,7 @@ func TestUnAssign(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UnAssignCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer, fx.ActionWaiter) + cmd := UnAssignCmd.CobraCommand(fx.State()) action := &hcloud.Action{ID: 1} fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/primaryip/update.go b/internal/cmd/primaryip/update.go index d9ed6935..94220a46 100644 --- a/internal/cmd/primaryip/update.go +++ b/internal/cmd/primaryip/update.go @@ -1,13 +1,12 @@ package primaryip import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,14 +14,14 @@ var updateCmd = base.UpdateCmd{ ResourceNameSingular: "Primary IP", ShortDescription: "Update a Primary IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.PrimaryIP().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.PrimaryIP().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Primary IP name") cmd.Flags().Bool("auto-delete", false, "Delete this Primary IP when the resource it is assigned to is deleted") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { primaryIP := resource.(*hcloud.PrimaryIP) updOpts := hcloud.PrimaryIPUpdateOpts{ Name: flags["name"].String(), @@ -33,7 +32,7 @@ var updateCmd = base.UpdateCmd{ updOpts.AutoDelete = hcloud.Ptr(autoDelete) } - _, _, err := client.PrimaryIP().Update(ctx, primaryIP, updOpts) + _, _, err := s.PrimaryIP().Update(s, primaryIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/primaryip/update_test.go b/internal/cmd/primaryip/update_test.go index b69510aa..20c1c12e 100644 --- a/internal/cmd/primaryip/update_test.go +++ b/internal/cmd/primaryip/update_test.go @@ -1,7 +1,6 @@ package primaryip import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := updateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := updateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). @@ -42,10 +38,7 @@ func TestUpdateAutoDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := updateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := updateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.PrimaryIPClient.EXPECT(). diff --git a/internal/cmd/server/add_to_placement_group.go b/internal/cmd/server/add_to_placement_group.go index 4701e486..81637865 100644 --- a/internal/cmd/server/add_to_placement_group.go +++ b/internal/cmd/server/add_to_placement_group.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -27,9 +26,9 @@ var AddToPlacementGroupCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -38,7 +37,7 @@ var AddToPlacementGroupCmd = base.Cmd{ } placementGroupIDOrName, _ := cmd.Flags().GetString("placement-group") - placementGroup, _, err := client.PlacementGroup().Get(ctx, placementGroupIDOrName) + placementGroup, _, err := s.PlacementGroup().Get(s, placementGroupIDOrName) if err != nil { return err } @@ -46,12 +45,12 @@ var AddToPlacementGroupCmd = base.Cmd{ return fmt.Errorf("placement group not found %s", placementGroupIDOrName) } - action, _, err := client.Server().AddToPlacementGroup(ctx, server, placementGroup) + action, _, err := s.Server().AddToPlacementGroup(s, server, placementGroup) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/add_to_placement_group_test.go b/internal/cmd/server/add_to_placement_group_test.go index 62073307..23f610cf 100644 --- a/internal/cmd/server/add_to_placement_group_test.go +++ b/internal/cmd/server/add_to_placement_group_test.go @@ -1,7 +1,6 @@ package server_test import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -16,11 +15,7 @@ func TestAddToPlacementGroup(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := server.AddToPlacementGroupCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := server.AddToPlacementGroupCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() var ( diff --git a/internal/cmd/server/attach_iso.go b/internal/cmd/server/attach_iso.go index 2d757b97..7dec788a 100644 --- a/internal/cmd/server/attach_iso.go +++ b/internal/cmd/server/attach_iso.go @@ -1,7 +1,6 @@ package server import ( - "context" "errors" "fmt" @@ -27,9 +26,9 @@ var AttachISOCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -38,7 +37,7 @@ var AttachISOCmd = base.Cmd{ } isoIDOrName := args[1] - iso, _, err := client.ISO().Get(ctx, isoIDOrName) + iso, _, err := s.ISO().Get(s, isoIDOrName) if err != nil { return err } @@ -52,12 +51,12 @@ var AttachISOCmd = base.Cmd{ return errors.New("failed to attach iso: iso has a different architecture than the server") } - action, _, err := client.Server().AttachISO(ctx, server, iso) + action, _, err := s.Server().AttachISO(s, server, iso) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/attach_to_network.go b/internal/cmd/server/attach_to_network.go index 4135f651..2c377cc8 100644 --- a/internal/cmd/server/attach_to_network.go +++ b/internal/cmd/server/attach_to_network.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "net" @@ -34,9 +33,9 @@ var AttachToNetworkCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -45,7 +44,7 @@ var AttachToNetworkCmd = base.Cmd{ } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := client.Network().Get(ctx, networkIDOrName) + network, _, err := s.Network().Get(s, networkIDOrName) if err != nil { return err } @@ -63,13 +62,13 @@ var AttachToNetworkCmd = base.Cmd{ for _, aliasIP := range aliasIPs { opts.AliasIPs = append(opts.AliasIPs, aliasIP) } - action, _, err := client.Server().AttachToNetwork(ctx, server, opts) + action, _, err := s.Server().AttachToNetwork(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/change_alias_ips.go b/internal/cmd/server/change_alias_ips.go index 19bc6646..6caa9410 100644 --- a/internal/cmd/server/change_alias_ips.go +++ b/internal/cmd/server/change_alias_ips.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "net" @@ -34,10 +33,10 @@ var ChangeAliasIPsCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { clear, _ := cmd.Flags().GetBool("clear") idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -46,7 +45,7 @@ var ChangeAliasIPsCmd = base.Cmd{ } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := client.Network().Get(ctx, networkIDOrName) + network, _, err := s.Network().Get(s, networkIDOrName) if err != nil { return err } @@ -66,13 +65,13 @@ var ChangeAliasIPsCmd = base.Cmd{ opts.AliasIPs = append(opts.AliasIPs, net.ParseIP(aliasIP)) } } - action, _, err := client.Server().ChangeAliasIPs(ctx, server, opts) + action, _, err := s.Server().ChangeAliasIPs(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/change_type.go b/internal/cmd/server/change_type.go index d36ccdf9..af95fc8b 100644 --- a/internal/cmd/server/change_type.go +++ b/internal/cmd/server/change_type.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -30,9 +29,9 @@ var ChangeTypeCmd = base.Cmd{ cmd.Flags().Bool("keep-disk", false, "Keep disk size of current server type. This enables downgrading the server.") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -41,7 +40,7 @@ var ChangeTypeCmd = base.Cmd{ } serverTypeIDOrName := args[1] - serverType, _, err := client.ServerType().Get(ctx, serverTypeIDOrName) + serverType, _, err := s.ServerType().Get(s, serverTypeIDOrName) if err != nil { return err } @@ -58,12 +57,12 @@ var ChangeTypeCmd = base.Cmd{ ServerType: serverType, UpgradeDisk: !keepDisk, } - action, _, err := client.Server().ChangeType(ctx, server, opts) + action, _, err := s.Server().ChangeType(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/create.go b/internal/cmd/server/create.go index 46ddc0fe..bfa8724a 100644 --- a/internal/cmd/server/create.go +++ b/internal/cmd/server/create.go @@ -2,7 +2,6 @@ package server import ( "bytes" - "context" "encoding/base64" "fmt" "io/ioutil" @@ -97,43 +96,43 @@ var CreateCmd = base.CreateCmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { - createOpts, protectionOpts, err := createOptsFromFlags(ctx, client, cmd) + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { + createOpts, protectionOpts, err := createOptsFromFlags(s, cmd) if err != nil { return nil, nil, err } - result, _, err := client.Server().Create(ctx, createOpts) + result, _, err := s.Server().Create(s, createOpts) if err != nil { return nil, nil, err } - if err := actionWaiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } - if err := actionWaiter.WaitForActions(cmd, ctx, result.NextActions); err != nil { + if err := s.WaitForActions(cmd, s, result.NextActions); err != nil { return nil, nil, err } - server, _, err := client.Server().GetByID(ctx, result.Server.ID) + server, _, err := s.Server().GetByID(s, result.Server.ID) if err != nil { return nil, nil, err } cmd.Printf("Server %d created\n", result.Server.ID) - if err := changeProtection(ctx, client, actionWaiter, cmd, server, true, protectionOpts); err != nil { + if err := changeProtection(s, cmd, server, true, protectionOpts); err != nil { return nil, nil, err } enableBackup, _ := cmd.Flags().GetBool("enable-backup") if enableBackup { - action, _, err := client.Server().EnableBackup(ctx, server, "") + action, _, err := s.Server().EnableBackup(s, server, "") if err != nil { return nil, nil, err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return nil, nil, err } @@ -144,7 +143,7 @@ var CreateCmd = base.CreateCmd{ createResultSchema{Server: hcloud.SchemaFromServer(server), RootPassword: result.RootPassword}, nil }, - PrintResource: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource any) { + PrintResource: func(s state.State, cmd *cobra.Command, resource any) { result := resource.(createResult) server := result.Server @@ -158,7 +157,7 @@ var CreateCmd = base.CreateCmd{ if len(server.PrivateNet) > 0 { var networks []string for _, network := range server.PrivateNet { - networks = append(networks, fmt.Sprintf("- %s (%s)", network.IP.String(), client.Network().Name(network.Network.ID))) + networks = append(networks, fmt.Sprintf("- %s (%s)", network.IP.String(), s.Network().Name(network.Network.ID))) } cmd.Printf("Private Networks:\n\t%s\n", strings.Join(networks, "\n")) } @@ -238,7 +237,7 @@ func buildUserData(files []string) (string, error) { } func createOptsFromFlags( - ctx context.Context, client hcapi2.Client, cmd *cobra.Command, + s state.State, cmd *cobra.Command, ) (createOpts hcloud.ServerCreateOpts, protectionOps hcloud.ServerChangeProtectionOpts, err error) { flags := cmd.Flags() name, _ := flags.GetString("name") @@ -262,7 +261,7 @@ func createOptsFromFlags( primaryIPv6IDorName, _ := flags.GetString("primary-ipv6") protection, _ := flags.GetStringSlice("enable-protection") - serverType, _, err := client.ServerType().Get(ctx, serverTypeName) + serverType, _, err := s.ServerType().Get(s, serverTypeName) if err != nil { return } @@ -276,7 +275,7 @@ func createOptsFromFlags( } // Select correct image based on server type architecture - image, _, err := client.Image().GetForArchitecture(ctx, imageIDorName, serverType.Architecture) + image, _, err := s.Image().GetForArchitecture(s, imageIDorName, serverType.Architecture) if err != nil { return } @@ -316,7 +315,7 @@ func createOptsFromFlags( } if primaryIPv4IDorName != "" { var primaryIPv4 *hcloud.PrimaryIP - primaryIPv4, _, err = client.PrimaryIP().Get(ctx, primaryIPv4IDorName) + primaryIPv4, _, err = s.PrimaryIP().Get(s, primaryIPv4IDorName) if err != nil { return } @@ -328,7 +327,7 @@ func createOptsFromFlags( } if primaryIPv6IDorName != "" { var primaryIPv6 *hcloud.PrimaryIP - primaryIPv6, _, err = client.PrimaryIP().Get(ctx, primaryIPv6IDorName) + primaryIPv6, _, err = s.PrimaryIP().Get(s, primaryIPv6IDorName) if err != nil { return } @@ -359,13 +358,13 @@ func createOptsFromFlags( for _, sshKeyIDOrName := range sshKeys { var sshKey *hcloud.SSHKey - sshKey, _, err = client.SSHKey().Get(ctx, sshKeyIDOrName) + sshKey, _, err = s.SSHKey().Get(s, sshKeyIDOrName) if err != nil { return } if sshKey == nil { - sshKey, err = getSSHKeyForFingerprint(ctx, client, sshKeyIDOrName) + sshKey, err = getSSHKeyForFingerprint(s, sshKeyIDOrName) if err != nil { return } @@ -379,7 +378,7 @@ func createOptsFromFlags( } for _, volumeIDOrName := range volumes { var volume *hcloud.Volume - volume, _, err = client.Volume().Get(ctx, volumeIDOrName) + volume, _, err = s.Volume().Get(s, volumeIDOrName) if err != nil { return } @@ -392,7 +391,7 @@ func createOptsFromFlags( } for _, networkIDOrName := range networks { var network *hcloud.Network - network, _, err = client.Network().Get(ctx, networkIDOrName) + network, _, err = s.Network().Get(s, networkIDOrName) if err != nil { return } @@ -405,7 +404,7 @@ func createOptsFromFlags( } for _, firewallIDOrName := range firewalls { var firewall *hcloud.Firewall - firewall, _, err = client.Firewall().Get(ctx, firewallIDOrName) + firewall, _, err = s.Firewall().Get(s, firewallIDOrName) if err != nil { return } @@ -425,7 +424,7 @@ func createOptsFromFlags( } if placementGroupIDorName != "" { var placementGroup *hcloud.PlacementGroup - placementGroup, _, err = client.PlacementGroup().Get(ctx, placementGroupIDorName) + placementGroup, _, err = s.PlacementGroup().Get(s, placementGroupIDorName) if err != nil { return } @@ -441,7 +440,7 @@ func createOptsFromFlags( } func getSSHKeyForFingerprint( - ctx context.Context, client hcapi2.Client, file string, + s state.State, file string, ) (sshKey *hcloud.SSHKey, err error) { var ( fileContent []byte @@ -460,7 +459,7 @@ func getSSHKeyForFingerprint( err = fmt.Errorf("lookup SSH key by fingerprint: %v", err) return } - sshKey, _, err = client.SSHKey().GetByFingerprint(ctx, ssh.FingerprintLegacyMD5(publicKey)) + sshKey, _, err = s.SSHKey().GetByFingerprint(s, ssh.FingerprintLegacyMD5(publicKey)) if err != nil { err = fmt.Errorf("lookup SSH key by fingerprint: %v", err) return diff --git a/internal/cmd/server/create_image.go b/internal/cmd/server/create_image.go index 75348384..ac4f5489 100644 --- a/internal/cmd/server/create_image.go +++ b/internal/cmd/server/create_image.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -30,9 +29,9 @@ var CreateImageCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -56,12 +55,12 @@ var CreateImageCmd = base.Cmd{ Description: hcloud.Ptr(description), Labels: labels, } - result, _, err := client.Server().CreateImage(ctx, server, opts) + result, _, err := s.Server().CreateImage(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return err } diff --git a/internal/cmd/server/create_test.go b/internal/cmd/server/create_test.go index 90d4eb9e..601aa33d 100644 --- a/internal/cmd/server/create_test.go +++ b/internal/cmd/server/create_test.go @@ -22,12 +22,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := server.CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter, - ) + cmd := server.CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() @@ -83,12 +78,7 @@ func TestCreateJSON(t *testing.T) { time.Local = time.UTC - cmd := server.CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter, - ) + cmd := server.CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() srv := &hcloud.Server{ @@ -182,12 +172,7 @@ func TestCreateProtectionBackup(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := server.CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter, - ) + cmd := server.CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() diff --git a/internal/cmd/server/delete.go b/internal/cmd/server/delete.go index a1009d47..38d9c463 100644 --- a/internal/cmd/server/delete.go +++ b/internal/cmd/server/delete.go @@ -1,8 +1,6 @@ package server import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,17 +13,17 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Server", ShortDescription: "Delete a server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Server().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Server().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { server := resource.(*hcloud.Server) - result, _, err := client.Server().DeleteWithResult(ctx, server) + result, _, err := s.Server().DeleteWithResult(s, server) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return err } diff --git a/internal/cmd/server/delete_test.go b/internal/cmd/server/delete_test.go index 0ad138a1..83bfca3e 100644 --- a/internal/cmd/server/delete_test.go +++ b/internal/cmd/server/delete_test.go @@ -1,7 +1,6 @@ package server import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() srv := &hcloud.Server{ diff --git a/internal/cmd/server/describe.go b/internal/cmd/server/describe.go index 9e760f5e..9ebd3cbe 100644 --- a/internal/cmd/server/describe.go +++ b/internal/cmd/server/describe.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" humanize "github.com/dustin/go-humanize" @@ -10,6 +9,7 @@ import ( "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" ) @@ -19,14 +19,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "server", JSONKeyGetByName: "servers", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - srv, _, err := client.Server().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + srv, _, err := s.Server().Get(s, idOrName) if err != nil { return nil, nil, err } return srv, hcloud.SchemaFromServer(srv), nil }, - PrintText: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { server := resource.(*hcloud.Server) cmd.Printf("ID:\t\t%d\n", server.ID) @@ -67,7 +67,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf(" Floating IPs:\n") if len(server.PublicNet.FloatingIPs) > 0 { for _, f := range server.PublicNet.FloatingIPs { - floatingIP, _, err := client.FloatingIP().GetByID(ctx, f.ID) + floatingIP, _, err := s.FloatingIP().GetByID(s, f.ID) if err != nil { return fmt.Errorf("error fetching Floating IP: %v", err) } @@ -82,7 +82,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf("Private Net:\n") if len(server.PrivateNet) > 0 { for _, n := range server.PrivateNet { - network, _, err := client.Network().GetByID(ctx, n.Network.ID) + network, _, err := s.Network().GetByID(s, n.Network.ID) if err != nil { return fmt.Errorf("error fetching network: %v", err) } @@ -106,7 +106,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf("Volumes:\n") if len(server.Volumes) > 0 { for _, v := range server.Volumes { - volume, _, err := client.Volume().GetByID(ctx, v.ID) + volume, _, err := s.Volume().GetByID(s, v.ID) if err != nil { return fmt.Errorf("error fetching Volume: %v", err) } diff --git a/internal/cmd/server/describe_test.go b/internal/cmd/server/describe_test.go index 30495c03..48b3b496 100644 --- a/internal/cmd/server/describe_test.go +++ b/internal/cmd/server/describe_test.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "testing" "time" @@ -21,10 +20,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() srv := &hcloud.Server{ diff --git a/internal/cmd/server/detach_from_network.go b/internal/cmd/server/detach_from_network.go index b2cb4a58..a98f9ac8 100644 --- a/internal/cmd/server/detach_from_network.go +++ b/internal/cmd/server/detach_from_network.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -29,9 +28,9 @@ var DetachFromNetworkCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -39,7 +38,7 @@ var DetachFromNetworkCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := client.Network().Get(ctx, networkIDOrName) + network, _, err := s.Network().Get(s, networkIDOrName) if err != nil { return err } @@ -50,12 +49,12 @@ var DetachFromNetworkCmd = base.Cmd{ opts := hcloud.ServerDetachFromNetworkOpts{ Network: network, } - action, _, err := client.Server().DetachFromNetwork(ctx, server, opts) + action, _, err := s.Server().DetachFromNetwork(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/detach_iso.go b/internal/cmd/server/detach_iso.go index c16d07b3..85e2125d 100644 --- a/internal/cmd/server/detach_iso.go +++ b/internal/cmd/server/detach_iso.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var DetachISOCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var DetachISOCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().DetachISO(ctx, server) + action, _, err := s.Server().DetachISO(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/disable_backup.go b/internal/cmd/server/disable_backup.go index 20e6c435..9abc9dde 100644 --- a/internal/cmd/server/disable_backup.go +++ b/internal/cmd/server/disable_backup.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var DisableBackupCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var DisableBackupCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().DisableBackup(ctx, server) + action, _, err := s.Server().DisableBackup(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/disable_protection.go b/internal/cmd/server/disable_protection.go index 4727689a..c239fe2f 100644 --- a/internal/cmd/server/disable_protection.go +++ b/internal/cmd/server/disable_protection.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,9 +25,9 @@ var DisableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -41,6 +40,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, server, false, opts) + return changeProtection(s, cmd, server, false, opts) }, } diff --git a/internal/cmd/server/disable_rescue.go b/internal/cmd/server/disable_rescue.go index a3c7284a..95adafb3 100644 --- a/internal/cmd/server/disable_rescue.go +++ b/internal/cmd/server/disable_rescue.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var DisableRescueCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var DisableRescueCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().DisableRescue(ctx, server) + action, _, err := s.Server().DisableRescue(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/enable_backup.go b/internal/cmd/server/enable_backup.go index 591a12f6..5c9549b4 100644 --- a/internal/cmd/server/enable_backup.go +++ b/internal/cmd/server/enable_backup.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -27,9 +26,9 @@ var EnableBackupCmd = base.Cmd{ "(deprecated) The time window for the daily backup to run. All times are in UTC. 22-02 means that the backup will be started between 10 PM and 2 AM.") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -42,12 +41,12 @@ var EnableBackupCmd = base.Cmd{ cmd.Print("[WARN] The ability to specify a backup window when enabling backups has been removed. Ignoring flag.\n") } - action, _, err := client.Server().EnableBackup(ctx, server, "") + action, _, err := s.Server().EnableBackup(s, server, "") if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/enable_protection.go b/internal/cmd/server/enable_protection.go index 167ec95b..3e23bdc2 100644 --- a/internal/cmd/server/enable_protection.go +++ b/internal/cmd/server/enable_protection.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "strings" @@ -36,19 +35,19 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.ServerChangePr return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, server *hcloud.Server, enable bool, opts hcloud.ServerChangeProtectionOpts) error { if opts.Delete == nil && opts.Rebuild == nil { return nil } - action, _, err := client.Server().ChangeProtection(ctx, server, opts) + action, _, err := s.Server().ChangeProtection(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -74,9 +73,9 @@ var EnableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -89,6 +88,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, server, true, opts) + return changeProtection(s, cmd, server, true, opts) }, } diff --git a/internal/cmd/server/enable_rescue.go b/internal/cmd/server/enable_rescue.go index d9eed635..af8e9e36 100644 --- a/internal/cmd/server/enable_rescue.go +++ b/internal/cmd/server/enable_rescue.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -30,9 +29,9 @@ var EnableRescueCmd = base.Cmd{ cmd.RegisterFlagCompletionFunc("ssh-key", cmpl.SuggestCandidatesF(client.SSHKey().Names)) return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -48,7 +47,7 @@ var EnableRescueCmd = base.Cmd{ sshKeys, _ := cmd.Flags().GetStringSlice("ssh-key") for _, sshKeyIDOrName := range sshKeys { - sshKey, _, err := client.SSHKey().Get(ctx, sshKeyIDOrName) + sshKey, _, err := s.SSHKey().Get(s, sshKeyIDOrName) if err != nil { return err } @@ -58,12 +57,12 @@ var EnableRescueCmd = base.Cmd{ opts.SSHKeys = append(opts.SSHKeys, sshKey) } - result, _, err := client.Server().EnableRescue(ctx, server, opts) + result, _, err := s.Server().EnableRescue(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return err } diff --git a/internal/cmd/server/ip.go b/internal/cmd/server/ip.go index 3784b122..ca36b088 100644 --- a/internal/cmd/server/ip.go +++ b/internal/cmd/server/ip.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -25,10 +24,10 @@ var IPCmd = base.Cmd{ cmd.Flags().BoolP("ipv6", "6", false, "Print the first address of the IPv6 public server network") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { ipv6, err := cmd.Flags().GetBool("ipv6") idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/server/labels.go b/internal/cmd/server/labels.go index f684f929..42c43ce2 100644 --- a/internal/cmd/server/labels.go +++ b/internal/cmd/server/labels.go @@ -1,11 +1,11 @@ package server import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + state "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Server().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - server, _, err := client.Server().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + server, _, err := s.Server().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return server.Labels, server.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.ServerUpdateOpts{ Labels: labels, } - _, _, err := client.Server().Update(ctx, &hcloud.Server{ID: id}, opts) + _, _, err := s.Server().Update(s, &hcloud.Server{ID: id}, opts) return err }, } diff --git a/internal/cmd/server/labels_test.go b/internal/cmd/server/labels_test.go index ee8965fb..03d22c53 100644 --- a/internal/cmd/server/labels_test.go +++ b/internal/cmd/server/labels_test.go @@ -1,7 +1,6 @@ package server import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerClient.EXPECT(). diff --git a/internal/cmd/server/list.go b/internal/cmd/server/list.go index f2eb6805..ada09fbd 100644 --- a/internal/cmd/server/list.go +++ b/internal/cmd/server/list.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "slices" "strconv" @@ -17,6 +16,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -44,7 +44,7 @@ var ListCmd = base.ListCmd{ _ = cmd.RegisterFlagCompletionFunc("status", cmpl.SuggestCandidates(serverStatusStrings...)) }, - Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { statuses, _ := flags.GetStringSlice("status") opts := hcloud.ServerListOpts{ListOpts: listOpts} @@ -60,7 +60,7 @@ var ListCmd = base.ListCmd{ } } } - servers, err := client.Server().AllWithOpts(ctx, opts) + servers, err := s.Server().AllWithOpts(s, opts) var resources []interface{} for _, r := range servers { diff --git a/internal/cmd/server/list_test.go b/internal/cmd/server/list_test.go index 19aa89e3..6c1333cf 100644 --- a/internal/cmd/server/list_test.go +++ b/internal/cmd/server/list_test.go @@ -1,7 +1,6 @@ package server import ( - "context" "net" "testing" "time" @@ -19,7 +18,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerClient.EXPECT(). diff --git a/internal/cmd/server/metrics.go b/internal/cmd/server/metrics.go index e9e7678c..164eed69 100644 --- a/internal/cmd/server/metrics.go +++ b/internal/cmd/server/metrics.go @@ -1,7 +1,6 @@ package server import ( - "context" "encoding/json" "fmt" "slices" @@ -48,11 +47,11 @@ var MetricsCmd = base.Cmd{ output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML()) return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -88,7 +87,7 @@ var MetricsCmd = base.Cmd{ } } - m, resp, err := client.Server().GetMetrics(ctx, server, hcloud.ServerGetMetricsOpts{ + m, resp, err := s.Server().GetMetrics(s, server, hcloud.ServerGetMetricsOpts{ Types: metricTypes, Start: startTime, End: endTime, diff --git a/internal/cmd/server/poweroff.go b/internal/cmd/server/poweroff.go index b8f40966..ddc75f6b 100644 --- a/internal/cmd/server/poweroff.go +++ b/internal/cmd/server/poweroff.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var PoweroffCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var PoweroffCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().Poweroff(ctx, server) + action, _, err := s.Server().Poweroff(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/poweron.go b/internal/cmd/server/poweron.go index 9f37a8a7..876b97fd 100644 --- a/internal/cmd/server/poweron.go +++ b/internal/cmd/server/poweron.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var PoweronCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var PoweronCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().Poweron(ctx, server) + action, _, err := s.Server().Poweron(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/reboot.go b/internal/cmd/server/reboot.go index 9ffe7165..94add797 100644 --- a/internal/cmd/server/reboot.go +++ b/internal/cmd/server/reboot.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var RebootCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var RebootCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().Reboot(ctx, server) + action, _, err := s.Server().Reboot(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/rebuild.go b/internal/cmd/server/rebuild.go index c32ebd96..54b7bf60 100644 --- a/internal/cmd/server/rebuild.go +++ b/internal/cmd/server/rebuild.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "time" @@ -32,9 +31,9 @@ var RebuildCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { serverIDOrName := args[0] - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return err } @@ -44,7 +43,7 @@ var RebuildCmd = base.Cmd{ imageIDOrName, _ := cmd.Flags().GetString("image") // Select correct image based on server type architecture - image, _, err := client.Image().GetForArchitecture(ctx, imageIDOrName, server.ServerType.Architecture) + image, _, err := s.Image().GetForArchitecture(s, imageIDOrName, server.ServerType.Architecture) if err != nil { return err } @@ -65,12 +64,12 @@ var RebuildCmd = base.Cmd{ opts := hcloud.ServerRebuildOpts{ Image: image, } - result, _, err := client.Server().RebuildWithResult(ctx, server, opts) + result, _, err := s.Server().RebuildWithResult(s, server, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return err } diff --git a/internal/cmd/server/remove_from_placement_group.go b/internal/cmd/server/remove_from_placement_group.go index c269c779..ee4f4138 100644 --- a/internal/cmd/server/remove_from_placement_group.go +++ b/internal/cmd/server/remove_from_placement_group.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -24,9 +23,9 @@ var RemoveFromPlacementGroupCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, actionWaiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -34,12 +33,12 @@ var RemoveFromPlacementGroupCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().RemoveFromPlacementGroup(ctx, server) + action, _, err := s.Server().RemoveFromPlacementGroup(s, server) if err != nil { return err } - if err := actionWaiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/remove_from_placement_group_test.go b/internal/cmd/server/remove_from_placement_group_test.go index 6ed86162..935f85f1 100644 --- a/internal/cmd/server/remove_from_placement_group_test.go +++ b/internal/cmd/server/remove_from_placement_group_test.go @@ -1,7 +1,6 @@ package server_test import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -16,11 +15,7 @@ func TestRemoveFromPlacementGroup(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := server.RemoveFromPlacementGroupCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := server.RemoveFromPlacementGroupCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() server := hcloud.Server{ diff --git a/internal/cmd/server/request_console.go b/internal/cmd/server/request_console.go index 09972928..9b8310e4 100644 --- a/internal/cmd/server/request_console.go +++ b/internal/cmd/server/request_console.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -27,10 +26,10 @@ var RequestConsoleCmd = base.Cmd{ output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML()) return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -38,12 +37,12 @@ var RequestConsoleCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - result, _, err := client.Server().RequestConsole(ctx, server) + result, _, err := s.Server().RequestConsole(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return err } diff --git a/internal/cmd/server/reset.go b/internal/cmd/server/reset.go index 4d7291b2..6e77d5e3 100644 --- a/internal/cmd/server/reset.go +++ b/internal/cmd/server/reset.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var ResetCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var ResetCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().Reset(ctx, server) + action, _, err := s.Server().Reset(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/server/reset_password.go b/internal/cmd/server/reset_password.go index 2bf5077e..ea7a8db9 100644 --- a/internal/cmd/server/reset_password.go +++ b/internal/cmd/server/reset_password.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,9 +22,9 @@ var ResetPasswordCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -33,12 +32,12 @@ var ResetPasswordCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - result, _, err := client.Server().ResetPassword(ctx, server) + result, _, err := s.Server().ResetPassword(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return err } diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go index e95b0e15..a51cc13b 100644 --- a/internal/cmd/server/server.go +++ b/internal/cmd/server/server.go @@ -3,11 +3,10 @@ package server import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "server", Short: "Manage servers", @@ -16,40 +15,40 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - RebootCmd.CobraCommand(cli.Context, client, cli, cli), - PoweronCmd.CobraCommand(cli.Context, client, cli, cli), - PoweroffCmd.CobraCommand(cli.Context, client, cli, cli), - ResetCmd.CobraCommand(cli.Context, client, cli, cli), - ShutdownCmd.CobraCommand(cli.Context, client, cli, cli), - CreateImageCmd.CobraCommand(cli.Context, client, cli, cli), - ResetPasswordCmd.CobraCommand(cli.Context, client, cli, cli), - EnableRescueCmd.CobraCommand(cli.Context, client, cli, cli), - DisableRescueCmd.CobraCommand(cli.Context, client, cli, cli), - AttachISOCmd.CobraCommand(cli.Context, client, cli, cli), - DetachISOCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - ChangeTypeCmd.CobraCommand(cli.Context, client, cli, cli), - RebuildCmd.CobraCommand(cli.Context, client, cli, cli), - EnableBackupCmd.CobraCommand(cli.Context, client, cli, cli), - DisableBackupCmd.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - SSHCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), - SetRDNSCmd.CobraCommand(cli.Context, client, cli, cli), - AttachToNetworkCmd.CobraCommand(cli.Context, client, cli, cli), - DetachFromNetworkCmd.CobraCommand(cli.Context, client, cli, cli), - ChangeAliasIPsCmd.CobraCommand(cli.Context, client, cli, cli), - IPCmd.CobraCommand(cli.Context, client, cli, cli), - RequestConsoleCmd.CobraCommand(cli.Context, client, cli, cli), - MetricsCmd.CobraCommand(cli.Context, client, cli, cli), - AddToPlacementGroupCmd.CobraCommand(cli.Context, client, cli, cli), - RemoveFromPlacementGroupCmd.CobraCommand(cli.Context, client, cli, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + RebootCmd.CobraCommand(s), + PoweronCmd.CobraCommand(s), + PoweroffCmd.CobraCommand(s), + ResetCmd.CobraCommand(s), + ShutdownCmd.CobraCommand(s), + CreateImageCmd.CobraCommand(s), + ResetPasswordCmd.CobraCommand(s), + EnableRescueCmd.CobraCommand(s), + DisableRescueCmd.CobraCommand(s), + AttachISOCmd.CobraCommand(s), + DetachISOCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + ChangeTypeCmd.CobraCommand(s), + RebuildCmd.CobraCommand(s), + EnableBackupCmd.CobraCommand(s), + DisableBackupCmd.CobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + SSHCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), + SetRDNSCmd.CobraCommand(s), + AttachToNetworkCmd.CobraCommand(s), + DetachFromNetworkCmd.CobraCommand(s), + ChangeAliasIPsCmd.CobraCommand(s), + IPCmd.CobraCommand(s), + RequestConsoleCmd.CobraCommand(s), + MetricsCmd.CobraCommand(s), + AddToPlacementGroupCmd.CobraCommand(s), + RemoveFromPlacementGroupCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/server/set_rdns.go b/internal/cmd/server/set_rdns.go index 200ebae9..45b1ab5f 100644 --- a/internal/cmd/server/set_rdns.go +++ b/internal/cmd/server/set_rdns.go @@ -1,13 +1,13 @@ package server import ( - "context" "net" "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var SetRDNSCmd = base.SetRdnsCmd{ ResourceNameSingular: "Server", ShortDescription: "Change reverse DNS of a Server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Server().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Server().Get(s, idOrName) }, GetDefaultIP: func(resource interface{}) net.IP { server := resource.(*hcloud.Server) diff --git a/internal/cmd/server/shutdown.go b/internal/cmd/server/shutdown.go index d43e5304..b7e11670 100644 --- a/internal/cmd/server/shutdown.go +++ b/internal/cmd/server/shutdown.go @@ -1,7 +1,6 @@ package server import ( - "context" "errors" "fmt" "time" @@ -38,13 +37,13 @@ var ShutdownCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { wait, _ := cmd.Flags().GetBool("wait") timeout, _ := cmd.Flags().GetDuration("wait-timeout") idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } @@ -52,12 +51,12 @@ var ShutdownCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := client.Server().Shutdown(ctx, server) + action, _, err := s.Server().Shutdown(s, server) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -83,7 +82,7 @@ var ShutdownCmd = base.Cmd{ errCh <- errors.New("failed to shut down server") return } - server, _, err = client.Server().GetByID(ctx, server.ID) + server, _, err = s.Server().GetByID(s, server.ID) if err != nil { errCh <- err return diff --git a/internal/cmd/server/shutdown_test.go b/internal/cmd/server/shutdown_test.go index 6004f1f8..1ca5ba5f 100644 --- a/internal/cmd/server/shutdown_test.go +++ b/internal/cmd/server/shutdown_test.go @@ -1,7 +1,6 @@ package server import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -16,11 +15,7 @@ func TestShutdown(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ShutdownCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ShutdownCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() var ( @@ -52,11 +47,7 @@ func TestShutdownWait(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := ShutdownCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := ShutdownCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() var ( diff --git a/internal/cmd/server/ssh.go b/internal/cmd/server/ssh.go index 7f30c36b..62afab48 100644 --- a/internal/cmd/server/ssh.go +++ b/internal/cmd/server/ssh.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "os" "os/exec" @@ -32,9 +31,9 @@ var SSHCmd = base.Cmd{ cmd.Flags().IntP("port", "p", 22, "Port for SSH connection") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { + Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := client.Server().Get(ctx, idOrName) + server, _, err := s.Server().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/server/update.go b/internal/cmd/server/update.go index 8cb8f58b..d3802c72 100644 --- a/internal/cmd/server/update.go +++ b/internal/cmd/server/update.go @@ -1,13 +1,12 @@ package server import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Server", ShortDescription: "Update a Server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Server().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Server().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Server name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { floatingIP := resource.(*hcloud.Server) updOpts := hcloud.ServerUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.Server().Update(ctx, floatingIP, updOpts) + _, _, err := s.Server().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/server/update_test.go b/internal/cmd/server/update_test.go index 8d02ead8..82595e4a 100644 --- a/internal/cmd/server/update_test.go +++ b/internal/cmd/server/update_test.go @@ -1,7 +1,6 @@ package server import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerClient.EXPECT(). diff --git a/internal/cmd/servertype/describe.go b/internal/cmd/servertype/describe.go index 7768b4de..436b4551 100644 --- a/internal/cmd/servertype/describe.go +++ b/internal/cmd/servertype/describe.go @@ -1,13 +1,12 @@ package servertype import ( - "context" - "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" ) @@ -17,14 +16,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "server_type", JSONKeyGetByName: "server_types", NameSuggestions: func(c hcapi2.Client) func() []string { return c.ServerType().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - st, _, err := client.ServerType().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + st, _, err := s.ServerType().Get(s, idOrName) if err != nil { return nil, nil, err } return st, hcloud.SchemaFromServerType(st), nil }, - PrintText: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(_ state.State, cmd *cobra.Command, resource interface{}) error { serverType := resource.(*hcloud.ServerType) cmd.Printf("ID:\t\t\t%d\n", serverType.ID) diff --git a/internal/cmd/servertype/describe_test.go b/internal/cmd/servertype/describe_test.go index cf8c68c7..d9565f3a 100644 --- a/internal/cmd/servertype/describe_test.go +++ b/internal/cmd/servertype/describe_test.go @@ -1,7 +1,6 @@ package servertype import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestDescribe(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerTypeClient.EXPECT(). diff --git a/internal/cmd/servertype/list.go b/internal/cmd/servertype/list.go index 91103517..79508301 100644 --- a/internal/cmd/servertype/list.go +++ b/internal/cmd/servertype/list.go @@ -1,7 +1,6 @@ package servertype import ( - "context" "fmt" "github.com/spf13/pflag" @@ -10,6 +9,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -20,12 +20,12 @@ var ListCmd = base.ListCmd{ DefaultColumns: []string{"id", "name", "cores", "cpu_type", "architecture", "memory", "disk", "storage_type", "traffic"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.ServerTypeListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - servers, err := client.ServerType().AllWithOpts(ctx, opts) + servers, err := s.ServerType().AllWithOpts(s, opts) var resources []interface{} for _, r := range servers { diff --git a/internal/cmd/servertype/list_test.go b/internal/cmd/servertype/list_test.go index 04603095..f16128a4 100644 --- a/internal/cmd/servertype/list_test.go +++ b/internal/cmd/servertype/list_test.go @@ -1,7 +1,6 @@ package servertype import ( - "context" "testing" "time" @@ -19,7 +18,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.ServerTypeClient.EXPECT(). diff --git a/internal/cmd/servertype/server_type.go b/internal/cmd/servertype/server_type.go index ad802709..67463b88 100644 --- a/internal/cmd/servertype/server_type.go +++ b/internal/cmd/servertype/server_type.go @@ -3,11 +3,10 @@ package servertype import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "server-type", Short: "Manage server types", @@ -16,8 +15,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), ) return cmd } diff --git a/internal/cmd/sshkey/create.go b/internal/cmd/sshkey/create.go index def45d30..34f616c6 100644 --- a/internal/cmd/sshkey/create.go +++ b/internal/cmd/sshkey/create.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "io" "os" @@ -31,7 +30,7 @@ var CreateCmd = base.CreateCmd{ cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { name, _ := cmd.Flags().GetString("name") publicKey, _ := cmd.Flags().GetString("public-key") publicKeyFile, _ := cmd.Flags().GetString("public-key-from-file") @@ -58,7 +57,7 @@ var CreateCmd = base.CreateCmd{ PublicKey: publicKey, Labels: labels, } - sshKey, _, err := client.SSHKey().Create(ctx, opts) + sshKey, _, err := s.SSHKey().Create(s, opts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/sshkey/create_test.go b/internal/cmd/sshkey/create_test.go index 78a98c3d..e22b2c36 100644 --- a/internal/cmd/sshkey/create_test.go +++ b/internal/cmd/sshkey/create_test.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" _ "embed" "testing" "time" @@ -20,11 +19,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.SSHKeyClient.EXPECT(). @@ -53,11 +48,7 @@ func TestCreateJSON(t *testing.T) { time.Local = time.UTC - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.SSHKeyClient.EXPECT(). diff --git a/internal/cmd/sshkey/delete.go b/internal/cmd/sshkey/delete.go index aef3c620..367ccd48 100644 --- a/internal/cmd/sshkey/delete.go +++ b/internal/cmd/sshkey/delete.go @@ -1,8 +1,6 @@ package sshkey import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "SSH Key", ShortDescription: "Delete a SSH Key", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.SSHKey().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.SSHKey().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { sshKey := resource.(*hcloud.SSHKey) - if _, err := client.SSHKey().Delete(ctx, sshKey); err != nil { + if _, err := s.SSHKey().Delete(s, sshKey); err != nil { return err } return nil diff --git a/internal/cmd/sshkey/delete_test.go b/internal/cmd/sshkey/delete_test.go index 5129211a..f6f47379 100644 --- a/internal/cmd/sshkey/delete_test.go +++ b/internal/cmd/sshkey/delete_test.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() sshKey := &hcloud.SSHKey{ diff --git a/internal/cmd/sshkey/describe.go b/internal/cmd/sshkey/describe.go index 4626c96a..b29b241f 100644 --- a/internal/cmd/sshkey/describe.go +++ b/internal/cmd/sshkey/describe.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "strings" "github.com/dustin/go-humanize" @@ -10,6 +9,7 @@ import ( "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" ) @@ -19,14 +19,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "ssh_key", JSONKeyGetByName: "ssh_keys", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - key, _, err := client.SSHKey().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + key, _, err := s.SSHKey().Get(s, idOrName) if err != nil { return nil, nil, err } return key, hcloud.SchemaFromSSHKey(key), nil }, - PrintText: func(_ context.Context, _ hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(_ state.State, cmd *cobra.Command, resource interface{}) error { sshKey := resource.(*hcloud.SSHKey) cmd.Printf("ID:\t\t%d\n", sshKey.ID) cmd.Printf("Name:\t\t%s\n", sshKey.Name) diff --git a/internal/cmd/sshkey/describe_test.go b/internal/cmd/sshkey/describe_test.go index 57b229cc..8b6008b1 100644 --- a/internal/cmd/sshkey/describe_test.go +++ b/internal/cmd/sshkey/describe_test.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "fmt" "testing" "time" @@ -21,10 +20,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() key := &hcloud.SSHKey{ diff --git a/internal/cmd/sshkey/labels.go b/internal/cmd/sshkey/labels.go index 1980e094..52e86eab 100644 --- a/internal/cmd/sshkey/labels.go +++ b/internal/cmd/sshkey/labels.go @@ -1,11 +1,11 @@ package sshkey import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a SSH Key", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.SSHKey().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - sshKey, _, err := client.SSHKey().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + sshKey, _, err := s.SSHKey().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return sshKey.Labels, sshKey.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.SSHKeyUpdateOpts{ Labels: labels, } - _, _, err := client.SSHKey().Update(ctx, &hcloud.SSHKey{ID: id}, opts) + _, _, err := s.SSHKey().Update(s, &hcloud.SSHKey{ID: id}, opts) return err }, } diff --git a/internal/cmd/sshkey/labels_test.go b/internal/cmd/sshkey/labels_test.go index 0abd5cec..d293ee6f 100644 --- a/internal/cmd/sshkey/labels_test.go +++ b/internal/cmd/sshkey/labels_test.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.SSHKeyClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.SSHKeyClient.EXPECT(). diff --git a/internal/cmd/sshkey/list.go b/internal/cmd/sshkey/list.go index 443b2c6f..67bf7aab 100644 --- a/internal/cmd/sshkey/list.go +++ b/internal/cmd/sshkey/list.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "time" "github.com/spf13/pflag" @@ -10,6 +9,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -19,12 +19,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "ssh_keys", DefaultColumns: []string{"id", "name", "fingerprint", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.SSHKeyListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - sshKeys, err := client.SSHKey().AllWithOpts(ctx, opts) + sshKeys, err := s.SSHKey().AllWithOpts(s, opts) var resources []interface{} for _, n := range sshKeys { diff --git a/internal/cmd/sshkey/list_test.go b/internal/cmd/sshkey/list_test.go index d5f7a501..8e18e44d 100644 --- a/internal/cmd/sshkey/list_test.go +++ b/internal/cmd/sshkey/list_test.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.SSHKeyClient.EXPECT(). diff --git a/internal/cmd/sshkey/sshkey.go b/internal/cmd/sshkey/sshkey.go index 3168bda8..54431c18 100644 --- a/internal/cmd/sshkey/sshkey.go +++ b/internal/cmd/sshkey/sshkey.go @@ -3,11 +3,10 @@ package sshkey import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "ssh-key", Short: "Manage SSH keys", @@ -16,13 +15,13 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), ) return cmd } diff --git a/internal/cmd/sshkey/update.go b/internal/cmd/sshkey/update.go index 635b4c4f..a482dfc9 100644 --- a/internal/cmd/sshkey/update.go +++ b/internal/cmd/sshkey/update.go @@ -1,13 +1,12 @@ package sshkey import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "SSHKey", ShortDescription: "Update a SSHKey", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.SSHKey().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.SSHKey().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "SSH Key name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { floatingIP := resource.(*hcloud.SSHKey) updOpts := hcloud.SSHKeyUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.SSHKey().Update(ctx, floatingIP, updOpts) + _, _, err := s.SSHKey().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/sshkey/update_test.go b/internal/cmd/sshkey/update_test.go index f17f0d52..0d516b66 100644 --- a/internal/cmd/sshkey/update_test.go +++ b/internal/cmd/sshkey/update_test.go @@ -1,7 +1,6 @@ package sshkey import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.SSHKeyClient.EXPECT(). diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index 4eea83ca..0ff31a3d 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -7,18 +7,18 @@ import ( "github.com/hetznercloud/cli/internal/version" ) -func NewCommand(cli *state.State) *cobra.Command { +func NewCommand(_ state.State) *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Print version information", Args: cobra.NoArgs, DisableFlagsInUseLine: true, - RunE: cli.Wrap(runVersion), + RunE: runVersion, } return cmd } -func runVersion(cli *state.State, cmd *cobra.Command, args []string) error { +func runVersion(cmd *cobra.Command, _ []string) error { cmd.Printf("hcloud %s\n", version.Version) return nil } diff --git a/internal/cmd/volume/attach.go b/internal/cmd/volume/attach.go index 3c3aac85..2b9098dd 100644 --- a/internal/cmd/volume/attach.go +++ b/internal/cmd/volume/attach.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "github.com/spf13/cobra" @@ -30,8 +29,8 @@ var AttachCmd = base.Cmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { - volume, _, err := client.Volume().Get(ctx, args[0]) + Run: func(s state.State, cmd *cobra.Command, args []string) error { + volume, _, err := s.Volume().Get(s, args[0]) if err != nil { return err } @@ -40,7 +39,7 @@ var AttachCmd = base.Cmd{ } serverIDOrName, _ := cmd.Flags().GetString("server") - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return err } @@ -48,7 +47,7 @@ var AttachCmd = base.Cmd{ return fmt.Errorf("server not found: %s", serverIDOrName) } automount, _ := cmd.Flags().GetBool("automount") - action, _, err := client.Volume().AttachWithOpts(ctx, volume, hcloud.VolumeAttachOpts{ + action, _, err := s.Volume().AttachWithOpts(s, volume, hcloud.VolumeAttachOpts{ Server: server, Automount: &automount, }) @@ -57,7 +56,7 @@ var AttachCmd = base.Cmd{ return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/volume/create.go b/internal/cmd/volume/create.go index c3ce85d5..651866a5 100644 --- a/internal/cmd/volume/create.go +++ b/internal/cmd/volume/create.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "strconv" @@ -48,7 +47,7 @@ var CreateCmd = base.CreateCmd{ return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) (any, any, error) { + Run: func(s state.State, cmd *cobra.Command, args []string) (any, any, error) { name, _ := cmd.Flags().GetString("name") serverIDOrName, _ := cmd.Flags().GetString("server") size, _ := cmd.Flags().GetInt("size") @@ -78,7 +77,7 @@ var CreateCmd = base.CreateCmd{ } } if serverIDOrName != "" { - server, _, err := client.Server().Get(ctx, serverIDOrName) + server, _, err := s.Server().Get(s, serverIDOrName) if err != nil { return nil, nil, err } @@ -94,20 +93,20 @@ var CreateCmd = base.CreateCmd{ createOpts.Format = &format } - result, _, err := client.Volume().Create(ctx, createOpts) + result, _, err := s.Volume().Create(s, createOpts) if err != nil { return nil, nil, err } - if err := waiter.ActionProgress(cmd, ctx, result.Action); err != nil { + if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } - if err := waiter.WaitForActions(cmd, ctx, result.NextActions); err != nil { + if err := s.WaitForActions(cmd, s, result.NextActions); err != nil { return nil, nil, err } cmd.Printf("Volume %d created\n", result.Volume.ID) - if err := changeProtection(ctx, client, waiter, cmd, result.Volume, true, protectionOpts); err != nil { + if err := changeProtection(s, cmd, result.Volume, true, protectionOpts); err != nil { return nil, nil, err } diff --git a/internal/cmd/volume/create_test.go b/internal/cmd/volume/create_test.go index 66edbca7..2a45093d 100644 --- a/internal/cmd/volume/create_test.go +++ b/internal/cmd/volume/create_test.go @@ -1,7 +1,6 @@ package volume import ( - "context" _ "embed" "testing" "time" @@ -20,11 +19,7 @@ func TestCreate(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.VolumeClient.EXPECT(). @@ -63,11 +58,7 @@ func TestCreateJSON(t *testing.T) { time.Local = time.UTC - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.VolumeClient.EXPECT(). @@ -112,11 +103,7 @@ func TestCreateProtection(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := CreateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := CreateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() volume := &hcloud.Volume{ diff --git a/internal/cmd/volume/delete.go b/internal/cmd/volume/delete.go index 292e5aaa..0e8cf77d 100644 --- a/internal/cmd/volume/delete.go +++ b/internal/cmd/volume/delete.go @@ -1,8 +1,6 @@ package volume import ( - "context" - "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -15,12 +13,12 @@ var DeleteCmd = base.DeleteCmd{ ResourceNameSingular: "Volume", ShortDescription: "Delete a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Volume().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Volume().Get(s, idOrName) }, - Delete: func(ctx context.Context, client hcapi2.Client, _ state.ActionWaiter, cmd *cobra.Command, resource interface{}) error { + Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { volume := resource.(*hcloud.Volume) - if _, err := client.Volume().Delete(ctx, volume); err != nil { + if _, err := s.Volume().Delete(s, volume); err != nil { return err } return nil diff --git a/internal/cmd/volume/delete_test.go b/internal/cmd/volume/delete_test.go index b3b05ddd..a4ce604d 100644 --- a/internal/cmd/volume/delete_test.go +++ b/internal/cmd/volume/delete_test.go @@ -1,7 +1,6 @@ package volume import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,11 +14,7 @@ func TestDelete(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := DeleteCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer, - fx.ActionWaiter) + cmd := DeleteCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() volume := &hcloud.Volume{ diff --git a/internal/cmd/volume/describe.go b/internal/cmd/volume/describe.go index 9dd4ceb2..b4ccebc8 100644 --- a/internal/cmd/volume/describe.go +++ b/internal/cmd/volume/describe.go @@ -1,14 +1,13 @@ package volume import ( - "context" - humanize "github.com/dustin/go-humanize" "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" ) @@ -18,14 +17,14 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByID: "volume", JSONKeyGetByName: "volumes", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - v, _, err := client.Volume().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { + v, _, err := s.Volume().Get(s, idOrName) if err != nil { return nil, nil, err } return v, hcloud.SchemaFromVolume(v), nil }, - PrintText: func(_ context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}) error { + PrintText: func(s state.State, cmd *cobra.Command, resource interface{}) error { volume := resource.(*hcloud.Volume) cmd.Printf("ID:\t\t%d\n", volume.ID) @@ -43,7 +42,7 @@ var DescribeCmd = base.DescribeCmd{ if volume.Server != nil { cmd.Printf("Server:\n") cmd.Printf(" ID:\t\t%d\n", volume.Server.ID) - cmd.Printf(" Name:\t\t%s\n", client.Server().ServerName(volume.Server.ID)) + cmd.Printf(" Name:\t\t%s\n", s.Server().ServerName(volume.Server.ID)) } else { cmd.Print("Server:\n Not attached\n") } diff --git a/internal/cmd/volume/describe_test.go b/internal/cmd/volume/describe_test.go index 322f63f4..abf09aa5 100644 --- a/internal/cmd/volume/describe_test.go +++ b/internal/cmd/volume/describe_test.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "testing" "time" @@ -21,10 +20,7 @@ func TestDescribe(t *testing.T) { time.Local = time.UTC - cmd := DescribeCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := DescribeCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() volume := &hcloud.Volume{ diff --git a/internal/cmd/volume/detach.go b/internal/cmd/volume/detach.go index 38e4c591..499c9de9 100644 --- a/internal/cmd/volume/detach.go +++ b/internal/cmd/volume/detach.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "github.com/spf13/cobra" @@ -23,8 +22,8 @@ var DetachCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { - volume, _, err := client.Volume().Get(ctx, args[0]) + Run: func(s state.State, cmd *cobra.Command, args []string) error { + volume, _, err := s.Volume().Get(s, args[0]) if err != nil { return err } @@ -32,12 +31,12 @@ var DetachCmd = base.Cmd{ return fmt.Errorf("volume not found: %s", args[0]) } - action, _, err := client.Volume().Detach(ctx, volume) + action, _, err := s.Volume().Detach(s, volume) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/volume/disable_protection.go b/internal/cmd/volume/disable_protection.go index 87455a0e..d60d0449 100644 --- a/internal/cmd/volume/disable_protection.go +++ b/internal/cmd/volume/disable_protection.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,8 +25,8 @@ var DisableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { - volume, _, err := client.Volume().Get(ctx, args[0]) + Run: func(s state.State, cmd *cobra.Command, args []string) error { + volume, _, err := s.Volume().Get(s, args[0]) if err != nil { return err } @@ -40,6 +39,6 @@ var DisableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, volume, false, opts) + return changeProtection(s, cmd, volume, false, opts) }, } diff --git a/internal/cmd/volume/enable_protection.go b/internal/cmd/volume/enable_protection.go index efc5ffe3..207bb627 100644 --- a/internal/cmd/volume/enable_protection.go +++ b/internal/cmd/volume/enable_protection.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "strings" @@ -34,19 +33,19 @@ func getChangeProtectionOpts(enable bool, flags []string) (hcloud.VolumeChangePr return opts, nil } -func changeProtection(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, +func changeProtection(s state.State, cmd *cobra.Command, volume *hcloud.Volume, enable bool, opts hcloud.VolumeChangeProtectionOpts) error { if opts.Delete == nil { return nil } - action, _, err := client.Volume().ChangeProtection(ctx, volume, opts) + action, _, err := s.Volume().ChangeProtection(s, volume, opts) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } @@ -72,8 +71,8 @@ var EnableProtectionCmd = base.Cmd{ DisableFlagsInUseLine: true, } }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { - volume, _, err := client.Volume().Get(ctx, args[0]) + Run: func(s state.State, cmd *cobra.Command, args []string) error { + volume, _, err := s.Volume().Get(s, args[0]) if err != nil { return err } @@ -86,6 +85,6 @@ var EnableProtectionCmd = base.Cmd{ return err } - return changeProtection(ctx, client, waiter, cmd, volume, true, opts) + return changeProtection(s, cmd, volume, true, opts) }, } diff --git a/internal/cmd/volume/labels.go b/internal/cmd/volume/labels.go index b502f419..951823f6 100644 --- a/internal/cmd/volume/labels.go +++ b/internal/cmd/volume/labels.go @@ -1,11 +1,11 @@ package volume import ( - "context" "fmt" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,8 +15,8 @@ var LabelCmds = base.LabelCmds{ ShortDescriptionRemove: "Remove a label from a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Volume().LabelKeys }, - FetchLabels: func(ctx context.Context, client hcapi2.Client, idOrName string) (map[string]string, int64, error) { - volume, _, err := client.Volume().Get(ctx, idOrName) + FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { + volume, _, err := s.Volume().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -25,11 +25,11 @@ var LabelCmds = base.LabelCmds{ } return volume.Labels, volume.ID, nil }, - SetLabels: func(ctx context.Context, client hcapi2.Client, id int64, labels map[string]string) error { + SetLabels: func(s state.State, id int64, labels map[string]string) error { opts := hcloud.VolumeUpdateOpts{ Labels: labels, } - _, _, err := client.Volume().Update(ctx, &hcloud.Volume{ID: id}, opts) + _, _, err := s.Volume().Update(s, &hcloud.Volume{ID: id}, opts) return err }, } diff --git a/internal/cmd/volume/labels_test.go b/internal/cmd/volume/labels_test.go index 28c4ba3f..6bc15220 100644 --- a/internal/cmd/volume/labels_test.go +++ b/internal/cmd/volume/labels_test.go @@ -1,7 +1,6 @@ package volume import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestLabelAdd(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.AddCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.AddCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.VolumeClient.EXPECT(). @@ -43,10 +39,7 @@ func TestLabelRemove(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := LabelCmds.RemoveCobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := LabelCmds.RemoveCobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.VolumeClient.EXPECT(). diff --git a/internal/cmd/volume/list.go b/internal/cmd/volume/list.go index d3f1b05c..050c44da 100644 --- a/internal/cmd/volume/list.go +++ b/internal/cmd/volume/list.go @@ -1,7 +1,6 @@ package volume import ( - "context" "strings" "time" @@ -12,6 +11,7 @@ import ( "github.com/hetznercloud/cli/internal/cmd/output" "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" ) @@ -21,12 +21,12 @@ var ListCmd = base.ListCmd{ JSONKeyGetByName: "volumes", DefaultColumns: []string{"id", "name", "size", "server", "location", "age"}, - Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { + Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.VolumeListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } - volumes, err := client.Volume().AllWithOpts(ctx, opts) + volumes, err := s.Volume().AllWithOpts(s, opts) var resources []interface{} for _, n := range volumes { diff --git a/internal/cmd/volume/list_test.go b/internal/cmd/volume/list_test.go index e8cd7e35..d932a82f 100644 --- a/internal/cmd/volume/list_test.go +++ b/internal/cmd/volume/list_test.go @@ -1,7 +1,6 @@ package volume import ( - "context" "testing" "time" @@ -18,7 +17,7 @@ func TestList(t *testing.T) { time.Local = time.UTC - cmd := ListCmd.CobraCommand(context.Background(), fx.Client, fx.TokenEnsurer) + cmd := ListCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.VolumeClient.EXPECT(). diff --git a/internal/cmd/volume/resize.go b/internal/cmd/volume/resize.go index d5df7c9d..5760ac90 100644 --- a/internal/cmd/volume/resize.go +++ b/internal/cmd/volume/resize.go @@ -1,7 +1,6 @@ package volume import ( - "context" "fmt" "github.com/spf13/cobra" @@ -26,8 +25,8 @@ var ResizeCmd = base.Cmd{ cmd.MarkFlagRequired("size") return cmd }, - Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { - volume, _, err := client.Volume().Get(ctx, args[0]) + Run: func(s state.State, cmd *cobra.Command, args []string) error { + volume, _, err := s.Volume().Get(s, args[0]) if err != nil { return err } @@ -36,12 +35,12 @@ var ResizeCmd = base.Cmd{ } size, _ := cmd.Flags().GetInt("size") - action, _, err := client.Volume().Resize(ctx, volume, size) + action, _, err := s.Volume().Resize(s, volume, size) if err != nil { return err } - if err := waiter.ActionProgress(cmd, ctx, action); err != nil { + if err := s.ActionProgress(cmd, s, action); err != nil { return err } diff --git a/internal/cmd/volume/update.go b/internal/cmd/volume/update.go index 73a76e2e..663fcc08 100644 --- a/internal/cmd/volume/update.go +++ b/internal/cmd/volume/update.go @@ -1,13 +1,12 @@ package volume import ( - "context" - "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/hetznercloud/cli/internal/cmd/base" "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -15,18 +14,18 @@ var UpdateCmd = base.UpdateCmd{ ResourceNameSingular: "Volume", ShortDescription: "Update a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, - Fetch: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return client.Volume().Get(ctx, idOrName) + Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { + return s.Volume().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Volume name") }, - Update: func(ctx context.Context, client hcapi2.Client, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { + Update: func(s state.State, cmd *cobra.Command, resource interface{}, flags map[string]pflag.Value) error { floatingIP := resource.(*hcloud.Volume) updOpts := hcloud.VolumeUpdateOpts{ Name: flags["name"].String(), } - _, _, err := client.Volume().Update(ctx, floatingIP, updOpts) + _, _, err := s.Volume().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/volume/update_test.go b/internal/cmd/volume/update_test.go index 40e9a882..9117e27a 100644 --- a/internal/cmd/volume/update_test.go +++ b/internal/cmd/volume/update_test.go @@ -1,7 +1,6 @@ package volume import ( - "context" "testing" "github.com/golang/mock/gomock" @@ -15,10 +14,7 @@ func TestUpdateName(t *testing.T) { fx := testutil.NewFixture(t) defer fx.Finish() - cmd := UpdateCmd.CobraCommand( - context.Background(), - fx.Client, - fx.TokenEnsurer) + cmd := UpdateCmd.CobraCommand(fx.State()) fx.ExpectEnsureToken() fx.Client.VolumeClient.EXPECT(). diff --git a/internal/cmd/volume/volume.go b/internal/cmd/volume/volume.go index ea37493c..7eb82909 100644 --- a/internal/cmd/volume/volume.go +++ b/internal/cmd/volume/volume.go @@ -3,11 +3,10 @@ package volume import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand(s state.State) *cobra.Command { cmd := &cobra.Command{ Use: "volume", Short: "Manage Volumes", @@ -16,18 +15,18 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - ListCmd.CobraCommand(cli.Context, client, cli), - CreateCmd.CobraCommand(cli.Context, client, cli, cli), - UpdateCmd.CobraCommand(cli.Context, client, cli), - DeleteCmd.CobraCommand(cli.Context, client, cli, cli), - DescribeCmd.CobraCommand(cli.Context, client, cli), - AttachCmd.CobraCommand(cli.Context, client, cli, cli), - DetachCmd.CobraCommand(cli.Context, client, cli, cli), - ResizeCmd.CobraCommand(cli.Context, client, cli, cli), - EnableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - DisableProtectionCmd.CobraCommand(cli.Context, client, cli, cli), - LabelCmds.AddCobraCommand(cli.Context, client, cli), - LabelCmds.RemoveCobraCommand(cli.Context, client, cli), + ListCmd.CobraCommand(s), + CreateCmd.CobraCommand(s), + UpdateCmd.CobraCommand(s), + DeleteCmd.CobraCommand(s), + DescribeCmd.CobraCommand(s), + AttachCmd.CobraCommand(s), + DetachCmd.CobraCommand(s), + ResizeCmd.CobraCommand(s), + EnableProtectionCmd.CobraCommand(s), + DisableProtectionCmd.CobraCommand(s), + LabelCmds.AddCobraCommand(s), + LabelCmds.RemoveCobraCommand(s), ) return cmd } diff --git a/internal/state/helpers.go b/internal/state/helpers.go index a08eedfb..4b41254b 100644 --- a/internal/state/helpers.go +++ b/internal/state/helpers.go @@ -5,13 +5,11 @@ import ( "errors" "fmt" "os" - "time" "github.com/cheggaaa/pb/v3" "github.com/spf13/cobra" "golang.org/x/crypto/ssh/terminal" - "github.com/hetznercloud/cli/internal/version" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) @@ -20,51 +18,23 @@ const ( progressBarTpl = `{{ etime . }} {{ bar . "" "=" }} {{ percent . }}` ) -func (c *State) Wrap(f func(*State, *cobra.Command, []string) error) func(*cobra.Command, []string) error { +func Wrap(s State, f func(State, *cobra.Command, []string) error) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { - return f(c, cmd, args) + return f(s, cmd, args) } } -func (c *State) Client() *hcloud.Client { - if c.client == nil { - opts := []hcloud.ClientOption{ - hcloud.WithToken(c.Token), - hcloud.WithApplication("hcloud-cli", version.Version), - } - if c.Endpoint != "" { - opts = append(opts, hcloud.WithEndpoint(c.Endpoint)) - } - if c.Debug { - if c.DebugFilePath == "" { - opts = append(opts, hcloud.WithDebugWriter(os.Stderr)) - } else { - writer, _ := os.Create(c.DebugFilePath) - opts = append(opts, hcloud.WithDebugWriter(writer)) - } - } - // TODO Somehow pass here - // pollInterval, _ := c.RootCommand.PersistentFlags().GetDuration("poll-interval") - pollInterval := 500 * time.Millisecond - if pollInterval > 0 { - opts = append(opts, hcloud.WithPollInterval(pollInterval)) - } - c.client = hcloud.NewClient(opts...) - } - return c.client -} - // StdoutIsTerminal returns whether the CLI is run in a terminal. func StdoutIsTerminal() bool { return terminal.IsTerminal(int(os.Stdout.Fd())) } -func (c *State) ActionProgress(cmd *cobra.Command, ctx context.Context, action *hcloud.Action) error { +func (c *state) ActionProgress(cmd *cobra.Command, ctx context.Context, action *hcloud.Action) error { return c.ActionsProgresses(cmd, ctx, []*hcloud.Action{action}) } -func (c *State) ActionsProgresses(cmd *cobra.Command, ctx context.Context, actions []*hcloud.Action) error { - progressCh, errCh := c.Client().Action.WatchOverallProgress(ctx, actions) +func (c *state) ActionsProgresses(cmd *cobra.Command, ctx context.Context, actions []*hcloud.Action) error { + progressCh, errCh := c.hcloudClient.Action.WatchOverallProgress(ctx, actions) if StdoutIsTerminal() { progress := pb.New(100) @@ -89,14 +59,14 @@ func (c *State) ActionsProgresses(cmd *cobra.Command, ctx context.Context, actio } } -func (c *State) EnsureToken(_ *cobra.Command, _ []string) error { +func (c *state) EnsureToken(_ *cobra.Command, _ []string) error { if c.Token == "" { return errors.New("no active context or token (see `hcloud context --help`)") } return nil } -func (c *State) WaitForActions(cmd *cobra.Command, ctx context.Context, actions []*hcloud.Action) error { +func (c *state) WaitForActions(cmd *cobra.Command, ctx context.Context, actions []*hcloud.Action) error { for _, action := range actions { resources := make(map[string]int64) for _, resource := range action.Resources { @@ -113,7 +83,7 @@ func (c *State) WaitForActions(cmd *cobra.Command, ctx context.Context, actions waitingFor = fmt.Sprintf("Waiting for volume %d to have been attached to server %d", resources["volume"], resources["server"]) } - _, errCh := c.Client().Action.WatchProgress(ctx, action) + _, errCh := c.hcloudClient.Action.WatchProgress(ctx, action) err := DisplayProgressCircle(cmd, errCh, waitingFor) if err != nil { diff --git a/internal/state/state.go b/internal/state/state.go index ee6a7c24..650eb7cd 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -2,42 +2,65 @@ package state import ( "context" - "errors" + "fmt" "io/ioutil" "log" "os" "path/filepath" + "time" + "github.com/hetznercloud/cli/internal/hcapi2" + "github.com/hetznercloud/cli/internal/version" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) -type State struct { +type State interface { + TokenEnsurer + ActionWaiter + context.Context + hcapi2.Client + + WriteConfig() error + Config() *Config +} + +type state struct { + context.Context + hcapi2.Client + Token string Endpoint string - Context context.Context - Config *Config ConfigPath string Debug bool DebugFilePath string - client *hcloud.Client + hcloudClient *hcloud.Client + config *Config } -func New() *State { - s := &State{ +func New() (State, error) { + s := &state{ Context: context.Background(), - Config: &Config{}, + config: &Config{}, ConfigPath: DefaultConfigPath, } if p := os.Getenv("HCLOUD_CONFIG"); p != "" { s.ConfigPath = p } - return s + if err := s.readConfig(); err != nil { + return nil, fmt.Errorf("unable to read config file %q: %s\n", s.ConfigPath, err) + } + s.readEnv() + s.hcloudClient = s.newClient() + s.Client = hcapi2.NewClient(s.hcloudClient) + return s, nil } -var ErrConfigPathUnknown = errors.New("config file path unknown") +func (c *state) Config() *Config { + return c.config +} -func (c *State) ReadEnv() { +func (c *state) readEnv() { if s := os.Getenv("HCLOUD_TOKEN"); s != "" { c.Token = s } @@ -50,9 +73,9 @@ func (c *State) ReadEnv() { if s := os.Getenv("HCLOUD_DEBUG_FILE"); s != "" { c.DebugFilePath = s } - if s := os.Getenv("HCLOUD_CONTEXT"); s != "" && c.Config != nil { - if cfgCtx := c.Config.ContextByName(s); cfgCtx != nil { - c.Config.ActiveContext = cfgCtx + if s := os.Getenv("HCLOUD_CONTEXT"); s != "" && c.config != nil { + if cfgCtx := c.config.ContextByName(s); cfgCtx != nil { + c.config.ActiveContext = cfgCtx c.Token = cfgCtx.Token } else { log.Printf("warning: context %q specified in HCLOUD_CONTEXT does not exist\n", s) @@ -60,39 +83,60 @@ func (c *State) ReadEnv() { } } -func (c *State) ReadConfig() error { - if c.ConfigPath == "" { - return ErrConfigPathUnknown +func (c *state) readConfig() error { + _, err := os.Stat(c.ConfigPath) + if err != nil && !os.IsNotExist(err) { + return err } - data, err := ioutil.ReadFile(c.ConfigPath) + data, err := os.ReadFile(c.ConfigPath) if err != nil { return err } - - if err = UnmarshalConfig(c.Config, data); err != nil { + if err = UnmarshalConfig(c.config, data); err != nil { return err } - if c.Config.ActiveContext != nil { - c.Token = c.Config.ActiveContext.Token + if c.config.ActiveContext != nil { + c.Token = c.config.ActiveContext.Token } - if c.Config.Endpoint != "" { - c.Endpoint = c.Config.Endpoint + if c.config.Endpoint != "" { + c.Endpoint = c.config.Endpoint } - return nil } -func (c *State) WriteConfig() error { - if c.ConfigPath == "" { - return ErrConfigPathUnknown +func (c *state) newClient() *hcloud.Client { + opts := []hcloud.ClientOption{ + hcloud.WithToken(c.Token), + hcloud.WithApplication("hcloud-cli", version.Version), + } + if c.Endpoint != "" { + opts = append(opts, hcloud.WithEndpoint(c.Endpoint)) + } + if c.Debug { + if c.DebugFilePath == "" { + opts = append(opts, hcloud.WithDebugWriter(os.Stderr)) + } else { + writer, _ := os.Create(c.DebugFilePath) + opts = append(opts, hcloud.WithDebugWriter(writer)) + } + } + // TODO Somehow pass here + // pollInterval, _ := c.RootCommand.PersistentFlags().GetDuration("poll-interval") + pollInterval := 500 * time.Millisecond + if pollInterval > 0 { + opts = append(opts, hcloud.WithPollInterval(pollInterval)) } - if c.Config == nil { + return hcloud.NewClient(opts...) +} + +func (c *state) WriteConfig() error { + if c.config == nil { return nil } - data, err := MarshalConfig(c.Config) + data, err := MarshalConfig(c.config) if err != nil { return err } diff --git a/internal/testutil/fixture.go b/internal/testutil/fixture.go index 0eeec090..b504e026 100644 --- a/internal/testutil/fixture.go +++ b/internal/testutil/fixture.go @@ -1,12 +1,15 @@ package testutil import ( + "context" + "errors" "os" "testing" "github.com/golang/mock/gomock" "github.com/spf13/cobra" + "github.com/hetznercloud/cli/internal/hcapi2" hcapi2_mock "github.com/hetznercloud/cli/internal/hcapi2/mock" "github.com/hetznercloud/cli/internal/state" ) @@ -53,3 +56,29 @@ func (f *Fixture) Run(cmd *cobra.Command, args []string) (string, string, error) func (f *Fixture) Finish() { f.MockController.Finish() } + +// fixtureState implements state.State for testing purposes. +type fixtureState struct { + state.TokenEnsurer + state.ActionWaiter + context.Context + hcapi2.Client +} + +func (fixtureState) WriteConfig() error { + return errors.New("not implemented") +} + +func (fixtureState) Config() *state.Config { + return nil +} + +// State returns a state.State implementation for testing purposes. +func (f *Fixture) State() state.State { + return fixtureState{ + TokenEnsurer: f.TokenEnsurer, + ActionWaiter: f.ActionWaiter, + Context: context.Background(), + Client: f.Client, + } +} From 428d70010bbd65a9f1589699101991886915543e Mon Sep 17 00:00:00 2001 From: pauhull <22707808+pauhull@users.noreply.github.com> Date: Thu, 4 Jan 2024 13:40:43 +0100 Subject: [PATCH 2/5] namespace client, move WriteConfig() to state.Config --- internal/cmd/all/list.go | 2 +- internal/cmd/base/cmd.go | 2 +- internal/cmd/base/create.go | 2 +- internal/cmd/base/delete.go | 2 +- internal/cmd/base/describe.go | 2 +- internal/cmd/base/labels.go | 6 +- internal/cmd/base/list.go | 4 +- internal/cmd/base/set_rdns.go | 4 +- internal/cmd/base/update.go | 2 +- internal/cmd/certificate/create.go | 4 +- internal/cmd/certificate/delete.go | 4 +- internal/cmd/certificate/describe.go | 4 +- internal/cmd/certificate/labels.go | 4 +- internal/cmd/certificate/list.go | 2 +- internal/cmd/certificate/update.go | 4 +- internal/cmd/context/create.go | 2 +- internal/cmd/context/delete.go | 2 +- internal/cmd/context/use.go | 2 +- internal/cmd/datacenter/describe.go | 6 +- internal/cmd/datacenter/list.go | 2 +- internal/cmd/firewall/add_rule.go | 4 +- internal/cmd/firewall/apply_to_resource.go | 6 +- internal/cmd/firewall/create.go | 2 +- internal/cmd/firewall/delete.go | 4 +- internal/cmd/firewall/delete_rule.go | 4 +- internal/cmd/firewall/describe.go | 4 +- internal/cmd/firewall/labels.go | 4 +- internal/cmd/firewall/list.go | 2 +- internal/cmd/firewall/remove_from_resource.go | 6 +- internal/cmd/firewall/replace_rules.go | 4 +- internal/cmd/firewall/update.go | 4 +- internal/cmd/floatingip/assign.go | 6 +- internal/cmd/floatingip/create.go | 4 +- internal/cmd/floatingip/delete.go | 4 +- internal/cmd/floatingip/describe.go | 4 +- internal/cmd/floatingip/disable_protection.go | 2 +- internal/cmd/floatingip/enable_protection.go | 4 +- internal/cmd/floatingip/labels.go | 4 +- internal/cmd/floatingip/list.go | 2 +- internal/cmd/floatingip/set_rdns.go | 2 +- internal/cmd/floatingip/unassign.go | 4 +- internal/cmd/floatingip/update.go | 4 +- internal/cmd/image/delete.go | 4 +- internal/cmd/image/describe.go | 2 +- internal/cmd/image/enable_protection.go | 2 +- internal/cmd/image/labels.go | 4 +- internal/cmd/image/list.go | 2 +- internal/cmd/image/update.go | 4 +- internal/cmd/iso/describe.go | 2 +- internal/cmd/iso/list.go | 2 +- internal/cmd/loadbalancer/add_service.go | 4 +- internal/cmd/loadbalancer/add_target.go | 10 +- .../cmd/loadbalancer/attach_to_network.go | 6 +- internal/cmd/loadbalancer/change_algorithm.go | 4 +- internal/cmd/loadbalancer/change_type.go | 6 +- internal/cmd/loadbalancer/create.go | 4 +- internal/cmd/loadbalancer/delete.go | 4 +- internal/cmd/loadbalancer/delete_service.go | 4 +- internal/cmd/loadbalancer/describe.go | 6 +- .../cmd/loadbalancer/detach_from_network.go | 6 +- .../cmd/loadbalancer/disable_protection.go | 2 +- .../loadbalancer/disable_public_interface.go | 4 +- .../cmd/loadbalancer/enable_protection.go | 4 +- .../loadbalancer/enable_public_interface.go | 4 +- internal/cmd/loadbalancer/labels.go | 4 +- internal/cmd/loadbalancer/list.go | 2 +- internal/cmd/loadbalancer/metrics.go | 4 +- internal/cmd/loadbalancer/remove_target.go | 10 +- internal/cmd/loadbalancer/set_rdns.go | 2 +- internal/cmd/loadbalancer/update.go | 4 +- internal/cmd/loadbalancer/update_service.go | 4 +- internal/cmd/loadbalancertype/describe.go | 2 +- internal/cmd/loadbalancertype/list.go | 2 +- internal/cmd/location/describe.go | 2 +- internal/cmd/location/list.go | 2 +- internal/cmd/network/add_route.go | 4 +- internal/cmd/network/add_subnet.go | 4 +- internal/cmd/network/change_ip_range.go | 4 +- internal/cmd/network/create.go | 2 +- internal/cmd/network/delete.go | 4 +- internal/cmd/network/describe.go | 2 +- internal/cmd/network/disable_protection.go | 2 +- internal/cmd/network/enable_protection.go | 4 +- .../cmd/network/expose-routes-to-vswitch.go | 4 +- internal/cmd/network/labels.go | 4 +- internal/cmd/network/list.go | 2 +- internal/cmd/network/remove_route.go | 4 +- internal/cmd/network/remove_subnet.go | 4 +- internal/cmd/network/update.go | 4 +- internal/cmd/placementgroup/create.go | 2 +- internal/cmd/placementgroup/delete.go | 4 +- internal/cmd/placementgroup/describe.go | 4 +- internal/cmd/placementgroup/labels.go | 4 +- internal/cmd/placementgroup/list.go | 2 +- internal/cmd/placementgroup/update.go | 4 +- internal/cmd/primaryip/assign.go | 6 +- internal/cmd/primaryip/changedns.go | 4 +- internal/cmd/primaryip/create.go | 2 +- internal/cmd/primaryip/delete.go | 4 +- internal/cmd/primaryip/describe.go | 2 +- internal/cmd/primaryip/disable_protection.go | 2 +- internal/cmd/primaryip/enable_protection.go | 4 +- internal/cmd/primaryip/labels.go | 4 +- internal/cmd/primaryip/list.go | 2 +- internal/cmd/primaryip/unassign.go | 4 +- internal/cmd/primaryip/update.go | 4 +- internal/cmd/server/add_to_placement_group.go | 6 +- internal/cmd/server/attach_iso.go | 6 +- internal/cmd/server/attach_to_network.go | 6 +- internal/cmd/server/change_alias_ips.go | 6 +- internal/cmd/server/change_type.go | 6 +- internal/cmd/server/create.go | 28 ++-- internal/cmd/server/create_image.go | 4 +- internal/cmd/server/delete.go | 4 +- internal/cmd/server/describe.go | 8 +- internal/cmd/server/detach_from_network.go | 6 +- internal/cmd/server/detach_iso.go | 4 +- internal/cmd/server/disable_backup.go | 4 +- internal/cmd/server/disable_protection.go | 2 +- internal/cmd/server/disable_rescue.go | 4 +- internal/cmd/server/enable_backup.go | 4 +- internal/cmd/server/enable_protection.go | 4 +- internal/cmd/server/enable_rescue.go | 6 +- internal/cmd/server/ip.go | 2 +- internal/cmd/server/labels.go | 4 +- internal/cmd/server/list.go | 2 +- internal/cmd/server/metrics.go | 4 +- internal/cmd/server/poweroff.go | 4 +- internal/cmd/server/poweron.go | 4 +- internal/cmd/server/reboot.go | 4 +- internal/cmd/server/rebuild.go | 6 +- .../cmd/server/remove_from_placement_group.go | 4 +- internal/cmd/server/request_console.go | 4 +- internal/cmd/server/reset.go | 4 +- internal/cmd/server/reset_password.go | 4 +- internal/cmd/server/set_rdns.go | 2 +- internal/cmd/server/shutdown.go | 6 +- internal/cmd/server/ssh.go | 2 +- internal/cmd/server/update.go | 4 +- internal/cmd/servertype/describe.go | 2 +- internal/cmd/servertype/list.go | 2 +- internal/cmd/sshkey/create.go | 2 +- internal/cmd/sshkey/delete.go | 4 +- internal/cmd/sshkey/describe.go | 2 +- internal/cmd/sshkey/labels.go | 4 +- internal/cmd/sshkey/list.go | 2 +- internal/cmd/sshkey/update.go | 4 +- internal/cmd/volume/attach.go | 6 +- internal/cmd/volume/create.go | 4 +- internal/cmd/volume/delete.go | 4 +- internal/cmd/volume/describe.go | 4 +- internal/cmd/volume/detach.go | 4 +- internal/cmd/volume/disable_protection.go | 2 +- internal/cmd/volume/enable_protection.go | 4 +- internal/cmd/volume/labels.go | 4 +- internal/cmd/volume/list.go | 2 +- internal/cmd/volume/resize.go | 4 +- internal/cmd/volume/update.go | 4 +- internal/state/config.go | 17 +++ internal/state/helpers.go | 2 +- internal/state/state.go | 127 +++++++++--------- internal/testutil/fixture.go | 19 ++- 162 files changed, 397 insertions(+), 380 deletions(-) diff --git a/internal/cmd/all/list.go b/internal/cmd/all/list.go index cb3919fc..1ec1a5df 100644 --- a/internal/cmd/all/list.go +++ b/internal/cmd/all/list.go @@ -161,7 +161,7 @@ Listed resources are: for i, lc := range cmds { cols := lc.DefaultColumns - table := lc.OutputTable(s) + table := lc.OutputTable(s.Client()) table.WriteHeader(cols) if len(resources[i]) == 0 { diff --git a/internal/cmd/base/cmd.go b/internal/cmd/base/cmd.go index 434976ee..079680d3 100644 --- a/internal/cmd/base/cmd.go +++ b/internal/cmd/base/cmd.go @@ -16,7 +16,7 @@ type Cmd struct { // CobraCommand creates a command that can be registered with cobra. func (gc *Cmd) CobraCommand(s state.State) *cobra.Command { - cmd := gc.BaseCobraCommand(s) + cmd := gc.BaseCobraCommand(s.Client()) if cmd.Args == nil { cmd.Args = cobra.NoArgs diff --git a/internal/cmd/base/create.go b/internal/cmd/base/create.go index a863a761..496a0dd0 100644 --- a/internal/cmd/base/create.go +++ b/internal/cmd/base/create.go @@ -22,7 +22,7 @@ type CreateCmd struct { // CobraCommand creates a command that can be registered with cobra. func (cc *CreateCmd) CobraCommand(s state.State) *cobra.Command { - cmd := cc.BaseCobraCommand(s) + cmd := cc.BaseCobraCommand(s.Client()) output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML()) diff --git a/internal/cmd/base/delete.go b/internal/cmd/base/delete.go index bf4ee117..a1ef8342 100644 --- a/internal/cmd/base/delete.go +++ b/internal/cmd/base/delete.go @@ -30,7 +30,7 @@ func (dc *DeleteCmd) CobraCommand(s state.State) *cobra.Command { Use: fmt.Sprintf("delete [FLAGS] %s", strings.ToUpper(dc.ResourceNameSingular)), Short: dc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(s.EnsureToken), diff --git a/internal/cmd/base/describe.go b/internal/cmd/base/describe.go index fe6cd527..5d50faf1 100644 --- a/internal/cmd/base/describe.go +++ b/internal/cmd/base/describe.go @@ -35,7 +35,7 @@ func (dc *DescribeCmd) CobraCommand(s state.State) *cobra.Command { Use: fmt.Sprintf("describe [FLAGS] %s", strings.ToUpper(dc.ResourceNameSingular)), Short: dc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(dc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(s.EnsureToken), diff --git a/internal/cmd/base/labels.go b/internal/cmd/base/labels.go index 88e31497..d5472770 100644 --- a/internal/cmd/base/labels.go +++ b/internal/cmd/base/labels.go @@ -30,7 +30,7 @@ func (lc *LabelCmds) AddCobraCommand(s state.State) *cobra.Command { Use: fmt.Sprintf("add-label [FLAGS] %s LABEL", strings.ToUpper(lc.ResourceNameSingular)), Short: lc.ShortDescriptionAdd, Args: cobra.ExactArgs(2), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(lc.NameSuggestions(s))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(lc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(validateAddLabel, s.EnsureToken), @@ -88,12 +88,12 @@ func (lc *LabelCmds) RemoveCobraCommand(s state.State) *cobra.Command { Short: lc.ShortDescriptionRemove, Args: cobra.RangeArgs(1, 2), ValidArgsFunction: cmpl.SuggestArgs( - cmpl.SuggestCandidatesF(lc.NameSuggestions(s)), + cmpl.SuggestCandidatesF(lc.NameSuggestions(s.Client())), cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string { if len(args) != 1 { return nil } - return lc.LabelKeySuggestions(s)(args[0]) + return lc.LabelKeySuggestions(s.Client())(args[0]) }), ), TraverseChildren: true, diff --git a/internal/cmd/base/list.go b/internal/cmd/base/list.go index 2f2d2230..a15dcff0 100644 --- a/internal/cmd/base/list.go +++ b/internal/cmd/base/list.go @@ -26,7 +26,7 @@ type ListCmd struct { // CobraCommand creates a command that can be registered with cobra. func (lc *ListCmd) CobraCommand(s state.State) *cobra.Command { - outputColumns := lc.OutputTable(s).Columns() + outputColumns := lc.OutputTable(s.Client()).Columns() cmd := &cobra.Command{ Use: "list [FlAGS]", @@ -81,7 +81,7 @@ func (lc *ListCmd) Run(s state.State, cmd *cobra.Command) error { cols = outOpts["columns"] } - table := lc.OutputTable(s) + table := lc.OutputTable(s.Client()) if !outOpts.IsSet("noheader") { table.WriteHeader(cols) } diff --git a/internal/cmd/base/set_rdns.go b/internal/cmd/base/set_rdns.go index d4857df2..d363ad7a 100644 --- a/internal/cmd/base/set_rdns.go +++ b/internal/cmd/base/set_rdns.go @@ -31,7 +31,7 @@ func (rc *SetRdnsCmd) CobraCommand(s state.State) *cobra.Command { Use: fmt.Sprintf("set-rdns [FLAGS] %s", strings.ToUpper(rc.ResourceNameSingular)), Short: rc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(rc.NameSuggestions(s))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(rc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(s.EnsureToken), @@ -69,7 +69,7 @@ func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) erro ip = rc.GetDefaultIP(resource) } hostname, _ := cmd.Flags().GetString("hostname") - action, _, err := s.RDNS().ChangeDNSPtr(s, resource.(hcloud.RDNSSupporter), ip, hcloud.String(hostname)) + action, _, err := s.Client().RDNS().ChangeDNSPtr(s, resource.(hcloud.RDNSSupporter), ip, hcloud.String(hostname)) if err != nil { return err } diff --git a/internal/cmd/base/update.go b/internal/cmd/base/update.go index a2907953..9ddf0c30 100644 --- a/internal/cmd/base/update.go +++ b/internal/cmd/base/update.go @@ -31,7 +31,7 @@ func (uc *UpdateCmd) CobraCommand(s state.State) *cobra.Command { Use: fmt.Sprintf("update [FLAGS] %s", strings.ToUpper(uc.ResourceNameSingular)), Short: uc.ShortDescription, Args: cobra.ExactArgs(1), - ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(uc.NameSuggestions(s))), + ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(uc.NameSuggestions(s.Client()))), TraverseChildren: true, DisableFlagsInUseLine: true, PreRunE: util.ChainRunE(s.EnsureToken), diff --git a/internal/cmd/certificate/create.go b/internal/cmd/certificate/create.go index 173af176..43c5e1db 100644 --- a/internal/cmd/certificate/create.go +++ b/internal/cmd/certificate/create.go @@ -95,7 +95,7 @@ func createUploaded(s state.State, cmd *cobra.Command) (*hcloud.Certificate, err Certificate: string(certPEM), PrivateKey: string(keyPEM), } - cert, _, err = s.Certificate().Create(s, createOpts) + cert, _, err = s.Client().Certificate().Create(s, createOpts) if err != nil { return nil, err } @@ -126,7 +126,7 @@ func createManaged(s state.State, cmd *cobra.Command) (*hcloud.Certificate, erro Type: hcloud.CertificateTypeManaged, DomainNames: domains, } - res, _, err = s.Certificate().CreateCertificate(s, createOpts) + res, _, err = s.Client().Certificate().CreateCertificate(s, createOpts) if err != nil { return nil, err } diff --git a/internal/cmd/certificate/delete.go b/internal/cmd/certificate/delete.go index 9298f1a8..66f51813 100644 --- a/internal/cmd/certificate/delete.go +++ b/internal/cmd/certificate/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Certificate().Get(s, idOrName) + return s.Client().Certificate().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { certificate := resource.(*hcloud.Certificate) - if _, err := s.Certificate().Delete(s, certificate); err != nil { + if _, err := s.Client().Certificate().Delete(s, certificate); err != nil { return err } return nil diff --git a/internal/cmd/certificate/describe.go b/internal/cmd/certificate/describe.go index 75cfedc8..6573cbc2 100644 --- a/internal/cmd/certificate/describe.go +++ b/internal/cmd/certificate/describe.go @@ -18,7 +18,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "certificates", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Certificate().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - cert, _, err := s.Certificate().Get(s, idOrName) + cert, _, err := s.Client().Certificate().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -66,7 +66,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf(" - ID: %d\n", ub.ID) continue } - cmd.Printf(" - Name: %s\n", s.LoadBalancer().LoadBalancerName(ub.ID)) + cmd.Printf(" - Name: %s\n", s.Client().LoadBalancer().LoadBalancerName(ub.ID)) } } return nil diff --git a/internal/cmd/certificate/labels.go b/internal/cmd/certificate/labels.go index 6080b00d..60d1305d 100644 --- a/internal/cmd/certificate/labels.go +++ b/internal/cmd/certificate/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.Certificate().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Certificate().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - certificate, _, err := s.Certificate().Get(s, idOrName) + certificate, _, err := s.Client().Certificate().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.CertificateUpdateOpts{ Labels: labels, } - _, _, err := s.Certificate().Update(s, &hcloud.Certificate{ID: id}, opts) + _, _, err := s.Client().Certificate().Update(s, &hcloud.Certificate{ID: id}, opts) return err }, } diff --git a/internal/cmd/certificate/list.go b/internal/cmd/certificate/list.go index d45c29a2..9acc0a19 100644 --- a/internal/cmd/certificate/list.go +++ b/internal/cmd/certificate/list.go @@ -25,7 +25,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - certificates, err := s.Certificate().AllWithOpts(s, opts) + certificates, err := s.Client().Certificate().AllWithOpts(s, opts) var resources []interface{} for _, n := range certificates { diff --git a/internal/cmd/certificate/update.go b/internal/cmd/certificate/update.go index 55985934..5d347d04 100644 --- a/internal/cmd/certificate/update.go +++ b/internal/cmd/certificate/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a certificate", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Certificate().Get(s, idOrName) + return s.Client().Certificate().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Certificate Name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.CertificateUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.Certificate().Update(s, certificate, updOpts) + _, _, err := s.Client().Certificate().Update(s, certificate, updOpts) if err != nil { return err } diff --git a/internal/cmd/context/create.go b/internal/cmd/context/create.go index 4a33762f..92b6db5c 100644 --- a/internal/cmd/context/create.go +++ b/internal/cmd/context/create.go @@ -85,7 +85,7 @@ func runCreate(s state.State, cmd *cobra.Command, args []string) error { cfg.Contexts = append(cfg.Contexts, context) cfg.ActiveContext = context - if err := s.WriteConfig(); err != nil { + if err := cfg.Write(); err != nil { return err } diff --git a/internal/cmd/context/delete.go b/internal/cmd/context/delete.go index 0c5f5467..89eca4be 100644 --- a/internal/cmd/context/delete.go +++ b/internal/cmd/context/delete.go @@ -35,5 +35,5 @@ func runDelete(s state.State, _ *cobra.Command, args []string) error { cfg.ActiveContext = nil } cfg.RemoveContext(context) - return s.WriteConfig() + return cfg.Write() } diff --git a/internal/cmd/context/use.go b/internal/cmd/context/use.go index e82c4f06..f5d89f1d 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -34,5 +34,5 @@ func runUse(s state.State, _ *cobra.Command, args []string) error { return fmt.Errorf("context not found: %v", name) } cfg.ActiveContext = context - return s.WriteConfig() + return cfg.Write() } diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index 7d73f88c..132dec36 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -16,7 +16,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "datacenters", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Datacenter().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - dc, _, err := s.Datacenter().Get(s, idOrName) + dc, _, err := s.Client().Datacenter().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -40,8 +40,8 @@ var DescribeCmd = base.DescribeCmd{ printServerTypes := func(list []*hcloud.ServerType) { for _, t := range list { cmd.Printf(" - ID:\t\t %d\n", t.ID) - cmd.Printf(" Name:\t %s\n", s.ServerType().ServerTypeName(t.ID)) - cmd.Printf(" Description: %s\n", s.ServerType().ServerTypeDescription(t.ID)) + cmd.Printf(" Name:\t %s\n", s.Client().ServerType().ServerTypeName(t.ID)) + cmd.Printf(" Description: %s\n", s.Client().ServerType().ServerTypeDescription(t.ID)) } } diff --git a/internal/cmd/datacenter/list.go b/internal/cmd/datacenter/list.go index b74cece3..0894c0d0 100644 --- a/internal/cmd/datacenter/list.go +++ b/internal/cmd/datacenter/list.go @@ -21,7 +21,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - datacenters, err := s.Datacenter().AllWithOpts(s, opts) + datacenters, err := s.Client().Datacenter().AllWithOpts(s, opts) var resources []interface{} for _, n := range datacenters { resources = append(resources, n) diff --git a/internal/cmd/firewall/add_rule.go b/internal/cmd/firewall/add_rule.go index b220e1f9..736dd0e9 100644 --- a/internal/cmd/firewall/add_rule.go +++ b/internal/cmd/firewall/add_rule.go @@ -49,7 +49,7 @@ var AddRuleCmd = base.Cmd{ description, _ := cmd.Flags().GetString("description") idOrName := args[0] - firewall, _, err := s.Firewall().Get(s, idOrName) + firewall, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return err } @@ -106,7 +106,7 @@ var AddRuleCmd = base.Cmd{ rules := append(firewall.Rules, rule) - actions, _, err := s.Firewall().SetRules(s, firewall, + actions, _, err := s.Client().Firewall().SetRules(s, firewall, hcloud.FirewallSetRulesOpts{Rules: rules}, ) if err != nil { diff --git a/internal/cmd/firewall/apply_to_resource.go b/internal/cmd/firewall/apply_to_resource.go index f501ad11..156d03bc 100644 --- a/internal/cmd/firewall/apply_to_resource.go +++ b/internal/cmd/firewall/apply_to_resource.go @@ -53,7 +53,7 @@ var ApplyToResourceCmd = base.Cmd{ serverIdOrName, _ := cmd.Flags().GetString("server") labelSelector, _ := cmd.Flags().GetString("label-selector") idOrName := args[0] - firewall, _, err := s.Firewall().Get(s, idOrName) + firewall, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return err } @@ -64,7 +64,7 @@ var ApplyToResourceCmd = base.Cmd{ switch opts.Type { case hcloud.FirewallResourceTypeServer: - server, _, err := s.Server().Get(s, serverIdOrName) + server, _, err := s.Client().Server().Get(s, serverIdOrName) if err != nil { return err } @@ -78,7 +78,7 @@ var ApplyToResourceCmd = base.Cmd{ return fmt.Errorf("unknown type %s", opts.Type) } - actions, _, err := s.Firewall().ApplyResources(s, firewall, []hcloud.FirewallResource{opts}) + actions, _, err := s.Client().Firewall().ApplyResources(s, firewall, []hcloud.FirewallResource{opts}) if err != nil { return err } diff --git a/internal/cmd/firewall/create.go b/internal/cmd/firewall/create.go index d8d024d4..2feb9df5 100644 --- a/internal/cmd/firewall/create.go +++ b/internal/cmd/firewall/create.go @@ -78,7 +78,7 @@ var CreateCmd = base.CreateCmd{ } } - result, _, err := s.Firewall().Create(s, opts) + result, _, err := s.Client().Firewall().Create(s, opts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/firewall/delete.go b/internal/cmd/firewall/delete.go index ca4b0b49..eafce2ba 100644 --- a/internal/cmd/firewall/delete.go +++ b/internal/cmd/firewall/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Firewall().Get(s, idOrName) + return s.Client().Firewall().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { firewall := resource.(*hcloud.Firewall) - if _, err := s.Firewall().Delete(s, firewall); err != nil { + if _, err := s.Client().Firewall().Delete(s, firewall); err != nil { return err } return nil diff --git a/internal/cmd/firewall/delete_rule.go b/internal/cmd/firewall/delete_rule.go index 804e16e7..59e5d66a 100644 --- a/internal/cmd/firewall/delete_rule.go +++ b/internal/cmd/firewall/delete_rule.go @@ -50,7 +50,7 @@ var DeleteRuleCmd = base.Cmd{ description, _ := cmd.Flags().GetString("description") idOrName := args[0] - firewall, _, err := s.Firewall().Get(s, idOrName) + firewall, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return err } @@ -114,7 +114,7 @@ var DeleteRuleCmd = base.Cmd{ if len(rules) == len(firewall.Rules) { return fmt.Errorf("the specified rule was not found in the ruleset of Firewall %d", firewall.ID) } - actions, _, err := s.Firewall().SetRules(s, firewall, + actions, _, err := s.Client().Firewall().SetRules(s, firewall, hcloud.FirewallSetRulesOpts{Rules: rules}, ) if err != nil { diff --git a/internal/cmd/firewall/describe.go b/internal/cmd/firewall/describe.go index b2567541..bdcf3ab9 100644 --- a/internal/cmd/firewall/describe.go +++ b/internal/cmd/firewall/describe.go @@ -20,7 +20,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "firewalls", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - fw, _, err := s.Firewall().Get(s, idOrName) + fw, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -80,7 +80,7 @@ var DescribeCmd = base.DescribeCmd{ switch resource.Type { case hcloud.FirewallResourceTypeServer: cmd.Printf(" Server ID:\t\t%d\n", resource.Server.ID) - cmd.Printf(" Server Name:\t%s\n", s.Server().ServerName(resource.Server.ID)) + cmd.Printf(" Server Name:\t%s\n", s.Client().Server().ServerName(resource.Server.ID)) case hcloud.FirewallResourceTypeLabelSelector: cmd.Printf(" Label Selector:\t%s\n", resource.LabelSelector.Selector) } diff --git a/internal/cmd/firewall/labels.go b/internal/cmd/firewall/labels.go index 46902535..4c1f9883 100644 --- a/internal/cmd/firewall/labels.go +++ b/internal/cmd/firewall/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Firewall().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - firewall, _, err := s.Firewall().Get(s, idOrName) + firewall, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.FirewallUpdateOpts{ Labels: labels, } - _, _, err := s.Firewall().Update(s, &hcloud.Firewall{ID: id}, opts) + _, _, err := s.Client().Firewall().Update(s, &hcloud.Firewall{ID: id}, opts) return err }, } diff --git a/internal/cmd/firewall/list.go b/internal/cmd/firewall/list.go index 919c65ec..a983840e 100644 --- a/internal/cmd/firewall/list.go +++ b/internal/cmd/firewall/list.go @@ -23,7 +23,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - firewalls, err := s.Firewall().AllWithOpts(s, opts) + firewalls, err := s.Client().Firewall().AllWithOpts(s, opts) var resources []interface{} for _, n := range firewalls { diff --git a/internal/cmd/firewall/remove_from_resource.go b/internal/cmd/firewall/remove_from_resource.go index c4211acf..c2ab8e98 100644 --- a/internal/cmd/firewall/remove_from_resource.go +++ b/internal/cmd/firewall/remove_from_resource.go @@ -53,7 +53,7 @@ var RemoveFromResourceCmd = base.Cmd{ labelSelector, _ := cmd.Flags().GetString("label-selector") idOrName := args[0] - firewall, _, err := s.Firewall().Get(s, idOrName) + firewall, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return err } @@ -64,7 +64,7 @@ var RemoveFromResourceCmd = base.Cmd{ switch opts.Type { case hcloud.FirewallResourceTypeServer: - server, _, err := s.Server().Get(s, serverIdOrName) + server, _, err := s.Client().Server().Get(s, serverIdOrName) if err != nil { return err } @@ -77,7 +77,7 @@ var RemoveFromResourceCmd = base.Cmd{ default: return fmt.Errorf("unknown type %s", opts.Type) } - actions, _, err := s.Firewall().RemoveResources(s, firewall, []hcloud.FirewallResource{opts}) + actions, _, err := s.Client().Firewall().RemoveResources(s, firewall, []hcloud.FirewallResource{opts}) if err != nil { return err } diff --git a/internal/cmd/firewall/replace_rules.go b/internal/cmd/firewall/replace_rules.go index 3ea90aca..4e6eaa7a 100644 --- a/internal/cmd/firewall/replace_rules.go +++ b/internal/cmd/firewall/replace_rules.go @@ -33,7 +33,7 @@ var ReplaceRulesCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - firewall, _, err := s.Firewall().Get(s, idOrName) + firewall, _, err := s.Client().Firewall().Get(s, idOrName) if err != nil { return err } @@ -90,7 +90,7 @@ var ReplaceRulesCmd = base.Cmd{ opts.Rules = append(opts.Rules, r) } - actions, _, err := s.Firewall().SetRules(s, firewall, opts) + actions, _, err := s.Client().Firewall().SetRules(s, firewall, opts) if err != nil { return err } diff --git a/internal/cmd/firewall/update.go b/internal/cmd/firewall/update.go index 8767b80e..18cf7fbc 100644 --- a/internal/cmd/firewall/update.go +++ b/internal/cmd/firewall/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a firewall", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Firewall().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Firewall().Get(s, idOrName) + return s.Client().Firewall().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Firewall name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.FirewallUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.Firewall().Update(s, firewall, updOpts) + _, _, err := s.Client().Firewall().Update(s, firewall, updOpts) if err != nil { return err } diff --git a/internal/cmd/floatingip/assign.go b/internal/cmd/floatingip/assign.go index f0b6b2c2..40865898 100644 --- a/internal/cmd/floatingip/assign.go +++ b/internal/cmd/floatingip/assign.go @@ -27,7 +27,7 @@ var AssignCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := s.FloatingIP().Get(s, idOrName) + floatingIP, _, err := s.Client().FloatingIP().Get(s, idOrName) if err != nil { return err } @@ -36,7 +36,7 @@ var AssignCmd = base.Cmd{ } serverIDOrName := args[1] - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return err } @@ -44,7 +44,7 @@ var AssignCmd = base.Cmd{ return fmt.Errorf("server not found: %s", serverIDOrName) } - action, _, err := s.FloatingIP().Assign(s, floatingIP, server) + action, _, err := s.Client().FloatingIP().Assign(s, floatingIP, server) if err != nil { return err } diff --git a/internal/cmd/floatingip/create.go b/internal/cmd/floatingip/create.go index dadd3ecd..33765b73 100644 --- a/internal/cmd/floatingip/create.go +++ b/internal/cmd/floatingip/create.go @@ -79,7 +79,7 @@ var CreateCmd = base.CreateCmd{ createOpts.HomeLocation = &hcloud.Location{Name: homeLocation} } if serverNameOrID != "" { - server, _, err := s.Server().Get(s, serverNameOrID) + server, _, err := s.Client().Server().Get(s, serverNameOrID) if err != nil { return nil, nil, err } @@ -89,7 +89,7 @@ var CreateCmd = base.CreateCmd{ createOpts.Server = server } - result, _, err := s.FloatingIP().Create(s, createOpts) + result, _, err := s.Client().FloatingIP().Create(s, createOpts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/floatingip/delete.go b/internal/cmd/floatingip/delete.go index 66139746..010b603f 100644 --- a/internal/cmd/floatingip/delete.go +++ b/internal/cmd/floatingip/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.FloatingIP().Get(s, idOrName) + return s.Client().FloatingIP().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { floatingIP := resource.(*hcloud.FloatingIP) - if _, err := s.FloatingIP().Delete(s, floatingIP); err != nil { + if _, err := s.Client().FloatingIP().Delete(s, floatingIP); err != nil { return err } return nil diff --git a/internal/cmd/floatingip/describe.go b/internal/cmd/floatingip/describe.go index 8111abfa..0bfae839 100644 --- a/internal/cmd/floatingip/describe.go +++ b/internal/cmd/floatingip/describe.go @@ -18,7 +18,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "floating_ips", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - ip, _, err := s.FloatingIP().Get(s, idOrName) + ip, _, err := s.Client().FloatingIP().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -42,7 +42,7 @@ var DescribeCmd = base.DescribeCmd{ if floatingIP.Server != nil { cmd.Printf("Server:\n") cmd.Printf(" ID:\t%d\n", floatingIP.Server.ID) - cmd.Printf(" Name:\t%s\n", s.Server().ServerName(floatingIP.Server.ID)) + cmd.Printf(" Name:\t%s\n", s.Client().Server().ServerName(floatingIP.Server.ID)) } else { cmd.Print("Server:\n Not assigned\n") } diff --git a/internal/cmd/floatingip/disable_protection.go b/internal/cmd/floatingip/disable_protection.go index 5274f0cb..3c7cb60f 100644 --- a/internal/cmd/floatingip/disable_protection.go +++ b/internal/cmd/floatingip/disable_protection.go @@ -27,7 +27,7 @@ var DisableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := s.FloatingIP().Get(s, idOrName) + floatingIP, _, err := s.Client().FloatingIP().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/floatingip/enable_protection.go b/internal/cmd/floatingip/enable_protection.go index 484ccc60..e236c2ca 100644 --- a/internal/cmd/floatingip/enable_protection.go +++ b/internal/cmd/floatingip/enable_protection.go @@ -40,7 +40,7 @@ func changeProtection(s state.State, cmd *cobra.Command, return nil } - action, _, err := s.FloatingIP().ChangeProtection(s, floatingIP, opts) + action, _, err := s.Client().FloatingIP().ChangeProtection(s, floatingIP, opts) if err != nil { return err } @@ -74,7 +74,7 @@ var EnableProtectionCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := s.FloatingIP().Get(s, idOrName) + floatingIP, _, err := s.Client().FloatingIP().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/floatingip/labels.go b/internal/cmd/floatingip/labels.go index 6526ab68..a63a7598 100644 --- a/internal/cmd/floatingip/labels.go +++ b/internal/cmd/floatingip/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.FloatingIP().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - floatingIP, _, err := s.FloatingIP().Get(s, idOrName) + floatingIP, _, err := s.Client().FloatingIP().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.FloatingIPUpdateOpts{ Labels: labels, } - _, _, err := s.FloatingIP().Update(s, &hcloud.FloatingIP{ID: id}, opts) + _, _, err := s.Client().FloatingIP().Update(s, &hcloud.FloatingIP{ID: id}, opts) return err }, } diff --git a/internal/cmd/floatingip/list.go b/internal/cmd/floatingip/list.go index 9ec312c5..c2710642 100644 --- a/internal/cmd/floatingip/list.go +++ b/internal/cmd/floatingip/list.go @@ -26,7 +26,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - floatingIPs, err := s.FloatingIP().AllWithOpts(s, opts) + floatingIPs, err := s.Client().FloatingIP().AllWithOpts(s, opts) var resources []interface{} for _, n := range floatingIPs { diff --git a/internal/cmd/floatingip/set_rdns.go b/internal/cmd/floatingip/set_rdns.go index d910621d..b1d387a8 100644 --- a/internal/cmd/floatingip/set_rdns.go +++ b/internal/cmd/floatingip/set_rdns.go @@ -16,7 +16,7 @@ var SetRDNSCmd = base.SetRdnsCmd{ ShortDescription: "Change reverse DNS of a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.FloatingIP().Get(s, idOrName) + return s.Client().FloatingIP().Get(s, idOrName) }, GetDefaultIP: func(resource interface{}) net.IP { floatingIP := resource.(*hcloud.FloatingIP) diff --git a/internal/cmd/floatingip/unassign.go b/internal/cmd/floatingip/unassign.go index a3972dd5..d59837f8 100644 --- a/internal/cmd/floatingip/unassign.go +++ b/internal/cmd/floatingip/unassign.go @@ -24,7 +24,7 @@ var UnassignCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - floatingIP, _, err := s.FloatingIP().Get(s, idOrName) + floatingIP, _, err := s.Client().FloatingIP().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var UnassignCmd = base.Cmd{ return fmt.Errorf("Floating IP not found: %v", idOrName) } - action, _, err := s.FloatingIP().Unassign(s, floatingIP) + action, _, err := s.Client().FloatingIP().Unassign(s, floatingIP) if err != nil { return err } diff --git a/internal/cmd/floatingip/update.go b/internal/cmd/floatingip/update.go index 56a0b2f9..4a0175a2 100644 --- a/internal/cmd/floatingip/update.go +++ b/internal/cmd/floatingip/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a Floating IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.FloatingIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.FloatingIP().Get(s, idOrName) + return s.Client().FloatingIP().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Floating IP name") @@ -27,7 +27,7 @@ var UpdateCmd = base.UpdateCmd{ Name: flags["name"].String(), Description: flags["description"].String(), } - _, _, err := s.FloatingIP().Update(s, floatingIP, updOpts) + _, _, err := s.Client().FloatingIP().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/image/delete.go b/internal/cmd/image/delete.go index 55f79ab9..1f7f8cd9 100644 --- a/internal/cmd/image/delete.go +++ b/internal/cmd/image/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Image().Get(s, idOrName) + return s.Client().Image().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { image := resource.(*hcloud.Image) - if _, err := s.Image().Delete(s, image); err != nil { + if _, err := s.Client().Image().Delete(s, image); err != nil { return err } return nil diff --git a/internal/cmd/image/describe.go b/internal/cmd/image/describe.go index a2142b6b..327e22fa 100644 --- a/internal/cmd/image/describe.go +++ b/internal/cmd/image/describe.go @@ -34,7 +34,7 @@ var DescribeCmd = base.DescribeCmd{ if !cmd.Flags().Changed("architecture") { _, _ = fmt.Fprintln(os.Stderr, "INFO: This command only returns x86 images by default. Explicitly set the --architecture=x86|arm flag to hide this message.") } - img, _, err := s.Image().GetForArchitecture(s, idOrName, hcloud.Architecture(arch)) + img, _, err := s.Client().Image().GetForArchitecture(s, idOrName, hcloud.Architecture(arch)) if err != nil { return nil, nil, err } diff --git a/internal/cmd/image/enable_protection.go b/internal/cmd/image/enable_protection.go index 0d5f9a67..3bac739e 100644 --- a/internal/cmd/image/enable_protection.go +++ b/internal/cmd/image/enable_protection.go @@ -42,7 +42,7 @@ func changeProtection(s state.State, cmd *cobra.Command, return nil } - action, _, err := s.Image().ChangeProtection(s, image, opts) + action, _, err := s.Client().Image().ChangeProtection(s, image, opts) if err != nil { return err } diff --git a/internal/cmd/image/labels.go b/internal/cmd/image/labels.go index 851c0da4..e1ae3849 100644 --- a/internal/cmd/image/labels.go +++ b/internal/cmd/image/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Image().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - image, _, err := s.Image().Get(s, idOrName) + image, _, err := s.Client().Image().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.ImageUpdateOpts{ Labels: labels, } - _, _, err := s.Image().Update(s, &hcloud.Image{ID: id}, opts) + _, _, err := s.Client().Image().Update(s, &hcloud.Image{ID: id}, opts) return err }, } diff --git a/internal/cmd/image/list.go b/internal/cmd/image/list.go index 7ef3dccc..67178cd5 100644 --- a/internal/cmd/image/list.go +++ b/internal/cmd/image/list.go @@ -60,7 +60,7 @@ var ListCmd = base.ListCmd{ opts.Sort = sorts } - images, err := s.Image().AllWithOpts(s, opts) + images, err := s.Client().Image().AllWithOpts(s, opts) var resources []interface{} for _, n := range images { diff --git a/internal/cmd/image/update.go b/internal/cmd/image/update.go index 1292802f..75e2562f 100644 --- a/internal/cmd/image/update.go +++ b/internal/cmd/image/update.go @@ -16,7 +16,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update an image", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Image().Get(s, idOrName) + return s.Client().Image().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("description", "", "Image description") @@ -29,7 +29,7 @@ var UpdateCmd = base.UpdateCmd{ Description: hcloud.String(flags["description"].String()), Type: hcloud.ImageType(flags["type"].String()), } - _, _, err := s.Image().Update(s, image, updOpts) + _, _, err := s.Client().Image().Update(s, image, updOpts) if err != nil { return err } diff --git a/internal/cmd/iso/describe.go b/internal/cmd/iso/describe.go index 6b5c5c60..f14b3d84 100644 --- a/internal/cmd/iso/describe.go +++ b/internal/cmd/iso/describe.go @@ -18,7 +18,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "isos", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Location().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - iso, _, err := s.ISO().Get(s, idOrName) + iso, _, err := s.Client().ISO().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/iso/list.go b/internal/cmd/iso/list.go index 056da67f..2eebd966 100644 --- a/internal/cmd/iso/list.go +++ b/internal/cmd/iso/list.go @@ -65,7 +65,7 @@ var ListCmd = base.ListCmd{ opts.Sort = sorts } - isos, err := s.ISO().AllWithOpts(s, opts) + isos, err := s.Client().ISO().AllWithOpts(s, opts) var resources []interface{} for _, iso := range isos { diff --git a/internal/cmd/loadbalancer/add_service.go b/internal/cmd/loadbalancer/add_service.go index 772b85be..125e9904 100644 --- a/internal/cmd/loadbalancer/add_service.go +++ b/internal/cmd/loadbalancer/add_service.go @@ -82,7 +82,7 @@ var AddServiceCmd = base.Cmd{ httpCookieLifetime, _ := cmd.Flags().GetDuration("http-cookie-lifetime") httpRedirect, _ := cmd.Flags().GetBool("http-redirect-http") - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -117,7 +117,7 @@ var AddServiceCmd = base.Cmd{ opts.HTTP.Certificates = append(opts.HTTP.Certificates, &hcloud.Certificate{ID: certificateID}) } } - action, _, err := s.LoadBalancer().AddService(s, loadBalancer, opts) + action, _, err := s.Client().LoadBalancer().AddService(s, loadBalancer, opts) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/add_target.go b/internal/cmd/loadbalancer/add_target.go index cf0aed66..781668c5 100644 --- a/internal/cmd/loadbalancer/add_target.go +++ b/internal/cmd/loadbalancer/add_target.go @@ -50,7 +50,7 @@ var AddTargetCmd = base.Cmd{ if !util.ExactlyOneSet(serverIDOrName, labelSelector, ipAddr) { return fmt.Errorf("--server, --label-selector, and --ip are mutually exclusive") } - if loadBalancer, _, err = s.LoadBalancer().Get(s, idOrName); err != nil { + if loadBalancer, _, err = s.Client().LoadBalancer().Get(s, idOrName); err != nil { return err } if loadBalancer == nil { @@ -59,14 +59,14 @@ var AddTargetCmd = base.Cmd{ switch { case serverIDOrName != "": - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return err } if server == nil { return fmt.Errorf("server not found: %s", serverIDOrName) } - action, _, err = s.LoadBalancer().AddServerTarget(s, loadBalancer, hcloud.LoadBalancerAddServerTargetOpts{ + action, _, err = s.Client().LoadBalancer().AddServerTarget(s, loadBalancer, hcloud.LoadBalancerAddServerTargetOpts{ Server: server, UsePrivateIP: hcloud.Bool(usePrivateIP), }) @@ -74,7 +74,7 @@ var AddTargetCmd = base.Cmd{ return err } case labelSelector != "": - action, _, err = s.LoadBalancer().AddLabelSelectorTarget(s, loadBalancer, hcloud.LoadBalancerAddLabelSelectorTargetOpts{ + action, _, err = s.Client().LoadBalancer().AddLabelSelectorTarget(s, loadBalancer, hcloud.LoadBalancerAddLabelSelectorTargetOpts{ Selector: labelSelector, UsePrivateIP: hcloud.Bool(usePrivateIP), }) @@ -86,7 +86,7 @@ var AddTargetCmd = base.Cmd{ if ip == nil { return fmt.Errorf("invalid ip provided") } - action, _, err = s.LoadBalancer().AddIPTarget(s, loadBalancer, hcloud.LoadBalancerAddIPTargetOpts{ + action, _, err = s.Client().LoadBalancer().AddIPTarget(s, loadBalancer, hcloud.LoadBalancerAddIPTargetOpts{ IP: ip, }) if err != nil { diff --git a/internal/cmd/loadbalancer/attach_to_network.go b/internal/cmd/loadbalancer/attach_to_network.go index c400defc..63432446 100644 --- a/internal/cmd/loadbalancer/attach_to_network.go +++ b/internal/cmd/loadbalancer/attach_to_network.go @@ -33,7 +33,7 @@ var AttachToNetworkCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -42,7 +42,7 @@ var AttachToNetworkCmd = base.Cmd{ } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := s.Network().Get(s, networkIDOrName) + network, _, err := s.Client().Network().Get(s, networkIDOrName) if err != nil { return err } @@ -56,7 +56,7 @@ var AttachToNetworkCmd = base.Cmd{ Network: network, IP: ip, } - action, _, err := s.LoadBalancer().AttachToNetwork(s, loadBalancer, opts) + action, _, err := s.Client().LoadBalancer().AttachToNetwork(s, loadBalancer, opts) if err != nil { return err diff --git a/internal/cmd/loadbalancer/change_algorithm.go b/internal/cmd/loadbalancer/change_algorithm.go index 89223024..75507729 100644 --- a/internal/cmd/loadbalancer/change_algorithm.go +++ b/internal/cmd/loadbalancer/change_algorithm.go @@ -35,7 +35,7 @@ var ChangeAlgorithmCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] algorithm, _ := cmd.Flags().GetString("algorithm-type") - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -43,7 +43,7 @@ var ChangeAlgorithmCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - action, _, err := s.LoadBalancer().ChangeAlgorithm(s, loadBalancer, hcloud.LoadBalancerChangeAlgorithmOpts{Type: hcloud.LoadBalancerAlgorithmType(algorithm)}) + action, _, err := s.Client().LoadBalancer().ChangeAlgorithm(s, loadBalancer, hcloud.LoadBalancerChangeAlgorithmOpts{Type: hcloud.LoadBalancerAlgorithmType(algorithm)}) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/change_type.go b/internal/cmd/loadbalancer/change_type.go index b32b8fa9..0a589ce5 100644 --- a/internal/cmd/loadbalancer/change_type.go +++ b/internal/cmd/loadbalancer/change_type.go @@ -28,7 +28,7 @@ var ChangeTypeCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -37,7 +37,7 @@ var ChangeTypeCmd = base.Cmd{ } loadBalancerTypeIDOrName := args[1] - loadBalancerType, _, err := s.LoadBalancerType().Get(s, loadBalancerTypeIDOrName) + loadBalancerType, _, err := s.Client().LoadBalancerType().Get(s, loadBalancerTypeIDOrName) if err != nil { return err } @@ -48,7 +48,7 @@ var ChangeTypeCmd = base.Cmd{ opts := hcloud.LoadBalancerChangeTypeOpts{ LoadBalancerType: loadBalancerType, } - action, _, err := s.LoadBalancer().ChangeType(s, loadBalancer, opts) + action, _, err := s.Client().LoadBalancer().ChangeType(s, loadBalancer, opts) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/create.go b/internal/cmd/loadbalancer/create.go index 924f981a..59d338b6 100644 --- a/internal/cmd/loadbalancer/create.go +++ b/internal/cmd/loadbalancer/create.go @@ -76,7 +76,7 @@ var CreateCmd = base.CreateCmd{ if location != "" { createOpts.Location = &hcloud.Location{Name: location} } - result, _, err := s.LoadBalancer().Create(s, createOpts) + result, _, err := s.Client().LoadBalancer().Create(s, createOpts) if err != nil { return nil, nil, err } @@ -84,7 +84,7 @@ var CreateCmd = base.CreateCmd{ if err := s.ActionProgress(cmd, s, result.Action); err != nil { return nil, nil, err } - loadBalancer, _, err := s.LoadBalancer().GetByID(s, result.LoadBalancer.ID) + loadBalancer, _, err := s.Client().LoadBalancer().GetByID(s, result.LoadBalancer.ID) if err != nil { return nil, nil, err } diff --git a/internal/cmd/loadbalancer/delete.go b/internal/cmd/loadbalancer/delete.go index 6f665739..de5a1816 100644 --- a/internal/cmd/loadbalancer/delete.go +++ b/internal/cmd/loadbalancer/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.LoadBalancer().Get(s, idOrName) + return s.Client().LoadBalancer().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { loadBalancer := resource.(*hcloud.LoadBalancer) - if _, err := s.LoadBalancer().Delete(s, loadBalancer); err != nil { + if _, err := s.Client().LoadBalancer().Delete(s, loadBalancer); err != nil { return err } return nil diff --git a/internal/cmd/loadbalancer/delete_service.go b/internal/cmd/loadbalancer/delete_service.go index 5cd15965..4300c33e 100644 --- a/internal/cmd/loadbalancer/delete_service.go +++ b/internal/cmd/loadbalancer/delete_service.go @@ -29,14 +29,14 @@ var DeleteServiceCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { listenPort, _ := cmd.Flags().GetInt("listen-port") idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } if loadBalancer == nil { return fmt.Errorf("Load Balancer not found: %s", idOrName) } - _, _, err = s.LoadBalancer().DeleteService(s, loadBalancer, listenPort) + _, _, err = s.Client().LoadBalancer().DeleteService(s, loadBalancer, listenPort) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/describe.go b/internal/cmd/loadbalancer/describe.go index ecd30312..92041e6a 100644 --- a/internal/cmd/loadbalancer/describe.go +++ b/internal/cmd/loadbalancer/describe.go @@ -19,7 +19,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "load_balancers", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - lb, _, err := s.LoadBalancer().Get(s, idOrName) + lb, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -45,7 +45,7 @@ var DescribeCmd = base.DescribeCmd{ if len(loadBalancer.PrivateNet) > 0 { for _, n := range loadBalancer.PrivateNet { cmd.Printf(" - ID:\t\t\t%d\n", n.Network.ID) - cmd.Printf(" Name:\t\t%s\n", s.Network().Name(n.Network.ID)) + cmd.Printf(" Name:\t\t%s\n", s.Client().Network().Name(n.Network.ID)) cmd.Printf(" IP:\t\t\t%s\n", n.IP.String()) } } else { @@ -110,7 +110,7 @@ var DescribeCmd = base.DescribeCmd{ case hcloud.LoadBalancerTargetTypeServer: cmd.Printf(" Server:\n") cmd.Printf(" ID:\t\t\t%d\n", target.Server.Server.ID) - cmd.Printf(" Name:\t\t\t%s\n", s.Server().ServerName(target.Server.Server.ID)) + cmd.Printf(" Name:\t\t\t%s\n", s.Client().Server().ServerName(target.Server.Server.ID)) cmd.Printf(" Use Private IP:\t\t%s\n", util.YesNo(target.UsePrivateIP)) cmd.Printf(" Status:\n") for _, healthStatus := range target.HealthStatus { diff --git a/internal/cmd/loadbalancer/detach_from_network.go b/internal/cmd/loadbalancer/detach_from_network.go index 1249c12e..18d405ab 100644 --- a/internal/cmd/loadbalancer/detach_from_network.go +++ b/internal/cmd/loadbalancer/detach_from_network.go @@ -29,7 +29,7 @@ var DetachFromNetworkCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -37,7 +37,7 @@ var DetachFromNetworkCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := s.Network().Get(s, networkIDOrName) + network, _, err := s.Client().Network().Get(s, networkIDOrName) if err != nil { return err } @@ -48,7 +48,7 @@ var DetachFromNetworkCmd = base.Cmd{ opts := hcloud.LoadBalancerDetachFromNetworkOpts{ Network: network, } - action, _, err := s.LoadBalancer().DetachFromNetwork(s, loadBalancer, opts) + action, _, err := s.Client().LoadBalancer().DetachFromNetwork(s, loadBalancer, opts) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/disable_protection.go b/internal/cmd/loadbalancer/disable_protection.go index 5de5c68a..c22c17d7 100644 --- a/internal/cmd/loadbalancer/disable_protection.go +++ b/internal/cmd/loadbalancer/disable_protection.go @@ -27,7 +27,7 @@ var DisableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/disable_public_interface.go b/internal/cmd/loadbalancer/disable_public_interface.go index ff4d87de..ac69db54 100644 --- a/internal/cmd/loadbalancer/disable_public_interface.go +++ b/internal/cmd/loadbalancer/disable_public_interface.go @@ -24,7 +24,7 @@ var DisablePublicInterfaceCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var DisablePublicInterfaceCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - action, _, err := s.LoadBalancer().DisablePublicInterface(s, loadBalancer) + action, _, err := s.Client().LoadBalancer().DisablePublicInterface(s, loadBalancer) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/enable_protection.go b/internal/cmd/loadbalancer/enable_protection.go index a436315c..470e504e 100644 --- a/internal/cmd/loadbalancer/enable_protection.go +++ b/internal/cmd/loadbalancer/enable_protection.go @@ -40,7 +40,7 @@ func changeProtection(s state.State, cmd *cobra.Command, return nil } - action, _, err := s.LoadBalancer().ChangeProtection(s, loadBalancer, opts) + action, _, err := s.Client().LoadBalancer().ChangeProtection(s, loadBalancer, opts) if err != nil { return err } @@ -73,7 +73,7 @@ var EnableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/enable_public_interface.go b/internal/cmd/loadbalancer/enable_public_interface.go index 580051ed..dc09f6a8 100644 --- a/internal/cmd/loadbalancer/enable_public_interface.go +++ b/internal/cmd/loadbalancer/enable_public_interface.go @@ -24,7 +24,7 @@ var EnablePublicInterfaceCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var EnablePublicInterfaceCmd = base.Cmd{ return fmt.Errorf("Load Balancer not found: %s", idOrName) } - action, _, err := s.LoadBalancer().EnablePublicInterface(s, loadBalancer) + action, _, err := s.Client().LoadBalancer().EnablePublicInterface(s, loadBalancer) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/labels.go b/internal/cmd/loadbalancer/labels.go index 10b6e9ca..c7c19d74 100644 --- a/internal/cmd/loadbalancer/labels.go +++ b/internal/cmd/loadbalancer/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.LoadBalancer().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.LoadBalancerUpdateOpts{ Labels: labels, } - _, _, err := s.LoadBalancer().Update(s, &hcloud.LoadBalancer{ID: id}, opts) + _, _, err := s.Client().LoadBalancer().Update(s, &hcloud.LoadBalancer{ID: id}, opts) return err }, } diff --git a/internal/cmd/loadbalancer/list.go b/internal/cmd/loadbalancer/list.go index 058f2a4e..226d8a6d 100644 --- a/internal/cmd/loadbalancer/list.go +++ b/internal/cmd/loadbalancer/list.go @@ -24,7 +24,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - loadBalancers, err := s.LoadBalancer().AllWithOpts(s, opts) + loadBalancers, err := s.Client().LoadBalancer().AllWithOpts(s, opts) var resources []interface{} for _, r := range loadBalancers { diff --git a/internal/cmd/loadbalancer/metrics.go b/internal/cmd/loadbalancer/metrics.go index f6986d02..4ca0c378 100644 --- a/internal/cmd/loadbalancer/metrics.go +++ b/internal/cmd/loadbalancer/metrics.go @@ -52,7 +52,7 @@ var MetricsCmd = base.Cmd{ outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] - LoadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + LoadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -88,7 +88,7 @@ var MetricsCmd = base.Cmd{ } } - m, resp, err := s.LoadBalancer().GetMetrics(s, LoadBalancer, hcloud.LoadBalancerGetMetricsOpts{ + m, resp, err := s.Client().LoadBalancer().GetMetrics(s, LoadBalancer, hcloud.LoadBalancerGetMetricsOpts{ Types: metricTypes, Start: startTime, End: endTime, diff --git a/internal/cmd/loadbalancer/remove_target.go b/internal/cmd/loadbalancer/remove_target.go index 34a4dcdb..4c13d83f 100644 --- a/internal/cmd/loadbalancer/remove_target.go +++ b/internal/cmd/loadbalancer/remove_target.go @@ -47,7 +47,7 @@ var RemoveTargetCmd = base.Cmd{ idOrName := args[0] - loadBalancer, _, err = s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err = s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -60,19 +60,19 @@ var RemoveTargetCmd = base.Cmd{ } switch { case serverIDOrName != "": - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return err } if server == nil { return fmt.Errorf("server not found: %s", serverIDOrName) } - action, _, err = s.LoadBalancer().RemoveServerTarget(s, loadBalancer, server) + action, _, err = s.Client().LoadBalancer().RemoveServerTarget(s, loadBalancer, server) if err != nil { return err } case labelSelector != "": - action, _, err = s.LoadBalancer().RemoveLabelSelectorTarget(s, loadBalancer, labelSelector) + action, _, err = s.Client().LoadBalancer().RemoveLabelSelectorTarget(s, loadBalancer, labelSelector) if err != nil { return err } @@ -81,7 +81,7 @@ var RemoveTargetCmd = base.Cmd{ if ip == nil { return fmt.Errorf("invalid ip provided") } - if action, _, err = s.LoadBalancer().RemoveIPTarget(s, loadBalancer, ip); err != nil { + if action, _, err = s.Client().LoadBalancer().RemoveIPTarget(s, loadBalancer, ip); err != nil { return err } default: diff --git a/internal/cmd/loadbalancer/set_rdns.go b/internal/cmd/loadbalancer/set_rdns.go index 69ab5ff7..5ba4947d 100644 --- a/internal/cmd/loadbalancer/set_rdns.go +++ b/internal/cmd/loadbalancer/set_rdns.go @@ -16,7 +16,7 @@ var SetRDNSCmd = base.SetRdnsCmd{ ShortDescription: "Change reverse DNS of a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.LoadBalancer().Get(s, idOrName) + return s.Client().LoadBalancer().Get(s, idOrName) }, GetDefaultIP: func(resource interface{}) net.IP { loadBalancer := resource.(*hcloud.LoadBalancer) diff --git a/internal/cmd/loadbalancer/update.go b/internal/cmd/loadbalancer/update.go index 9ce08e03..4a7a2372 100644 --- a/internal/cmd/loadbalancer/update.go +++ b/internal/cmd/loadbalancer/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a Load Balancer", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancer().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.LoadBalancer().Get(s, idOrName) + return s.Client().LoadBalancer().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "LoadBalancer name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.LoadBalancerUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.LoadBalancer().Update(s, floatingIP, updOpts) + _, _, err := s.Client().LoadBalancer().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/loadbalancer/update_service.go b/internal/cmd/loadbalancer/update_service.go index b5b622fd..6b3a57d6 100644 --- a/internal/cmd/loadbalancer/update_service.go +++ b/internal/cmd/loadbalancer/update_service.go @@ -56,7 +56,7 @@ var UpdateServiceCmd = base.Cmd{ idOrName := args[0] listenPort, _ := cmd.Flags().GetInt("listen-port") - loadBalancer, _, err := s.LoadBalancer().Get(s, idOrName) + loadBalancer, _, err := s.Client().LoadBalancer().Get(s, idOrName) if err != nil { return err } @@ -159,7 +159,7 @@ var UpdateServiceCmd = base.Cmd{ } } - action, _, err := s.LoadBalancer().UpdateService(s, loadBalancer, listenPort, opts) + action, _, err := s.Client().LoadBalancer().UpdateService(s, loadBalancer, listenPort, opts) if err != nil { return err } diff --git a/internal/cmd/loadbalancertype/describe.go b/internal/cmd/loadbalancertype/describe.go index 1123d8cf..3c32fc8f 100644 --- a/internal/cmd/loadbalancertype/describe.go +++ b/internal/cmd/loadbalancertype/describe.go @@ -16,7 +16,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "load_balancer_types", NameSuggestions: func(c hcapi2.Client) func() []string { return c.LoadBalancerType().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - lbt, _, err := s.LoadBalancerType().Get(s, idOrName) + lbt, _, err := s.Client().LoadBalancerType().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/loadbalancertype/list.go b/internal/cmd/loadbalancertype/list.go index a6ccc6b7..20fff54a 100644 --- a/internal/cmd/loadbalancertype/list.go +++ b/internal/cmd/loadbalancertype/list.go @@ -22,7 +22,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - loadBalancerTypes, err := s.LoadBalancerType().AllWithOpts(s, opts) + loadBalancerTypes, err := s.Client().LoadBalancerType().AllWithOpts(s, opts) var resources []interface{} for _, r := range loadBalancerTypes { diff --git a/internal/cmd/location/describe.go b/internal/cmd/location/describe.go index 643620f2..f6ede55d 100644 --- a/internal/cmd/location/describe.go +++ b/internal/cmd/location/describe.go @@ -17,7 +17,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "locations", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Location().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - l, _, err := s.Location().Get(s, idOrName) + l, _, err := s.Client().Location().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/location/list.go b/internal/cmd/location/list.go index 4f34c695..25f2b14f 100644 --- a/internal/cmd/location/list.go +++ b/internal/cmd/location/list.go @@ -21,7 +21,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - locations, err := s.Location().AllWithOpts(s, opts) + locations, err := s.Client().Location().AllWithOpts(s, opts) var resources []interface{} for _, n := range locations { diff --git a/internal/cmd/network/add_route.go b/internal/cmd/network/add_route.go index cc2bf236..599a9f41 100644 --- a/internal/cmd/network/add_route.go +++ b/internal/cmd/network/add_route.go @@ -37,7 +37,7 @@ var AddRouteCmd = base.Cmd{ destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } @@ -51,7 +51,7 @@ var AddRouteCmd = base.Cmd{ Destination: &destination, }, } - action, _, err := s.Network().AddRoute(s, network, opts) + action, _, err := s.Client().Network().AddRoute(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/add_subnet.go b/internal/cmd/network/add_subnet.go index dc057ead..0ebab456 100644 --- a/internal/cmd/network/add_subnet.go +++ b/internal/cmd/network/add_subnet.go @@ -44,7 +44,7 @@ var AddSubnetCmd = base.Cmd{ vSwitchID, _ := cmd.Flags().GetInt64("vswitch-id") idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } @@ -66,7 +66,7 @@ var AddSubnetCmd = base.Cmd{ opts := hcloud.NetworkAddSubnetOpts{ Subnet: subnet, } - action, _, err := s.Network().AddSubnet(s, network, opts) + action, _, err := s.Client().Network().AddSubnet(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/change_ip_range.go b/internal/cmd/network/change_ip_range.go index 3db9ac7d..186cf2a1 100644 --- a/internal/cmd/network/change_ip_range.go +++ b/internal/cmd/network/change_ip_range.go @@ -31,7 +31,7 @@ var ChangeIPRangeCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } @@ -44,7 +44,7 @@ var ChangeIPRangeCmd = base.Cmd{ IPRange: &ipRange, } - action, _, err := s.Network().ChangeIPRange(s, network, opts) + action, _, err := s.Client().Network().ChangeIPRange(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/create.go b/internal/cmd/network/create.go index 40f825d8..b99ae525 100644 --- a/internal/cmd/network/create.go +++ b/internal/cmd/network/create.go @@ -54,7 +54,7 @@ var CreateCmd = base.CreateCmd{ ExposeRoutesToVSwitch: exposeRoutesToVSwitch, } - network, _, err := s.Network().Create(s, createOpts) + network, _, err := s.Client().Network().Create(s, createOpts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/network/delete.go b/internal/cmd/network/delete.go index e565219d..1507ab1b 100644 --- a/internal/cmd/network/delete.go +++ b/internal/cmd/network/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a network", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Network().Get(s, idOrName) + return s.Client().Network().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { network := resource.(*hcloud.Network) - if _, err := s.Network().Delete(s, network); err != nil { + if _, err := s.Client().Network().Delete(s, network); err != nil { return err } return nil diff --git a/internal/cmd/network/describe.go b/internal/cmd/network/describe.go index 0e075ec1..56115de5 100644 --- a/internal/cmd/network/describe.go +++ b/internal/cmd/network/describe.go @@ -19,7 +19,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "networks", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - n, _, err := s.Network().Get(s, idOrName) + n, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/network/disable_protection.go b/internal/cmd/network/disable_protection.go index ffe3f2ae..7ad0ef26 100644 --- a/internal/cmd/network/disable_protection.go +++ b/internal/cmd/network/disable_protection.go @@ -27,7 +27,7 @@ var DisableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/network/enable_protection.go b/internal/cmd/network/enable_protection.go index 67c4b53e..ec5d3640 100644 --- a/internal/cmd/network/enable_protection.go +++ b/internal/cmd/network/enable_protection.go @@ -40,7 +40,7 @@ func changeProtection(s state.State, cmd *cobra.Command, return nil } - action, _, err := s.Network().ChangeProtection(s, network, opts) + action, _, err := s.Client().Network().ChangeProtection(s, network, opts) if err != nil { return err } @@ -73,7 +73,7 @@ var EnableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/network/expose-routes-to-vswitch.go b/internal/cmd/network/expose-routes-to-vswitch.go index e9635322..413ec357 100644 --- a/internal/cmd/network/expose-routes-to-vswitch.go +++ b/internal/cmd/network/expose-routes-to-vswitch.go @@ -30,7 +30,7 @@ var ExposeRoutesToVSwitchCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } @@ -43,7 +43,7 @@ var ExposeRoutesToVSwitchCmd = base.Cmd{ ExposeRoutesToVSwitch: hcloud.Ptr(!disable), } - _, _, err = s.Network().Update(s, network, opts) + _, _, err = s.Client().Network().Update(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/labels.go b/internal/cmd/network/labels.go index f7f7d493..415d70ae 100644 --- a/internal/cmd/network/labels.go +++ b/internal/cmd/network/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Network().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.NetworkUpdateOpts{ Labels: labels, } - _, _, err := s.Network().Update(s, &hcloud.Network{ID: id}, opts) + _, _, err := s.Client().Network().Update(s, &hcloud.Network{ID: id}, opts) return err }, } diff --git a/internal/cmd/network/list.go b/internal/cmd/network/list.go index ee01ea6d..2d9c5449 100644 --- a/internal/cmd/network/list.go +++ b/internal/cmd/network/list.go @@ -26,7 +26,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - networks, err := s.Network().AllWithOpts(s, opts) + networks, err := s.Client().Network().AllWithOpts(s, opts) var resources []interface{} for _, n := range networks { diff --git a/internal/cmd/network/remove_route.go b/internal/cmd/network/remove_route.go index 282bd38b..7c17d2e5 100644 --- a/internal/cmd/network/remove_route.go +++ b/internal/cmd/network/remove_route.go @@ -36,7 +36,7 @@ var RemoveRouteCmd = base.Cmd{ gateway, _ := cmd.Flags().GetIP("gateway") destination, _ := cmd.Flags().GetIPNet("destination") idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } @@ -50,7 +50,7 @@ var RemoveRouteCmd = base.Cmd{ Destination: &destination, }, } - action, _, err := s.Network().DeleteRoute(s, network, opts) + action, _, err := s.Client().Network().DeleteRoute(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/remove_subnet.go b/internal/cmd/network/remove_subnet.go index 2110716a..b081e89a 100644 --- a/internal/cmd/network/remove_subnet.go +++ b/internal/cmd/network/remove_subnet.go @@ -30,7 +30,7 @@ var RemoveSubnetCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { ipRange, _ := cmd.Flags().GetIPNet("ip-range") idOrName := args[0] - network, _, err := s.Network().Get(s, idOrName) + network, _, err := s.Client().Network().Get(s, idOrName) if err != nil { return err } @@ -43,7 +43,7 @@ var RemoveSubnetCmd = base.Cmd{ IPRange: &ipRange, }, } - action, _, err := s.Network().DeleteSubnet(s, network, opts) + action, _, err := s.Client().Network().DeleteSubnet(s, network, opts) if err != nil { return err } diff --git a/internal/cmd/network/update.go b/internal/cmd/network/update.go index 01533110..8cebafe7 100644 --- a/internal/cmd/network/update.go +++ b/internal/cmd/network/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a Network.\n\nTo enable or disable exposing routes to the vSwitch connection you can use the subcommand \"hcloud network expose-routes-to-vswitch\".", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Network().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Network().Get(s, idOrName) + return s.Client().Network().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Network name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.NetworkUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.Network().Update(s, floatingIP, updOpts) + _, _, err := s.Client().Network().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/placementgroup/create.go b/internal/cmd/placementgroup/create.go index 3af6e5ce..dea1e2f2 100644 --- a/internal/cmd/placementgroup/create.go +++ b/internal/cmd/placementgroup/create.go @@ -36,7 +36,7 @@ var CreateCmd = base.CreateCmd{ Type: hcloud.PlacementGroupType(placementGroupType), } - result, _, err := s.PlacementGroup().Create(s, opts) + result, _, err := s.Client().PlacementGroup().Create(s, opts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/placementgroup/delete.go b/internal/cmd/placementgroup/delete.go index 77734369..13b52512 100644 --- a/internal/cmd/placementgroup/delete.go +++ b/internal/cmd/placementgroup/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a placement group", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.PlacementGroup().Get(s, idOrName) + return s.Client().PlacementGroup().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { placementGroup := resource.(*hcloud.PlacementGroup) - if _, err := s.PlacementGroup().Delete(s, placementGroup); err != nil { + if _, err := s.Client().PlacementGroup().Delete(s, placementGroup); err != nil { return err } return nil diff --git a/internal/cmd/placementgroup/describe.go b/internal/cmd/placementgroup/describe.go index e4b6239a..81abf85c 100644 --- a/internal/cmd/placementgroup/describe.go +++ b/internal/cmd/placementgroup/describe.go @@ -18,7 +18,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "placement_groups", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - pg, _, err := s.PlacementGroup().Get(s, idOrName) + pg, _, err := s.Client().PlacementGroup().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -43,7 +43,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Print("Servers:\n") for _, serverID := range placementGroup.Servers { cmd.Printf(" - Server ID:\t\t%d\n", serverID) - cmd.Printf(" Server Name:\t%s\n", s.Server().ServerName(serverID)) + cmd.Printf(" Server Name:\t%s\n", s.Client().Server().ServerName(serverID)) } cmd.Printf("Type:\t\t%s\n", placementGroup.Type) diff --git a/internal/cmd/placementgroup/labels.go b/internal/cmd/placementgroup/labels.go index 919f2eda..fc6dfe2a 100644 --- a/internal/cmd/placementgroup/labels.go +++ b/internal/cmd/placementgroup/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PlacementGroup().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - placementGroup, _, err := s.PlacementGroup().Get(s, idOrName) + placementGroup, _, err := s.Client().PlacementGroup().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.PlacementGroupUpdateOpts{ Labels: labels, } - _, _, err := s.PlacementGroup().Update(s, &hcloud.PlacementGroup{ID: id}, opts) + _, _, err := s.Client().PlacementGroup().Update(s, &hcloud.PlacementGroup{ID: id}, opts) return err }, } diff --git a/internal/cmd/placementgroup/list.go b/internal/cmd/placementgroup/list.go index f2114cc8..487b620b 100644 --- a/internal/cmd/placementgroup/list.go +++ b/internal/cmd/placementgroup/list.go @@ -25,7 +25,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - placementGroups, err := s.PlacementGroup().AllWithOpts(s, opts) + placementGroups, err := s.Client().PlacementGroup().AllWithOpts(s, opts) var resources []interface{} for _, n := range placementGroups { diff --git a/internal/cmd/placementgroup/update.go b/internal/cmd/placementgroup/update.go index 515b02b6..fae341d3 100644 --- a/internal/cmd/placementgroup/update.go +++ b/internal/cmd/placementgroup/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a placement group", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PlacementGroup().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.PlacementGroup().Get(s, idOrName) + return s.Client().PlacementGroup().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Placement group name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.PlacementGroupUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.PlacementGroup().Update(s, placementGroup, updOpts) + _, _, err := s.Client().PlacementGroup().Update(s, placementGroup, updOpts) if err != nil { return err } diff --git a/internal/cmd/primaryip/assign.go b/internal/cmd/primaryip/assign.go index 6169af33..35e674f8 100644 --- a/internal/cmd/primaryip/assign.go +++ b/internal/cmd/primaryip/assign.go @@ -31,7 +31,7 @@ var AssignCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) + primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -41,7 +41,7 @@ var AssignCmd = base.Cmd{ serverIDOrName, _ := cmd.Flags().GetString("server") - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return err } @@ -54,7 +54,7 @@ var AssignCmd = base.Cmd{ AssigneeID: server.ID, } - action, _, err := s.PrimaryIP().Assign(s, opts) + action, _, err := s.Client().PrimaryIP().Assign(s, opts) if err != nil { return err } diff --git a/internal/cmd/primaryip/changedns.go b/internal/cmd/primaryip/changedns.go index af2a38f7..c7ab9af1 100644 --- a/internal/cmd/primaryip/changedns.go +++ b/internal/cmd/primaryip/changedns.go @@ -32,7 +32,7 @@ var ChangeDNSCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) + primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -48,7 +48,7 @@ var ChangeDNSCmd = base.Cmd{ IP: ip, } - action, _, err := s.PrimaryIP().ChangeDNSPtr(s, opts) + action, _, err := s.Client().PrimaryIP().ChangeDNSPtr(s, opts) if err != nil { return err } diff --git a/internal/cmd/primaryip/create.go b/internal/cmd/primaryip/create.go index 35f68953..4813173b 100644 --- a/internal/cmd/primaryip/create.go +++ b/internal/cmd/primaryip/create.go @@ -61,7 +61,7 @@ var CreateCmd = base.CreateCmd{ createOpts.AssigneeID = &assigneeID } - result, _, err := s.PrimaryIP().Create(s, createOpts) + result, _, err := s.Client().PrimaryIP().Create(s, createOpts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/primaryip/delete.go b/internal/cmd/primaryip/delete.go index e8102745..87caf3c8 100644 --- a/internal/cmd/primaryip/delete.go +++ b/internal/cmd/primaryip/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a Primary IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.PrimaryIP().Get(s, idOrName) + return s.Client().PrimaryIP().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { primaryIP := resource.(*hcloud.PrimaryIP) - if _, err := s.PrimaryIP().Delete(s, primaryIP); err != nil { + if _, err := s.Client().PrimaryIP().Delete(s, primaryIP); err != nil { return err } return nil diff --git a/internal/cmd/primaryip/describe.go b/internal/cmd/primaryip/describe.go index fd689178..2c8008ea 100644 --- a/internal/cmd/primaryip/describe.go +++ b/internal/cmd/primaryip/describe.go @@ -18,7 +18,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "primary_ips", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - ip, _, err := s.PrimaryIP().Get(s, idOrName) + ip, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/primaryip/disable_protection.go b/internal/cmd/primaryip/disable_protection.go index de8678fb..272ae885 100644 --- a/internal/cmd/primaryip/disable_protection.go +++ b/internal/cmd/primaryip/disable_protection.go @@ -28,7 +28,7 @@ var DisableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) + primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/primaryip/enable_protection.go b/internal/cmd/primaryip/enable_protection.go index 592a9c51..4e146feb 100644 --- a/internal/cmd/primaryip/enable_protection.go +++ b/internal/cmd/primaryip/enable_protection.go @@ -38,7 +38,7 @@ func changeProtection(s state.State, cmd *cobra.Command, opts.ID = primaryIp.ID - action, _, err := s.PrimaryIP().ChangeProtection(s, opts) + action, _, err := s.Client().PrimaryIP().ChangeProtection(s, opts) if err != nil { return err } @@ -72,7 +72,7 @@ var EnableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) + primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/primaryip/labels.go b/internal/cmd/primaryip/labels.go index fb8adaec..5a84f92d 100644 --- a/internal/cmd/primaryip/labels.go +++ b/internal/cmd/primaryip/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.PrimaryIP().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) + primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.PrimaryIPUpdateOpts{ Labels: &labels, } - _, _, err := s.PrimaryIP().Update(s, &hcloud.PrimaryIP{ID: id}, opts) + _, _, err := s.Client().PrimaryIP().Update(s, &hcloud.PrimaryIP{ID: id}, opts) return err }, } diff --git a/internal/cmd/primaryip/list.go b/internal/cmd/primaryip/list.go index 55427791..5996d042 100644 --- a/internal/cmd/primaryip/list.go +++ b/internal/cmd/primaryip/list.go @@ -26,7 +26,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - primaryips, err := s.PrimaryIP().AllWithOpts(s, opts) + primaryips, err := s.Client().PrimaryIP().AllWithOpts(s, opts) var resources []interface{} for _, n := range primaryips { diff --git a/internal/cmd/primaryip/unassign.go b/internal/cmd/primaryip/unassign.go index d0a88281..95d3a0ff 100644 --- a/internal/cmd/primaryip/unassign.go +++ b/internal/cmd/primaryip/unassign.go @@ -27,7 +27,7 @@ var UnAssignCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - primaryIP, _, err := s.PrimaryIP().Get(s, idOrName) + primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) if err != nil { return err } @@ -35,7 +35,7 @@ var UnAssignCmd = base.Cmd{ return fmt.Errorf("Primary IP not found: %v", idOrName) } - action, _, err := s.PrimaryIP().Unassign(s, primaryIP.ID) + action, _, err := s.Client().PrimaryIP().Unassign(s, primaryIP.ID) if err != nil { return err } diff --git a/internal/cmd/primaryip/update.go b/internal/cmd/primaryip/update.go index 94220a46..5cb81bd7 100644 --- a/internal/cmd/primaryip/update.go +++ b/internal/cmd/primaryip/update.go @@ -15,7 +15,7 @@ var updateCmd = base.UpdateCmd{ ShortDescription: "Update a Primary IP", NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.PrimaryIP().Get(s, idOrName) + return s.Client().PrimaryIP().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Primary IP name") @@ -32,7 +32,7 @@ var updateCmd = base.UpdateCmd{ updOpts.AutoDelete = hcloud.Ptr(autoDelete) } - _, _, err := s.PrimaryIP().Update(s, primaryIP, updOpts) + _, _, err := s.Client().PrimaryIP().Update(s, primaryIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/server/add_to_placement_group.go b/internal/cmd/server/add_to_placement_group.go index 81637865..fbd54303 100644 --- a/internal/cmd/server/add_to_placement_group.go +++ b/internal/cmd/server/add_to_placement_group.go @@ -28,7 +28,7 @@ var AddToPlacementGroupCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -37,7 +37,7 @@ var AddToPlacementGroupCmd = base.Cmd{ } placementGroupIDOrName, _ := cmd.Flags().GetString("placement-group") - placementGroup, _, err := s.PlacementGroup().Get(s, placementGroupIDOrName) + placementGroup, _, err := s.Client().PlacementGroup().Get(s, placementGroupIDOrName) if err != nil { return err } @@ -45,7 +45,7 @@ var AddToPlacementGroupCmd = base.Cmd{ return fmt.Errorf("placement group not found %s", placementGroupIDOrName) } - action, _, err := s.Server().AddToPlacementGroup(s, server, placementGroup) + action, _, err := s.Client().Server().AddToPlacementGroup(s, server, placementGroup) if err != nil { return err } diff --git a/internal/cmd/server/attach_iso.go b/internal/cmd/server/attach_iso.go index 7dec788a..624cac5c 100644 --- a/internal/cmd/server/attach_iso.go +++ b/internal/cmd/server/attach_iso.go @@ -28,7 +28,7 @@ var AttachISOCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -37,7 +37,7 @@ var AttachISOCmd = base.Cmd{ } isoIDOrName := args[1] - iso, _, err := s.ISO().Get(s, isoIDOrName) + iso, _, err := s.Client().ISO().Get(s, isoIDOrName) if err != nil { return err } @@ -51,7 +51,7 @@ var AttachISOCmd = base.Cmd{ return errors.New("failed to attach iso: iso has a different architecture than the server") } - action, _, err := s.Server().AttachISO(s, server, iso) + action, _, err := s.Client().Server().AttachISO(s, server, iso) if err != nil { return err } diff --git a/internal/cmd/server/attach_to_network.go b/internal/cmd/server/attach_to_network.go index 2c377cc8..15ec347d 100644 --- a/internal/cmd/server/attach_to_network.go +++ b/internal/cmd/server/attach_to_network.go @@ -35,7 +35,7 @@ var AttachToNetworkCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -44,7 +44,7 @@ var AttachToNetworkCmd = base.Cmd{ } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := s.Network().Get(s, networkIDOrName) + network, _, err := s.Client().Network().Get(s, networkIDOrName) if err != nil { return err } @@ -62,7 +62,7 @@ var AttachToNetworkCmd = base.Cmd{ for _, aliasIP := range aliasIPs { opts.AliasIPs = append(opts.AliasIPs, aliasIP) } - action, _, err := s.Server().AttachToNetwork(s, server, opts) + action, _, err := s.Client().Server().AttachToNetwork(s, server, opts) if err != nil { return err diff --git a/internal/cmd/server/change_alias_ips.go b/internal/cmd/server/change_alias_ips.go index 6caa9410..75565e0a 100644 --- a/internal/cmd/server/change_alias_ips.go +++ b/internal/cmd/server/change_alias_ips.go @@ -36,7 +36,7 @@ var ChangeAliasIPsCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { clear, _ := cmd.Flags().GetBool("clear") idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -45,7 +45,7 @@ var ChangeAliasIPsCmd = base.Cmd{ } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := s.Network().Get(s, networkIDOrName) + network, _, err := s.Client().Network().Get(s, networkIDOrName) if err != nil { return err } @@ -65,7 +65,7 @@ var ChangeAliasIPsCmd = base.Cmd{ opts.AliasIPs = append(opts.AliasIPs, net.ParseIP(aliasIP)) } } - action, _, err := s.Server().ChangeAliasIPs(s, server, opts) + action, _, err := s.Client().Server().ChangeAliasIPs(s, server, opts) if err != nil { return err diff --git a/internal/cmd/server/change_type.go b/internal/cmd/server/change_type.go index af95fc8b..ebe6c52d 100644 --- a/internal/cmd/server/change_type.go +++ b/internal/cmd/server/change_type.go @@ -31,7 +31,7 @@ var ChangeTypeCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -40,7 +40,7 @@ var ChangeTypeCmd = base.Cmd{ } serverTypeIDOrName := args[1] - serverType, _, err := s.ServerType().Get(s, serverTypeIDOrName) + serverType, _, err := s.Client().ServerType().Get(s, serverTypeIDOrName) if err != nil { return err } @@ -57,7 +57,7 @@ var ChangeTypeCmd = base.Cmd{ ServerType: serverType, UpgradeDisk: !keepDisk, } - action, _, err := s.Server().ChangeType(s, server, opts) + action, _, err := s.Client().Server().ChangeType(s, server, opts) if err != nil { return err } diff --git a/internal/cmd/server/create.go b/internal/cmd/server/create.go index bfa8724a..3573314d 100644 --- a/internal/cmd/server/create.go +++ b/internal/cmd/server/create.go @@ -102,7 +102,7 @@ var CreateCmd = base.CreateCmd{ return nil, nil, err } - result, _, err := s.Server().Create(s, createOpts) + result, _, err := s.Client().Server().Create(s, createOpts) if err != nil { return nil, nil, err } @@ -114,7 +114,7 @@ var CreateCmd = base.CreateCmd{ return nil, nil, err } - server, _, err := s.Server().GetByID(s, result.Server.ID) + server, _, err := s.Client().Server().GetByID(s, result.Server.ID) if err != nil { return nil, nil, err } @@ -127,7 +127,7 @@ var CreateCmd = base.CreateCmd{ enableBackup, _ := cmd.Flags().GetBool("enable-backup") if enableBackup { - action, _, err := s.Server().EnableBackup(s, server, "") + action, _, err := s.Client().Server().EnableBackup(s, server, "") if err != nil { return nil, nil, err } @@ -157,7 +157,7 @@ var CreateCmd = base.CreateCmd{ if len(server.PrivateNet) > 0 { var networks []string for _, network := range server.PrivateNet { - networks = append(networks, fmt.Sprintf("- %s (%s)", network.IP.String(), s.Network().Name(network.Network.ID))) + networks = append(networks, fmt.Sprintf("- %s (%s)", network.IP.String(), s.Client().Network().Name(network.Network.ID))) } cmd.Printf("Private Networks:\n\t%s\n", strings.Join(networks, "\n")) } @@ -261,7 +261,7 @@ func createOptsFromFlags( primaryIPv6IDorName, _ := flags.GetString("primary-ipv6") protection, _ := flags.GetStringSlice("enable-protection") - serverType, _, err := s.ServerType().Get(s, serverTypeName) + serverType, _, err := s.Client().ServerType().Get(s, serverTypeName) if err != nil { return } @@ -275,7 +275,7 @@ func createOptsFromFlags( } // Select correct image based on server type architecture - image, _, err := s.Image().GetForArchitecture(s, imageIDorName, serverType.Architecture) + image, _, err := s.Client().Image().GetForArchitecture(s, imageIDorName, serverType.Architecture) if err != nil { return } @@ -315,7 +315,7 @@ func createOptsFromFlags( } if primaryIPv4IDorName != "" { var primaryIPv4 *hcloud.PrimaryIP - primaryIPv4, _, err = s.PrimaryIP().Get(s, primaryIPv4IDorName) + primaryIPv4, _, err = s.Client().PrimaryIP().Get(s, primaryIPv4IDorName) if err != nil { return } @@ -327,7 +327,7 @@ func createOptsFromFlags( } if primaryIPv6IDorName != "" { var primaryIPv6 *hcloud.PrimaryIP - primaryIPv6, _, err = s.PrimaryIP().Get(s, primaryIPv6IDorName) + primaryIPv6, _, err = s.Client().PrimaryIP().Get(s, primaryIPv6IDorName) if err != nil { return } @@ -358,7 +358,7 @@ func createOptsFromFlags( for _, sshKeyIDOrName := range sshKeys { var sshKey *hcloud.SSHKey - sshKey, _, err = s.SSHKey().Get(s, sshKeyIDOrName) + sshKey, _, err = s.Client().SSHKey().Get(s, sshKeyIDOrName) if err != nil { return } @@ -378,7 +378,7 @@ func createOptsFromFlags( } for _, volumeIDOrName := range volumes { var volume *hcloud.Volume - volume, _, err = s.Volume().Get(s, volumeIDOrName) + volume, _, err = s.Client().Volume().Get(s, volumeIDOrName) if err != nil { return } @@ -391,7 +391,7 @@ func createOptsFromFlags( } for _, networkIDOrName := range networks { var network *hcloud.Network - network, _, err = s.Network().Get(s, networkIDOrName) + network, _, err = s.Client().Network().Get(s, networkIDOrName) if err != nil { return } @@ -404,7 +404,7 @@ func createOptsFromFlags( } for _, firewallIDOrName := range firewalls { var firewall *hcloud.Firewall - firewall, _, err = s.Firewall().Get(s, firewallIDOrName) + firewall, _, err = s.Client().Firewall().Get(s, firewallIDOrName) if err != nil { return } @@ -424,7 +424,7 @@ func createOptsFromFlags( } if placementGroupIDorName != "" { var placementGroup *hcloud.PlacementGroup - placementGroup, _, err = s.PlacementGroup().Get(s, placementGroupIDorName) + placementGroup, _, err = s.Client().PlacementGroup().Get(s, placementGroupIDorName) if err != nil { return } @@ -459,7 +459,7 @@ func getSSHKeyForFingerprint( err = fmt.Errorf("lookup SSH key by fingerprint: %v", err) return } - sshKey, _, err = s.SSHKey().GetByFingerprint(s, ssh.FingerprintLegacyMD5(publicKey)) + sshKey, _, err = s.Client().SSHKey().GetByFingerprint(s, ssh.FingerprintLegacyMD5(publicKey)) if err != nil { err = fmt.Errorf("lookup SSH key by fingerprint: %v", err) return diff --git a/internal/cmd/server/create_image.go b/internal/cmd/server/create_image.go index ac4f5489..cf4f8402 100644 --- a/internal/cmd/server/create_image.go +++ b/internal/cmd/server/create_image.go @@ -31,7 +31,7 @@ var CreateImageCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -55,7 +55,7 @@ var CreateImageCmd = base.Cmd{ Description: hcloud.Ptr(description), Labels: labels, } - result, _, err := s.Server().CreateImage(s, server, opts) + result, _, err := s.Client().Server().CreateImage(s, server, opts) if err != nil { return err } diff --git a/internal/cmd/server/delete.go b/internal/cmd/server/delete.go index 38d9c463..cb19f5e6 100644 --- a/internal/cmd/server/delete.go +++ b/internal/cmd/server/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Server().Get(s, idOrName) + return s.Client().Server().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { server := resource.(*hcloud.Server) - result, _, err := s.Server().DeleteWithResult(s, server) + result, _, err := s.Client().Server().DeleteWithResult(s, server) if err != nil { return err } diff --git a/internal/cmd/server/describe.go b/internal/cmd/server/describe.go index 9ebd3cbe..ec385bad 100644 --- a/internal/cmd/server/describe.go +++ b/internal/cmd/server/describe.go @@ -20,7 +20,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "servers", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - srv, _, err := s.Server().Get(s, idOrName) + srv, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -67,7 +67,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf(" Floating IPs:\n") if len(server.PublicNet.FloatingIPs) > 0 { for _, f := range server.PublicNet.FloatingIPs { - floatingIP, _, err := s.FloatingIP().GetByID(s, f.ID) + floatingIP, _, err := s.Client().FloatingIP().GetByID(s, f.ID) if err != nil { return fmt.Errorf("error fetching Floating IP: %v", err) } @@ -82,7 +82,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf("Private Net:\n") if len(server.PrivateNet) > 0 { for _, n := range server.PrivateNet { - network, _, err := s.Network().GetByID(s, n.Network.ID) + network, _, err := s.Client().Network().GetByID(s, n.Network.ID) if err != nil { return fmt.Errorf("error fetching network: %v", err) } @@ -106,7 +106,7 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf("Volumes:\n") if len(server.Volumes) > 0 { for _, v := range server.Volumes { - volume, _, err := s.Volume().GetByID(s, v.ID) + volume, _, err := s.Client().Volume().GetByID(s, v.ID) if err != nil { return fmt.Errorf("error fetching Volume: %v", err) } diff --git a/internal/cmd/server/detach_from_network.go b/internal/cmd/server/detach_from_network.go index a98f9ac8..8d24e570 100644 --- a/internal/cmd/server/detach_from_network.go +++ b/internal/cmd/server/detach_from_network.go @@ -30,7 +30,7 @@ var DetachFromNetworkCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -38,7 +38,7 @@ var DetachFromNetworkCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } networkIDOrName, _ := cmd.Flags().GetString("network") - network, _, err := s.Network().Get(s, networkIDOrName) + network, _, err := s.Client().Network().Get(s, networkIDOrName) if err != nil { return err } @@ -49,7 +49,7 @@ var DetachFromNetworkCmd = base.Cmd{ opts := hcloud.ServerDetachFromNetworkOpts{ Network: network, } - action, _, err := s.Server().DetachFromNetwork(s, server, opts) + action, _, err := s.Client().Server().DetachFromNetwork(s, server, opts) if err != nil { return err } diff --git a/internal/cmd/server/detach_iso.go b/internal/cmd/server/detach_iso.go index 85e2125d..ae780cb7 100644 --- a/internal/cmd/server/detach_iso.go +++ b/internal/cmd/server/detach_iso.go @@ -24,7 +24,7 @@ var DetachISOCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var DetachISOCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().DetachISO(s, server) + action, _, err := s.Client().Server().DetachISO(s, server) if err != nil { return err } diff --git a/internal/cmd/server/disable_backup.go b/internal/cmd/server/disable_backup.go index 9abc9dde..416436c4 100644 --- a/internal/cmd/server/disable_backup.go +++ b/internal/cmd/server/disable_backup.go @@ -24,7 +24,7 @@ var DisableBackupCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var DisableBackupCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().DisableBackup(s, server) + action, _, err := s.Client().Server().DisableBackup(s, server) if err != nil { return err } diff --git a/internal/cmd/server/disable_protection.go b/internal/cmd/server/disable_protection.go index c239fe2f..bbf8d633 100644 --- a/internal/cmd/server/disable_protection.go +++ b/internal/cmd/server/disable_protection.go @@ -27,7 +27,7 @@ var DisableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/server/disable_rescue.go b/internal/cmd/server/disable_rescue.go index 95adafb3..826a9d73 100644 --- a/internal/cmd/server/disable_rescue.go +++ b/internal/cmd/server/disable_rescue.go @@ -24,7 +24,7 @@ var DisableRescueCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var DisableRescueCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().DisableRescue(s, server) + action, _, err := s.Client().Server().DisableRescue(s, server) if err != nil { return err } diff --git a/internal/cmd/server/enable_backup.go b/internal/cmd/server/enable_backup.go index 5c9549b4..9dd96720 100644 --- a/internal/cmd/server/enable_backup.go +++ b/internal/cmd/server/enable_backup.go @@ -28,7 +28,7 @@ var EnableBackupCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -41,7 +41,7 @@ var EnableBackupCmd = base.Cmd{ cmd.Print("[WARN] The ability to specify a backup window when enabling backups has been removed. Ignoring flag.\n") } - action, _, err := s.Server().EnableBackup(s, server, "") + action, _, err := s.Client().Server().EnableBackup(s, server, "") if err != nil { return err } diff --git a/internal/cmd/server/enable_protection.go b/internal/cmd/server/enable_protection.go index 3e23bdc2..fbd4474a 100644 --- a/internal/cmd/server/enable_protection.go +++ b/internal/cmd/server/enable_protection.go @@ -42,7 +42,7 @@ func changeProtection(s state.State, cmd *cobra.Command, return nil } - action, _, err := s.Server().ChangeProtection(s, server, opts) + action, _, err := s.Client().Server().ChangeProtection(s, server, opts) if err != nil { return err } @@ -75,7 +75,7 @@ var EnableProtectionCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/server/enable_rescue.go b/internal/cmd/server/enable_rescue.go index af8e9e36..d75cfaf7 100644 --- a/internal/cmd/server/enable_rescue.go +++ b/internal/cmd/server/enable_rescue.go @@ -31,7 +31,7 @@ var EnableRescueCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -47,7 +47,7 @@ var EnableRescueCmd = base.Cmd{ sshKeys, _ := cmd.Flags().GetStringSlice("ssh-key") for _, sshKeyIDOrName := range sshKeys { - sshKey, _, err := s.SSHKey().Get(s, sshKeyIDOrName) + sshKey, _, err := s.Client().SSHKey().Get(s, sshKeyIDOrName) if err != nil { return err } @@ -57,7 +57,7 @@ var EnableRescueCmd = base.Cmd{ opts.SSHKeys = append(opts.SSHKeys, sshKey) } - result, _, err := s.Server().EnableRescue(s, server, opts) + result, _, err := s.Client().Server().EnableRescue(s, server, opts) if err != nil { return err } diff --git a/internal/cmd/server/ip.go b/internal/cmd/server/ip.go index ca36b088..36654969 100644 --- a/internal/cmd/server/ip.go +++ b/internal/cmd/server/ip.go @@ -27,7 +27,7 @@ var IPCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { ipv6, err := cmd.Flags().GetBool("ipv6") idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/server/labels.go b/internal/cmd/server/labels.go index 42c43ce2..ecb5ede0 100644 --- a/internal/cmd/server/labels.go +++ b/internal/cmd/server/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Server().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.ServerUpdateOpts{ Labels: labels, } - _, _, err := s.Server().Update(s, &hcloud.Server{ID: id}, opts) + _, _, err := s.Client().Server().Update(s, &hcloud.Server{ID: id}, opts) return err }, } diff --git a/internal/cmd/server/list.go b/internal/cmd/server/list.go index ada09fbd..4ae20539 100644 --- a/internal/cmd/server/list.go +++ b/internal/cmd/server/list.go @@ -60,7 +60,7 @@ var ListCmd = base.ListCmd{ } } } - servers, err := s.Server().AllWithOpts(s, opts) + servers, err := s.Client().Server().AllWithOpts(s, opts) var resources []interface{} for _, r := range servers { diff --git a/internal/cmd/server/metrics.go b/internal/cmd/server/metrics.go index 164eed69..240f647b 100644 --- a/internal/cmd/server/metrics.go +++ b/internal/cmd/server/metrics.go @@ -51,7 +51,7 @@ var MetricsCmd = base.Cmd{ outputFlags := output.FlagsForCommand(cmd) idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -87,7 +87,7 @@ var MetricsCmd = base.Cmd{ } } - m, resp, err := s.Server().GetMetrics(s, server, hcloud.ServerGetMetricsOpts{ + m, resp, err := s.Client().Server().GetMetrics(s, server, hcloud.ServerGetMetricsOpts{ Types: metricTypes, Start: startTime, End: endTime, diff --git a/internal/cmd/server/poweroff.go b/internal/cmd/server/poweroff.go index ddc75f6b..706889c1 100644 --- a/internal/cmd/server/poweroff.go +++ b/internal/cmd/server/poweroff.go @@ -24,7 +24,7 @@ var PoweroffCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var PoweroffCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().Poweroff(s, server) + action, _, err := s.Client().Server().Poweroff(s, server) if err != nil { return err } diff --git a/internal/cmd/server/poweron.go b/internal/cmd/server/poweron.go index 876b97fd..f3626893 100644 --- a/internal/cmd/server/poweron.go +++ b/internal/cmd/server/poweron.go @@ -24,7 +24,7 @@ var PoweronCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var PoweronCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().Poweron(s, server) + action, _, err := s.Client().Server().Poweron(s, server) if err != nil { return err } diff --git a/internal/cmd/server/reboot.go b/internal/cmd/server/reboot.go index 94add797..864fc843 100644 --- a/internal/cmd/server/reboot.go +++ b/internal/cmd/server/reboot.go @@ -24,7 +24,7 @@ var RebootCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var RebootCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().Reboot(s, server) + action, _, err := s.Client().Server().Reboot(s, server) if err != nil { return err } diff --git a/internal/cmd/server/rebuild.go b/internal/cmd/server/rebuild.go index 54b7bf60..d0da69b1 100644 --- a/internal/cmd/server/rebuild.go +++ b/internal/cmd/server/rebuild.go @@ -33,7 +33,7 @@ var RebuildCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { serverIDOrName := args[0] - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return err } @@ -43,7 +43,7 @@ var RebuildCmd = base.Cmd{ imageIDOrName, _ := cmd.Flags().GetString("image") // Select correct image based on server type architecture - image, _, err := s.Image().GetForArchitecture(s, imageIDOrName, server.ServerType.Architecture) + image, _, err := s.Client().Image().GetForArchitecture(s, imageIDOrName, server.ServerType.Architecture) if err != nil { return err } @@ -64,7 +64,7 @@ var RebuildCmd = base.Cmd{ opts := hcloud.ServerRebuildOpts{ Image: image, } - result, _, err := s.Server().RebuildWithResult(s, server, opts) + result, _, err := s.Client().Server().RebuildWithResult(s, server, opts) if err != nil { return err } diff --git a/internal/cmd/server/remove_from_placement_group.go b/internal/cmd/server/remove_from_placement_group.go index ee4f4138..38f175c1 100644 --- a/internal/cmd/server/remove_from_placement_group.go +++ b/internal/cmd/server/remove_from_placement_group.go @@ -25,7 +25,7 @@ var RemoveFromPlacementGroupCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -33,7 +33,7 @@ var RemoveFromPlacementGroupCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().RemoveFromPlacementGroup(s, server) + action, _, err := s.Client().Server().RemoveFromPlacementGroup(s, server) if err != nil { return err } diff --git a/internal/cmd/server/request_console.go b/internal/cmd/server/request_console.go index 9b8310e4..26fdb41f 100644 --- a/internal/cmd/server/request_console.go +++ b/internal/cmd/server/request_console.go @@ -29,7 +29,7 @@ var RequestConsoleCmd = base.Cmd{ Run: func(s state.State, cmd *cobra.Command, args []string) error { outOpts := output.FlagsForCommand(cmd) idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -37,7 +37,7 @@ var RequestConsoleCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - result, _, err := s.Server().RequestConsole(s, server) + result, _, err := s.Client().Server().RequestConsole(s, server) if err != nil { return err } diff --git a/internal/cmd/server/reset.go b/internal/cmd/server/reset.go index 6e77d5e3..f8760fec 100644 --- a/internal/cmd/server/reset.go +++ b/internal/cmd/server/reset.go @@ -24,7 +24,7 @@ var ResetCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var ResetCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().Reset(s, server) + action, _, err := s.Client().Server().Reset(s, server) if err != nil { return err } diff --git a/internal/cmd/server/reset_password.go b/internal/cmd/server/reset_password.go index ea7a8db9..ec45e337 100644 --- a/internal/cmd/server/reset_password.go +++ b/internal/cmd/server/reset_password.go @@ -24,7 +24,7 @@ var ResetPasswordCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -32,7 +32,7 @@ var ResetPasswordCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - result, _, err := s.Server().ResetPassword(s, server) + result, _, err := s.Client().Server().ResetPassword(s, server) if err != nil { return err } diff --git a/internal/cmd/server/set_rdns.go b/internal/cmd/server/set_rdns.go index 45b1ab5f..6ca0e777 100644 --- a/internal/cmd/server/set_rdns.go +++ b/internal/cmd/server/set_rdns.go @@ -16,7 +16,7 @@ var SetRDNSCmd = base.SetRdnsCmd{ ShortDescription: "Change reverse DNS of a Server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Server().Get(s, idOrName) + return s.Client().Server().Get(s, idOrName) }, GetDefaultIP: func(resource interface{}) net.IP { server := resource.(*hcloud.Server) diff --git a/internal/cmd/server/shutdown.go b/internal/cmd/server/shutdown.go index b7e11670..67a81c5a 100644 --- a/internal/cmd/server/shutdown.go +++ b/internal/cmd/server/shutdown.go @@ -43,7 +43,7 @@ var ShutdownCmd = base.Cmd{ timeout, _ := cmd.Flags().GetDuration("wait-timeout") idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } @@ -51,7 +51,7 @@ var ShutdownCmd = base.Cmd{ return fmt.Errorf("server not found: %s", idOrName) } - action, _, err := s.Server().Shutdown(s, server) + action, _, err := s.Client().Server().Shutdown(s, server) if err != nil { return err } @@ -82,7 +82,7 @@ var ShutdownCmd = base.Cmd{ errCh <- errors.New("failed to shut down server") return } - server, _, err = s.Server().GetByID(s, server.ID) + server, _, err = s.Client().Server().GetByID(s, server.ID) if err != nil { errCh <- err return diff --git a/internal/cmd/server/ssh.go b/internal/cmd/server/ssh.go index 62afab48..ee3ce6da 100644 --- a/internal/cmd/server/ssh.go +++ b/internal/cmd/server/ssh.go @@ -33,7 +33,7 @@ var SSHCmd = base.Cmd{ }, Run: func(s state.State, cmd *cobra.Command, args []string) error { idOrName := args[0] - server, _, err := s.Server().Get(s, idOrName) + server, _, err := s.Client().Server().Get(s, idOrName) if err != nil { return err } diff --git a/internal/cmd/server/update.go b/internal/cmd/server/update.go index d3802c72..d89ee2fb 100644 --- a/internal/cmd/server/update.go +++ b/internal/cmd/server/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a Server", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Server().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Server().Get(s, idOrName) + return s.Client().Server().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Server name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.ServerUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.Server().Update(s, floatingIP, updOpts) + _, _, err := s.Client().Server().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/servertype/describe.go b/internal/cmd/servertype/describe.go index 436b4551..b95a35dd 100644 --- a/internal/cmd/servertype/describe.go +++ b/internal/cmd/servertype/describe.go @@ -17,7 +17,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "server_types", NameSuggestions: func(c hcapi2.Client) func() []string { return c.ServerType().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - st, _, err := s.ServerType().Get(s, idOrName) + st, _, err := s.Client().ServerType().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/servertype/list.go b/internal/cmd/servertype/list.go index 79508301..d2ef199e 100644 --- a/internal/cmd/servertype/list.go +++ b/internal/cmd/servertype/list.go @@ -25,7 +25,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - servers, err := s.ServerType().AllWithOpts(s, opts) + servers, err := s.Client().ServerType().AllWithOpts(s, opts) var resources []interface{} for _, r := range servers { diff --git a/internal/cmd/sshkey/create.go b/internal/cmd/sshkey/create.go index 34f616c6..ad85d310 100644 --- a/internal/cmd/sshkey/create.go +++ b/internal/cmd/sshkey/create.go @@ -57,7 +57,7 @@ var CreateCmd = base.CreateCmd{ PublicKey: publicKey, Labels: labels, } - sshKey, _, err := s.SSHKey().Create(s, opts) + sshKey, _, err := s.Client().SSHKey().Create(s, opts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/sshkey/delete.go b/internal/cmd/sshkey/delete.go index 367ccd48..902be564 100644 --- a/internal/cmd/sshkey/delete.go +++ b/internal/cmd/sshkey/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a SSH Key", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.SSHKey().Get(s, idOrName) + return s.Client().SSHKey().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { sshKey := resource.(*hcloud.SSHKey) - if _, err := s.SSHKey().Delete(s, sshKey); err != nil { + if _, err := s.Client().SSHKey().Delete(s, sshKey); err != nil { return err } return nil diff --git a/internal/cmd/sshkey/describe.go b/internal/cmd/sshkey/describe.go index b29b241f..6b9d8e88 100644 --- a/internal/cmd/sshkey/describe.go +++ b/internal/cmd/sshkey/describe.go @@ -20,7 +20,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "ssh_keys", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - key, _, err := s.SSHKey().Get(s, idOrName) + key, _, err := s.Client().SSHKey().Get(s, idOrName) if err != nil { return nil, nil, err } diff --git a/internal/cmd/sshkey/labels.go b/internal/cmd/sshkey/labels.go index 52e86eab..3bfaf396 100644 --- a/internal/cmd/sshkey/labels.go +++ b/internal/cmd/sshkey/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.SSHKey().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - sshKey, _, err := s.SSHKey().Get(s, idOrName) + sshKey, _, err := s.Client().SSHKey().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.SSHKeyUpdateOpts{ Labels: labels, } - _, _, err := s.SSHKey().Update(s, &hcloud.SSHKey{ID: id}, opts) + _, _, err := s.Client().SSHKey().Update(s, &hcloud.SSHKey{ID: id}, opts) return err }, } diff --git a/internal/cmd/sshkey/list.go b/internal/cmd/sshkey/list.go index 67bf7aab..996d4030 100644 --- a/internal/cmd/sshkey/list.go +++ b/internal/cmd/sshkey/list.go @@ -24,7 +24,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - sshKeys, err := s.SSHKey().AllWithOpts(s, opts) + sshKeys, err := s.Client().SSHKey().AllWithOpts(s, opts) var resources []interface{} for _, n := range sshKeys { diff --git a/internal/cmd/sshkey/update.go b/internal/cmd/sshkey/update.go index a482dfc9..467c4f7b 100644 --- a/internal/cmd/sshkey/update.go +++ b/internal/cmd/sshkey/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a SSHKey", NameSuggestions: func(c hcapi2.Client) func() []string { return c.SSHKey().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.SSHKey().Get(s, idOrName) + return s.Client().SSHKey().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "SSH Key name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.SSHKeyUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.SSHKey().Update(s, floatingIP, updOpts) + _, _, err := s.Client().SSHKey().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/cmd/volume/attach.go b/internal/cmd/volume/attach.go index 2b9098dd..b3d589c0 100644 --- a/internal/cmd/volume/attach.go +++ b/internal/cmd/volume/attach.go @@ -30,7 +30,7 @@ var AttachCmd = base.Cmd{ return cmd }, Run: func(s state.State, cmd *cobra.Command, args []string) error { - volume, _, err := s.Volume().Get(s, args[0]) + volume, _, err := s.Client().Volume().Get(s, args[0]) if err != nil { return err } @@ -39,7 +39,7 @@ var AttachCmd = base.Cmd{ } serverIDOrName, _ := cmd.Flags().GetString("server") - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return err } @@ -47,7 +47,7 @@ var AttachCmd = base.Cmd{ return fmt.Errorf("server not found: %s", serverIDOrName) } automount, _ := cmd.Flags().GetBool("automount") - action, _, err := s.Volume().AttachWithOpts(s, volume, hcloud.VolumeAttachOpts{ + action, _, err := s.Client().Volume().AttachWithOpts(s, volume, hcloud.VolumeAttachOpts{ Server: server, Automount: &automount, }) diff --git a/internal/cmd/volume/create.go b/internal/cmd/volume/create.go index 651866a5..5d72be6b 100644 --- a/internal/cmd/volume/create.go +++ b/internal/cmd/volume/create.go @@ -77,7 +77,7 @@ var CreateCmd = base.CreateCmd{ } } if serverIDOrName != "" { - server, _, err := s.Server().Get(s, serverIDOrName) + server, _, err := s.Client().Server().Get(s, serverIDOrName) if err != nil { return nil, nil, err } @@ -93,7 +93,7 @@ var CreateCmd = base.CreateCmd{ createOpts.Format = &format } - result, _, err := s.Volume().Create(s, createOpts) + result, _, err := s.Client().Volume().Create(s, createOpts) if err != nil { return nil, nil, err } diff --git a/internal/cmd/volume/delete.go b/internal/cmd/volume/delete.go index 0e8cf77d..9aa9b1c3 100644 --- a/internal/cmd/volume/delete.go +++ b/internal/cmd/volume/delete.go @@ -14,11 +14,11 @@ var DeleteCmd = base.DeleteCmd{ ShortDescription: "Delete a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Volume().Get(s, idOrName) + return s.Client().Volume().Get(s, idOrName) }, Delete: func(s state.State, cmd *cobra.Command, resource interface{}) error { volume := resource.(*hcloud.Volume) - if _, err := s.Volume().Delete(s, volume); err != nil { + if _, err := s.Client().Volume().Delete(s, volume); err != nil { return err } return nil diff --git a/internal/cmd/volume/describe.go b/internal/cmd/volume/describe.go index b4ccebc8..44c292d2 100644 --- a/internal/cmd/volume/describe.go +++ b/internal/cmd/volume/describe.go @@ -18,7 +18,7 @@ var DescribeCmd = base.DescribeCmd{ JSONKeyGetByName: "volumes", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, interface{}, error) { - v, _, err := s.Volume().Get(s, idOrName) + v, _, err := s.Client().Volume().Get(s, idOrName) if err != nil { return nil, nil, err } @@ -42,7 +42,7 @@ var DescribeCmd = base.DescribeCmd{ if volume.Server != nil { cmd.Printf("Server:\n") cmd.Printf(" ID:\t\t%d\n", volume.Server.ID) - cmd.Printf(" Name:\t\t%s\n", s.Server().ServerName(volume.Server.ID)) + cmd.Printf(" Name:\t\t%s\n", s.Client().Server().ServerName(volume.Server.ID)) } else { cmd.Print("Server:\n Not attached\n") } diff --git a/internal/cmd/volume/detach.go b/internal/cmd/volume/detach.go index 499c9de9..dff81363 100644 --- a/internal/cmd/volume/detach.go +++ b/internal/cmd/volume/detach.go @@ -23,7 +23,7 @@ var DetachCmd = base.Cmd{ } }, Run: func(s state.State, cmd *cobra.Command, args []string) error { - volume, _, err := s.Volume().Get(s, args[0]) + volume, _, err := s.Client().Volume().Get(s, args[0]) if err != nil { return err } @@ -31,7 +31,7 @@ var DetachCmd = base.Cmd{ return fmt.Errorf("volume not found: %s", args[0]) } - action, _, err := s.Volume().Detach(s, volume) + action, _, err := s.Client().Volume().Detach(s, volume) if err != nil { return err } diff --git a/internal/cmd/volume/disable_protection.go b/internal/cmd/volume/disable_protection.go index d60d0449..28ce5e68 100644 --- a/internal/cmd/volume/disable_protection.go +++ b/internal/cmd/volume/disable_protection.go @@ -26,7 +26,7 @@ var DisableProtectionCmd = base.Cmd{ } }, Run: func(s state.State, cmd *cobra.Command, args []string) error { - volume, _, err := s.Volume().Get(s, args[0]) + volume, _, err := s.Client().Volume().Get(s, args[0]) if err != nil { return err } diff --git a/internal/cmd/volume/enable_protection.go b/internal/cmd/volume/enable_protection.go index 207bb627..f0803114 100644 --- a/internal/cmd/volume/enable_protection.go +++ b/internal/cmd/volume/enable_protection.go @@ -40,7 +40,7 @@ func changeProtection(s state.State, cmd *cobra.Command, return nil } - action, _, err := s.Volume().ChangeProtection(s, volume, opts) + action, _, err := s.Client().Volume().ChangeProtection(s, volume, opts) if err != nil { return err } @@ -72,7 +72,7 @@ var EnableProtectionCmd = base.Cmd{ } }, Run: func(s state.State, cmd *cobra.Command, args []string) error { - volume, _, err := s.Volume().Get(s, args[0]) + volume, _, err := s.Client().Volume().Get(s, args[0]) if err != nil { return err } diff --git a/internal/cmd/volume/labels.go b/internal/cmd/volume/labels.go index 951823f6..57a8d972 100644 --- a/internal/cmd/volume/labels.go +++ b/internal/cmd/volume/labels.go @@ -16,7 +16,7 @@ var LabelCmds = base.LabelCmds{ NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, LabelKeySuggestions: func(c hcapi2.Client) func(idOrName string) []string { return c.Volume().LabelKeys }, FetchLabels: func(s state.State, idOrName string) (map[string]string, int64, error) { - volume, _, err := s.Volume().Get(s, idOrName) + volume, _, err := s.Client().Volume().Get(s, idOrName) if err != nil { return nil, 0, err } @@ -29,7 +29,7 @@ var LabelCmds = base.LabelCmds{ opts := hcloud.VolumeUpdateOpts{ Labels: labels, } - _, _, err := s.Volume().Update(s, &hcloud.Volume{ID: id}, opts) + _, _, err := s.Client().Volume().Update(s, &hcloud.Volume{ID: id}, opts) return err }, } diff --git a/internal/cmd/volume/list.go b/internal/cmd/volume/list.go index 050c44da..ab5885e7 100644 --- a/internal/cmd/volume/list.go +++ b/internal/cmd/volume/list.go @@ -26,7 +26,7 @@ var ListCmd = base.ListCmd{ if len(sorts) > 0 { opts.Sort = sorts } - volumes, err := s.Volume().AllWithOpts(s, opts) + volumes, err := s.Client().Volume().AllWithOpts(s, opts) var resources []interface{} for _, n := range volumes { diff --git a/internal/cmd/volume/resize.go b/internal/cmd/volume/resize.go index 5760ac90..869d3fc1 100644 --- a/internal/cmd/volume/resize.go +++ b/internal/cmd/volume/resize.go @@ -26,7 +26,7 @@ var ResizeCmd = base.Cmd{ return cmd }, Run: func(s state.State, cmd *cobra.Command, args []string) error { - volume, _, err := s.Volume().Get(s, args[0]) + volume, _, err := s.Client().Volume().Get(s, args[0]) if err != nil { return err } @@ -35,7 +35,7 @@ var ResizeCmd = base.Cmd{ } size, _ := cmd.Flags().GetInt("size") - action, _, err := s.Volume().Resize(s, volume, size) + action, _, err := s.Client().Volume().Resize(s, volume, size) if err != nil { return err } diff --git a/internal/cmd/volume/update.go b/internal/cmd/volume/update.go index 663fcc08..8b39917e 100644 --- a/internal/cmd/volume/update.go +++ b/internal/cmd/volume/update.go @@ -15,7 +15,7 @@ var UpdateCmd = base.UpdateCmd{ ShortDescription: "Update a Volume", NameSuggestions: func(c hcapi2.Client) func() []string { return c.Volume().Names }, Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) { - return s.Volume().Get(s, idOrName) + return s.Client().Volume().Get(s, idOrName) }, DefineFlags: func(cmd *cobra.Command) { cmd.Flags().String("name", "", "Volume name") @@ -25,7 +25,7 @@ var UpdateCmd = base.UpdateCmd{ updOpts := hcloud.VolumeUpdateOpts{ Name: flags["name"].String(), } - _, _, err := s.Volume().Update(s, floatingIP, updOpts) + _, _, err := s.Client().Volume().Update(s, floatingIP, updOpts) if err != nil { return err } diff --git a/internal/state/config.go b/internal/state/config.go index eabba389..6155e8d1 100644 --- a/internal/state/config.go +++ b/internal/state/config.go @@ -2,6 +2,8 @@ package state import ( "fmt" + "os" + "path/filepath" toml "github.com/pelletier/go-toml/v2" ) @@ -9,6 +11,7 @@ import ( var DefaultConfigPath string type Config struct { + Path string Endpoint string ActiveContext *ConfigContext Contexts []*ConfigContext @@ -19,6 +22,20 @@ type ConfigContext struct { Token string } +func (config *Config) Write() error { + data, err := MarshalConfig(config) + if err != nil { + return err + } + if err := os.MkdirAll(filepath.Dir(config.Path), 0777); err != nil { + return err + } + if err := os.WriteFile(config.Path, data, 0600); err != nil { + return err + } + return nil +} + func (config *Config) ContextNames() []string { if len(config.Contexts) == 0 { return nil diff --git a/internal/state/helpers.go b/internal/state/helpers.go index 4b41254b..4dcde3d5 100644 --- a/internal/state/helpers.go +++ b/internal/state/helpers.go @@ -60,7 +60,7 @@ func (c *state) ActionsProgresses(cmd *cobra.Command, ctx context.Context, actio } func (c *state) EnsureToken(_ *cobra.Command, _ []string) error { - if c.Token == "" { + if c.token == "" { return errors.New("no active context or token (see `hcloud context --help`)") } return nil diff --git a/internal/state/state.go b/internal/state/state.go index 650eb7cd..9ea01c9b 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -3,10 +3,8 @@ package state import ( "context" "fmt" - "io/ioutil" "log" "os" - "path/filepath" "time" "github.com/hetznercloud/cli/internal/hcapi2" @@ -15,110 +13,125 @@ import ( ) type State interface { + context.Context + TokenEnsurer ActionWaiter - context.Context - hcapi2.Client - WriteConfig() error + Client() hcapi2.Client Config() *Config } type state struct { context.Context - hcapi2.Client - - Token string - Endpoint string - ConfigPath string - Debug bool - DebugFilePath string - hcloudClient *hcloud.Client - config *Config + token string + endpoint string + debug bool + debugFilePath string + client hcapi2.Client + hcloudClient *hcloud.Client + config *Config } func New() (State, error) { - s := &state{ - Context: context.Background(), - config: &Config{}, - ConfigPath: DefaultConfigPath, + configPath := os.Getenv("HCLOUD_CONFIG") + if configPath == "" { + configPath = DefaultConfigPath } - if p := os.Getenv("HCLOUD_CONFIG"); p != "" { - s.ConfigPath = p + + cfg, err := readConfig(configPath) + if err != nil { + return nil, fmt.Errorf("unable to read config file %q: %s\n", configPath, err) } - if err := s.readConfig(); err != nil { - return nil, fmt.Errorf("unable to read config file %q: %s\n", s.ConfigPath, err) + + var ( + token string + endpoint string + ) + if cfg.ActiveContext != nil { + token = cfg.ActiveContext.Token + } + if cfg.Endpoint != "" { + endpoint = cfg.Endpoint + } + + s := &state{ + Context: context.Background(), + config: cfg, + token: token, + endpoint: endpoint, } + s.readEnv() s.hcloudClient = s.newClient() - s.Client = hcapi2.NewClient(s.hcloudClient) + s.client = hcapi2.NewClient(s.hcloudClient) return s, nil } +func (c *state) Client() hcapi2.Client { + return c.client +} + func (c *state) Config() *Config { return c.config } func (c *state) readEnv() { if s := os.Getenv("HCLOUD_TOKEN"); s != "" { - c.Token = s + c.token = s } if s := os.Getenv("HCLOUD_ENDPOINT"); s != "" { - c.Endpoint = s + c.endpoint = s } if s := os.Getenv("HCLOUD_DEBUG"); s != "" { - c.Debug = true + c.debug = true } if s := os.Getenv("HCLOUD_DEBUG_FILE"); s != "" { - c.DebugFilePath = s + c.debugFilePath = s } if s := os.Getenv("HCLOUD_CONTEXT"); s != "" && c.config != nil { if cfgCtx := c.config.ContextByName(s); cfgCtx != nil { c.config.ActiveContext = cfgCtx - c.Token = cfgCtx.Token + c.token = cfgCtx.Token } else { log.Printf("warning: context %q specified in HCLOUD_CONTEXT does not exist\n", s) } } } -func (c *state) readConfig() error { - _, err := os.Stat(c.ConfigPath) +func readConfig(path string) (*Config, error) { + _, err := os.Stat(path) if err != nil && !os.IsNotExist(err) { - return err + return nil, err } - data, err := os.ReadFile(c.ConfigPath) + data, err := os.ReadFile(path) if err != nil { - return err - } - if err = UnmarshalConfig(c.config, data); err != nil { - return err + return nil, err } - if c.config.ActiveContext != nil { - c.Token = c.config.ActiveContext.Token - } - if c.config.Endpoint != "" { - c.Endpoint = c.config.Endpoint + cfg := &Config{Path: path} + if err = UnmarshalConfig(cfg, data); err != nil { + return nil, err } - return nil + + return cfg, nil } func (c *state) newClient() *hcloud.Client { opts := []hcloud.ClientOption{ - hcloud.WithToken(c.Token), + hcloud.WithToken(c.token), hcloud.WithApplication("hcloud-cli", version.Version), } - if c.Endpoint != "" { - opts = append(opts, hcloud.WithEndpoint(c.Endpoint)) + if c.endpoint != "" { + opts = append(opts, hcloud.WithEndpoint(c.endpoint)) } - if c.Debug { - if c.DebugFilePath == "" { + if c.debug { + if c.debugFilePath == "" { opts = append(opts, hcloud.WithDebugWriter(os.Stderr)) } else { - writer, _ := os.Create(c.DebugFilePath) + writer, _ := os.Create(c.debugFilePath) opts = append(opts, hcloud.WithDebugWriter(writer)) } } @@ -130,21 +143,3 @@ func (c *state) newClient() *hcloud.Client { } return hcloud.NewClient(opts...) } - -func (c *state) WriteConfig() error { - if c.config == nil { - return nil - } - - data, err := MarshalConfig(c.config) - if err != nil { - return err - } - if err := os.MkdirAll(filepath.Dir(c.ConfigPath), 0777); err != nil { - return err - } - if err := ioutil.WriteFile(c.ConfigPath, data, 0600); err != nil { - return err - } - return nil -} diff --git a/internal/testutil/fixture.go b/internal/testutil/fixture.go index b504e026..f21b66e1 100644 --- a/internal/testutil/fixture.go +++ b/internal/testutil/fixture.go @@ -59,26 +59,31 @@ func (f *Fixture) Finish() { // fixtureState implements state.State for testing purposes. type fixtureState struct { + context.Context state.TokenEnsurer state.ActionWaiter - context.Context - hcapi2.Client + + client hcapi2.Client } -func (fixtureState) WriteConfig() error { +func (*fixtureState) WriteConfig() error { return errors.New("not implemented") } -func (fixtureState) Config() *state.Config { +func (s *fixtureState) Client() hcapi2.Client { + return s.client +} + +func (*fixtureState) Config() *state.Config { return nil } // State returns a state.State implementation for testing purposes. func (f *Fixture) State() state.State { - return fixtureState{ + return &fixtureState{ + Context: context.Background(), TokenEnsurer: f.TokenEnsurer, ActionWaiter: f.ActionWaiter, - Context: context.Background(), - Client: f.Client, + client: f.Client, } } From 6d862d246ab39b66333251a4ca6beb95d47ca89a Mon Sep 17 00:00:00 2001 From: pauhull <22707808+pauhull@users.noreply.github.com> Date: Thu, 4 Jan 2024 13:52:15 +0100 Subject: [PATCH 3/5] Fix error if cli.toml does not exist --- internal/state/state.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/state/state.go b/internal/state/state.go index 9ea01c9b..9fde1837 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -101,9 +101,14 @@ func (c *state) readEnv() { } func readConfig(path string) (*Config, error) { + cfg := &Config{Path: path} + _, err := os.Stat(path) - if err != nil && !os.IsNotExist(err) { - return nil, err + if err != nil { + if os.IsNotExist(err) { + err = nil + } + return cfg, err } data, err := os.ReadFile(path) @@ -111,7 +116,6 @@ func readConfig(path string) (*Config, error) { return nil, err } - cfg := &Config{Path: path} if err = UnmarshalConfig(cfg, data); err != nil { return nil, err } From 6597bd5fe7c10b10bbbe860795530db30e8626b0 Mon Sep 17 00:00:00 2001 From: pauhull <22707808+pauhull@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:15:30 +0100 Subject: [PATCH 4/5] Move config creation logic into main.go --- cmd/hcloud/main.go | 12 ++++++- internal/state/config.go | 2 -- internal/state/config_unix.go | 8 ++--- internal/state/config_windows.go | 6 ++-- internal/state/state.go | 59 +++++++++++++------------------- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/cmd/hcloud/main.go b/cmd/hcloud/main.go index 17afdf1c..ddb41d37 100644 --- a/cmd/hcloud/main.go +++ b/cmd/hcloud/main.go @@ -15,7 +15,17 @@ func init() { } func main() { - s, err := state.New() + configPath := os.Getenv("HCLOUD_CONFIG") + if configPath == "" { + configPath = state.DefaultConfigPath() + } + + cfg, err := state.ReadConfig(configPath) + if err != nil { + log.Fatalf("unable to read config file %q: %s\n", configPath, err) + } + + s, err := state.New(cfg) if err != nil { log.Fatalln(err) } diff --git a/internal/state/config.go b/internal/state/config.go index 6155e8d1..09b42fac 100644 --- a/internal/state/config.go +++ b/internal/state/config.go @@ -8,8 +8,6 @@ import ( toml "github.com/pelletier/go-toml/v2" ) -var DefaultConfigPath string - type Config struct { Path string Endpoint string diff --git a/internal/state/config_unix.go b/internal/state/config_unix.go index b302a902..291cc725 100644 --- a/internal/state/config_unix.go +++ b/internal/state/config_unix.go @@ -1,5 +1,4 @@ //go:build !windows -// +build !windows package state @@ -8,12 +7,13 @@ import ( "path/filepath" ) -func init() { +func DefaultConfigPath() string { usr, err := user.Current() if err != nil { - return + return "" } if usr.HomeDir != "" { - DefaultConfigPath = filepath.Join(usr.HomeDir, ".config", "hcloud", "cli.toml") + return filepath.Join(usr.HomeDir, ".config", "hcloud", "cli.toml") } + return "" } diff --git a/internal/state/config_windows.go b/internal/state/config_windows.go index 985d0285..6dc7b2c6 100644 --- a/internal/state/config_windows.go +++ b/internal/state/config_windows.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package state @@ -8,9 +7,10 @@ import ( "path/filepath" ) -func init() { +func DefaultConfigPath() string { dir := os.Getenv("APPDATA") if dir != "" { - DefaultConfigPath = filepath.Join(dir, "hcloud", "cli.toml") + return filepath.Join(dir, "hcloud", "cli.toml") } + return "" } diff --git a/internal/state/state.go b/internal/state/state.go index 9fde1837..06619360 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -2,7 +2,6 @@ package state import ( "context" - "fmt" "log" "os" "time" @@ -34,17 +33,7 @@ type state struct { config *Config } -func New() (State, error) { - configPath := os.Getenv("HCLOUD_CONFIG") - if configPath == "" { - configPath = DefaultConfigPath - } - - cfg, err := readConfig(configPath) - if err != nil { - return nil, fmt.Errorf("unable to read config file %q: %s\n", configPath, err) - } - +func New(cfg *Config) (State, error) { var ( token string endpoint string @@ -69,6 +58,29 @@ func New() (State, error) { return s, nil } +func ReadConfig(path string) (*Config, error) { + cfg := &Config{Path: path} + + _, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + err = nil + } + return cfg, err + } + + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + if err = UnmarshalConfig(cfg, data); err != nil { + return nil, err + } + + return cfg, nil +} + func (c *state) Client() hcapi2.Client { return c.client } @@ -100,29 +112,6 @@ func (c *state) readEnv() { } } -func readConfig(path string) (*Config, error) { - cfg := &Config{Path: path} - - _, err := os.Stat(path) - if err != nil { - if os.IsNotExist(err) { - err = nil - } - return cfg, err - } - - data, err := os.ReadFile(path) - if err != nil { - return nil, err - } - - if err = UnmarshalConfig(cfg, data); err != nil { - return nil, err - } - - return cfg, nil -} - func (c *state) newClient() *hcloud.Client { opts := []hcloud.ClientOption{ hcloud.WithToken(c.token), From f5a7833ca9541d1ebb6a8d5a5eb6a7afc46926d2 Mon Sep 17 00:00:00 2001 From: pauhull <22707808+pauhull@users.noreply.github.com> Date: Thu, 4 Jan 2024 16:37:07 +0100 Subject: [PATCH 5/5] change file not exist error handling Co-authored-by: Jonas L. --- internal/state/state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/state/state.go b/internal/state/state.go index 06619360..574d05c6 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -64,7 +64,7 @@ func ReadConfig(path string) (*Config, error) { _, err := os.Stat(path) if err != nil { if os.IsNotExist(err) { - err = nil + return cfg, nil } return cfg, err }