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] 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),