From 110dad327075e8967060a7b2d15df2c0fb816aa8 Mon Sep 17 00:00:00 2001 From: pauhull Date: Wed, 3 Jan 2024 13:12:54 +0100 Subject: [PATCH] refactor: make root command mockable --- cmd/hcloud/main.go | 2 +- internal/cli/root.go | 43 ++++++----- internal/cmd/all/all.go | 8 +- internal/cmd/certificate/certificate.go | 20 +++-- internal/cmd/completion/completion.go | 4 +- internal/cmd/datacenter/datacenter.go | 9 ++- internal/cmd/firewall/firewall.go | 30 ++++---- internal/cmd/floatingip/floatingip.go | 30 ++++---- internal/cmd/image/image.go | 22 +++--- internal/cmd/iso/iso.go | 9 ++- internal/cmd/loadbalancer/load_balancer.go | 50 +++++++------ .../loadbalancertype/load_balancer_type.go | 10 ++- internal/cmd/location/location.go | 9 ++- internal/cmd/network/network.go | 36 +++++---- internal/cmd/placementgroup/placementgroup.go | 20 +++-- internal/cmd/primaryip/primaryip.go | 30 ++++---- internal/cmd/server/server.go | 74 ++++++++++--------- internal/cmd/servertype/server_type.go | 10 ++- internal/cmd/sshkey/sshkey.go | 20 +++-- internal/cmd/version/version.go | 7 +- internal/cmd/volume/volume.go | 29 ++++---- 21 files changed, 268 insertions(+), 204 deletions(-) diff --git a/cmd/hcloud/main.go b/cmd/hcloud/main.go index 2ab664b9e..8d894943d 100644 --- a/cmd/hcloud/main.go +++ b/cmd/hcloud/main.go @@ -34,7 +34,7 @@ func main() { cliState.ReadEnv() apiClient := hcapi2.NewClient(cliState.Client()) - rootCommand := cli.NewRootCommand(cliState, apiClient) + rootCommand := cli.NewRootCommand(cliState, cliState.Context, apiClient, cliState, cliState) if err := rootCommand.Execute(); err != nil { log.Fatalln(err) } diff --git a/internal/cli/root.go b/internal/cli/root.go index b3ca29c31..daad445cd 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -1,6 +1,7 @@ package cli import ( + stdcontext "context" "os" "time" @@ -30,7 +31,9 @@ import ( "github.com/hetznercloud/cli/internal/state" ) -func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command { +func NewRootCommand( + state *state.State, ctx stdcontext.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "hcloud", Short: "Hetzner Cloud CLI", @@ -41,26 +44,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), + all.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + floatingip.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + image.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + server.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + sshkey.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + version.NewCommand(), + completion.NewCommand(), + servertype.NewCommand(ctx, client, tokenEnsurer), 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), + datacenter.NewCommand(ctx, client, tokenEnsurer), + location.NewCommand(ctx, client, tokenEnsurer), + iso.NewCommand(ctx, client, tokenEnsurer), + volume.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + network.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + loadbalancer.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + loadbalancertype.NewCommand(ctx, client, tokenEnsurer), + certificate.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + firewall.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + placementgroup.NewCommand(ctx, client, tokenEnsurer, actionWaiter), + primaryip.NewCommand(ctx, client, tokenEnsurer, actionWaiter), ) 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 0dba942fe..13b30cd66 100644 --- a/internal/cmd/all/all.go +++ b/internal/cmd/all/all.go @@ -1,13 +1,17 @@ package all import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "all", Short: "Commands that apply to all resources", @@ -16,7 +20,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { DisableFlagsInUseLine: true, } cmd.AddCommand( - listCmd.CobraCommand(cli.Context, client, cli, cli), + listCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), ) return cmd } diff --git a/internal/cmd/certificate/certificate.go b/internal/cmd/certificate/certificate.go index 550d3c45b..be987d581 100644 --- a/internal/cmd/certificate/certificate.go +++ b/internal/cmd/certificate/certificate.go @@ -1,13 +1,17 @@ package certificate import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "certificate", Short: "Manage certificates", @@ -16,13 +20,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(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), ) return cmd diff --git a/internal/cmd/completion/completion.go b/internal/cmd/completion/completion.go index 791462fc9..e2714e06d 100644 --- a/internal/cmd/completion/completion.go +++ b/internal/cmd/completion/completion.go @@ -5,8 +5,6 @@ import ( "os" "github.com/spf13/cobra" - - "github.com/hetznercloud/cli/internal/state" ) // const ( @@ -31,7 +29,7 @@ import ( // source <(hcloud completion zsh)` // ) -func NewCommand(cli *state.State) *cobra.Command { +func NewCommand() *cobra.Command { cmd := &cobra.Command{ Use: "completion [FLAGS] SHELL", Short: "Output shell completion code for the specified shell", diff --git a/internal/cmd/datacenter/datacenter.go b/internal/cmd/datacenter/datacenter.go index 523da491d..f9b466675 100644 --- a/internal/cmd/datacenter/datacenter.go +++ b/internal/cmd/datacenter/datacenter.go @@ -2,12 +2,15 @@ package datacenter import ( "github.com/spf13/cobra" + "golang.org/x/net/context" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, +) *cobra.Command { cmd := &cobra.Command{ Use: "datacenter", Short: "Manage datacenters", @@ -16,8 +19,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/firewall/firewall.go b/internal/cmd/firewall/firewall.go index c04b7f598..e6ba22c3e 100644 --- a/internal/cmd/firewall/firewall.go +++ b/internal/cmd/firewall/firewall.go @@ -1,13 +1,17 @@ package firewall import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "firewall", Short: "Manage Firewalls", @@ -16,18 +20,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + ReplaceRulesCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AddRuleCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DeleteRuleCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ApplyToResourceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RemoveFromResourceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/floatingip/floatingip.go b/internal/cmd/floatingip/floatingip.go index 464c38d0f..63aec0fea 100644 --- a/internal/cmd/floatingip/floatingip.go +++ b/internal/cmd/floatingip/floatingip.go @@ -1,13 +1,17 @@ package floatingip import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "floating-ip", Short: "Manage Floating IPs", @@ -16,18 +20,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(ctx, client, tokenEnsurer), + ListCmd.CobraCommand(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + AssignCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UnassignCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), + SetRDNSCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), ) return cmd } diff --git a/internal/cmd/image/image.go b/internal/cmd/image/image.go index a1c1214db..002aa5676 100644 --- a/internal/cmd/image/image.go +++ b/internal/cmd/image/image.go @@ -1,13 +1,17 @@ package image import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "image", Short: "Manage images", @@ -16,14 +20,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(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/iso/iso.go b/internal/cmd/iso/iso.go index f649f1e25..23b020e61 100644 --- a/internal/cmd/iso/iso.go +++ b/internal/cmd/iso/iso.go @@ -2,12 +2,15 @@ package iso import ( "github.com/spf13/cobra" + "golang.org/x/net/context" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, +) *cobra.Command { cmd := &cobra.Command{ Use: "iso", Short: "Manage ISOs", @@ -16,8 +19,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/loadbalancer/load_balancer.go b/internal/cmd/loadbalancer/load_balancer.go index d4d8bf579..ebf78efea 100644 --- a/internal/cmd/loadbalancer/load_balancer.go +++ b/internal/cmd/loadbalancer/load_balancer.go @@ -1,13 +1,17 @@ package loadbalancer import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer", Short: "Manage Load Balancers", @@ -17,28 +21,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(ctx, client, tokenEnsurer, actionWaiter), + ListCmd.CobraCommand(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), + AddTargetCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RemoveTargetCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ChangeAlgorithmCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateServiceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DeleteServiceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AddServiceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AttachToNetworkCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DetachFromNetworkCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnablePublicInterfaceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisablePublicInterfaceCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ChangeTypeCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + MetricsCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + SetRDNSCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), ) return cmd } diff --git a/internal/cmd/loadbalancertype/load_balancer_type.go b/internal/cmd/loadbalancertype/load_balancer_type.go index 223544f1a..e5ed7b739 100644 --- a/internal/cmd/loadbalancertype/load_balancer_type.go +++ b/internal/cmd/loadbalancertype/load_balancer_type.go @@ -1,13 +1,17 @@ package loadbalancertype import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, +) *cobra.Command { cmd := &cobra.Command{ Use: "load-balancer-type", Short: "Manage Load Balancer types", @@ -16,8 +20,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(ctx, client, tokenEnsurer), + ListCmd.CobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/location/location.go b/internal/cmd/location/location.go index 5df721117..418e5c8ec 100644 --- a/internal/cmd/location/location.go +++ b/internal/cmd/location/location.go @@ -2,12 +2,15 @@ package location import ( "github.com/spf13/cobra" + "golang.org/x/net/context" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, +) *cobra.Command { cmd := &cobra.Command{ Use: "location", Short: "Manage locations", @@ -16,8 +19,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/network/network.go b/internal/cmd/network/network.go index 6a2cb149d..960fb19ef 100644 --- a/internal/cmd/network/network.go +++ b/internal/cmd/network/network.go @@ -1,13 +1,17 @@ package network import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "network", Short: "Manage networks", @@ -16,21 +20,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ChangeIPRangeCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AddRouteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RemoveRouteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AddSubnetCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RemoveSubnetCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ExposeRoutesToVSwitchCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), ) return cmd } diff --git a/internal/cmd/placementgroup/placementgroup.go b/internal/cmd/placementgroup/placementgroup.go index ded363315..949c2d71b 100644 --- a/internal/cmd/placementgroup/placementgroup.go +++ b/internal/cmd/placementgroup/placementgroup.go @@ -1,13 +1,17 @@ package placementgroup import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "placement-group", Short: "Manage Placement Groups", @@ -16,13 +20,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(ctx, client, tokenEnsurer, actionWaiter), + ListCmd.CobraCommand(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/primaryip/primaryip.go b/internal/cmd/primaryip/primaryip.go index ed80ea16e..2699be018 100644 --- a/internal/cmd/primaryip/primaryip.go +++ b/internal/cmd/primaryip/primaryip.go @@ -1,13 +1,17 @@ package primaryip import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "primary-ip", Short: "Manage Primary IPs", @@ -16,18 +20,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + updateCmd.CobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AssignCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UnAssignCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ChangeDNSCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go index e95b0e153..4f1c6cb53 100644 --- a/internal/cmd/server/server.go +++ b/internal/cmd/server/server.go @@ -1,13 +1,17 @@ package server import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "server", Short: "Manage servers", @@ -16,40 +20,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RebootCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + PoweronCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + PoweroffCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ResetCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ShutdownCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + CreateImageCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ResetPasswordCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableRescueCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableRescueCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AttachISOCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DetachISOCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + ChangeTypeCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RebuildCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableBackupCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableBackupCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + SSHCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), + SetRDNSCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AttachToNetworkCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DetachFromNetworkCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ChangeAliasIPsCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + IPCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RequestConsoleCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + MetricsCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + AddToPlacementGroupCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + RemoveFromPlacementGroupCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), ) return cmd } diff --git a/internal/cmd/servertype/server_type.go b/internal/cmd/servertype/server_type.go index ad8027094..e2d86ce82 100644 --- a/internal/cmd/servertype/server_type.go +++ b/internal/cmd/servertype/server_type.go @@ -1,13 +1,17 @@ package servertype import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, +) *cobra.Command { cmd := &cobra.Command{ Use: "server-type", Short: "Manage server types", @@ -16,8 +20,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(ctx, client, tokenEnsurer), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/sshkey/sshkey.go b/internal/cmd/sshkey/sshkey.go index 3168bda80..f59c3a63d 100644 --- a/internal/cmd/sshkey/sshkey.go +++ b/internal/cmd/sshkey/sshkey.go @@ -1,13 +1,17 @@ package sshkey import ( + "context" + "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( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "ssh-key", Short: "Manage SSH keys", @@ -16,13 +20,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(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), ) return cmd } diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index 4eea83ca9..de18cd495 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -3,22 +3,21 @@ package version import ( "github.com/spf13/cobra" - "github.com/hetznercloud/cli/internal/state" "github.com/hetznercloud/cli/internal/version" ) -func NewCommand(cli *state.State) *cobra.Command { +func NewCommand() *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/volume.go b/internal/cmd/volume/volume.go index ea37493cb..55788b377 100644 --- a/internal/cmd/volume/volume.go +++ b/internal/cmd/volume/volume.go @@ -2,12 +2,15 @@ package volume import ( "github.com/spf13/cobra" + "golang.org/x/net/context" "github.com/hetznercloud/cli/internal/hcapi2" "github.com/hetznercloud/cli/internal/state" ) -func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command { +func NewCommand( + ctx context.Context, client hcapi2.Client, tokenEnsurer state.TokenEnsurer, actionWaiter state.ActionWaiter, +) *cobra.Command { cmd := &cobra.Command{ Use: "volume", Short: "Manage Volumes", @@ -16,18 +19,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(ctx, client, tokenEnsurer), + CreateCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + UpdateCmd.CobraCommand(ctx, client, tokenEnsurer), + DeleteCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DescribeCmd.CobraCommand(ctx, client, tokenEnsurer), + AttachCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DetachCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + ResizeCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + EnableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + DisableProtectionCmd.CobraCommand(ctx, client, tokenEnsurer, actionWaiter), + LabelCmds.AddCobraCommand(ctx, client, tokenEnsurer), + LabelCmds.RemoveCobraCommand(ctx, client, tokenEnsurer), ) return cmd }