Skip to content

Commit

Permalink
refactor: make root command mockable
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 3, 2024
1 parent af9dec4 commit 110dad3
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 204 deletions.
2 changes: 1 addition & 1 deletion cmd/hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
43 changes: 23 additions & 20 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
stdcontext "context"
"os"
"time"

Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/all/all.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
}
20 changes: 12 additions & 8 deletions internal/cmd/certificate/certificate.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions internal/cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/state"
)

// const (
Expand All @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions internal/cmd/datacenter/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
30 changes: 17 additions & 13 deletions internal/cmd/firewall/firewall.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
}
30 changes: 17 additions & 13 deletions internal/cmd/floatingip/floatingip.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
}
22 changes: 13 additions & 9 deletions internal/cmd/image/image.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
}
9 changes: 6 additions & 3 deletions internal/cmd/iso/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
Loading

0 comments on commit 110dad3

Please sign in to comment.