Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: make state config mockable #672

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
9 changes: 5 additions & 4 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 All @@ -36,11 +37,11 @@ func runCreate(s state.State, cmd *cobra.Command, args []string) error {
if name == "" {
return errors.New("invalid name")
}
if cfg.ContextByName(name) != nil {
if config.ContextByName(cfg, name) != nil {
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
11 changes: 6 additions & 5 deletions internal/cmd/context/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

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

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(s.Config().ContextNames)),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidates(config.ContextNames(s.Config())...)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: state.Wrap(s, runDelete),
Expand All @@ -26,14 +27,14 @@ func newDeleteCommand(s state.State) *cobra.Command {
func runDelete(s state.State, _ *cobra.Command, args []string) error {
name := args[0]
cfg := s.Config()
context := cfg.ContextByName(name)
context := config.ContextByName(cfg, name)
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)
config.RemoveContext(cfg, context)
return cfg.Write()
}
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
7 changes: 4 additions & 3 deletions internal/cmd/context/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

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

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(s.Config().ContextNames)),
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidates(config.ContextNames(s.Config())...)),
TraverseChildren: true,
DisableFlagsInUseLine: true,
RunE: state.Wrap(s, runUse),
Expand All @@ -29,10 +30,10 @@ func runUse(s state.State, _ *cobra.Command, args []string) error {
}
name := args[0]
cfg := s.Config()
context := cfg.ContextByName(name)
context := config.ContextByName(cfg, name)
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