Skip to content

Commit

Permalink
test: make state config mockable
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 10, 2024
1 parent a5e871d commit d89a322
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 227 deletions.
48 changes: 46 additions & 2 deletions cmd/hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,28 @@ import (
"os"

"github.com/hetznercloud/cli/internal/cli"
"github.com/hetznercloud/cli/internal/cmd/all"
"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/cmd/completion"
"github.com/hetznercloud/cli/internal/cmd/context"
"github.com/hetznercloud/cli/internal/cmd/datacenter"
"github.com/hetznercloud/cli/internal/cmd/firewall"
"github.com/hetznercloud/cli/internal/cmd/floatingip"
"github.com/hetznercloud/cli/internal/cmd/image"
"github.com/hetznercloud/cli/internal/cmd/iso"
"github.com/hetznercloud/cli/internal/cmd/loadbalancer"
"github.com/hetznercloud/cli/internal/cmd/loadbalancertype"
"github.com/hetznercloud/cli/internal/cmd/location"
"github.com/hetznercloud/cli/internal/cmd/network"
"github.com/hetznercloud/cli/internal/cmd/placementgroup"
"github.com/hetznercloud/cli/internal/cmd/primaryip"
"github.com/hetznercloud/cli/internal/cmd/server"
"github.com/hetznercloud/cli/internal/cmd/servertype"
"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/state"
"github.com/hetznercloud/cli/internal/state/config"
)

func init() {
Expand All @@ -17,10 +38,10 @@ func init() {
func main() {
configPath := os.Getenv("HCLOUD_CONFIG")
if configPath == "" {
configPath = state.DefaultConfigPath()
configPath = config.DefaultConfigPath()
}

cfg, err := state.ReadConfig(configPath)
cfg, err := config.ReadConfig(configPath)
if err != nil {
log.Fatalf("unable to read config file %q: %s\n", configPath, err)
}
Expand All @@ -31,6 +52,29 @@ func main() {
}

rootCommand := cli.NewRootCommand(s)
rootCommand.AddCommand(
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),
)

if err := rootCommand.Execute(); err != nil {
log.Fatalln(err)
}
Expand Down
42 changes: 0 additions & 42 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ import (

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/all"
"github.com/hetznercloud/cli/internal/cmd/certificate"
"github.com/hetznercloud/cli/internal/cmd/completion"
"github.com/hetznercloud/cli/internal/cmd/context"
"github.com/hetznercloud/cli/internal/cmd/datacenter"
"github.com/hetznercloud/cli/internal/cmd/firewall"
"github.com/hetznercloud/cli/internal/cmd/floatingip"
"github.com/hetznercloud/cli/internal/cmd/image"
"github.com/hetznercloud/cli/internal/cmd/iso"
"github.com/hetznercloud/cli/internal/cmd/loadbalancer"
"github.com/hetznercloud/cli/internal/cmd/loadbalancertype"
"github.com/hetznercloud/cli/internal/cmd/location"
"github.com/hetznercloud/cli/internal/cmd/network"
"github.com/hetznercloud/cli/internal/cmd/placementgroup"
"github.com/hetznercloud/cli/internal/cmd/primaryip"
"github.com/hetznercloud/cli/internal/cmd/server"
"github.com/hetznercloud/cli/internal/cmd/servertype"
"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/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)
Expand All @@ -40,28 +20,6 @@ func NewRootCommand(s state.State) *cobra.Command {
SilenceErrors: true,
DisableFlagsInUseLine: true,
}
cmd.AddCommand(
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.PersistentFlags().Bool("quiet", false, "Only print error messages")

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/context/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ 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 cfg := s.Config(); cfg.ActiveContext != nil {
cmd.Println(cfg.ActiveContext.Name)
if ctx := s.Config().ActiveContext(); ctx != nil {
cmd.Println(ctx.Name)
}
return nil
}
7 changes: 4 additions & 3 deletions internal/cmd/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/term"

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

func newCreateCommand(s state.State) *cobra.Command {
Expand Down Expand Up @@ -40,7 +41,7 @@ func runCreate(s state.State, cmd *cobra.Command, args []string) error {
return errors.New("name already used")
}

context := &state.ConfigContext{Name: name}
context := &config.Context{Name: name}

var token string

Expand Down Expand Up @@ -82,8 +83,8 @@ func runCreate(s state.State, cmd *cobra.Command, args []string) error {

context.Token = token

cfg.Contexts = append(cfg.Contexts, context)
cfg.ActiveContext = context
cfg.SetContexts(append(cfg.Contexts(), context))
cfg.SetActiveContext(context)

if err := cfg.Write(); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/context/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func runDelete(s state.State, _ *cobra.Command, args []string) error {
if context == nil {
return fmt.Errorf("context not found: %v", name)
}
if cfg.ActiveContext == context {
if cfg.ActiveContext() == context {
_, _ = fmt.Fprintln(os.Stderr, "Warning: You are deleting the currently active context. Please select a new active context.")
cfg.ActiveContext = nil
cfg.SetActiveContext(nil)
}
cfg.RemoveContext(context)
return cfg.Write()
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func runList(s state.State, cmd *cobra.Command, _ []string) error {
tw.WriteHeader(cols)
}
cfg := s.Config()
for _, context := range cfg.Contexts {
for _, context := range cfg.Contexts() {
presentation := ContextPresentation{
Name: context.Name,
Token: context.Token,
Active: " ",
}
if ctx := cfg.ActiveContext; ctx != nil && ctx.Name == context.Name {
if ctx := cfg.ActiveContext(); ctx != nil && ctx.Name == context.Name {
presentation.Active = "*"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/context/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func runUse(s state.State, _ *cobra.Command, args []string) error {
if context == nil {
return fmt.Errorf("context not found: %v", name)
}
cfg.ActiveContext = context
cfg.SetActiveContext(context)
return cfg.Write()
}
20 changes: 0 additions & 20 deletions internal/hcapi2/mock/mock_gen.go

This file was deleted.

117 changes: 0 additions & 117 deletions internal/state/config.go

This file was deleted.

Loading

0 comments on commit d89a322

Please sign in to comment.