Skip to content

Commit

Permalink
Move config creation logic into main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 4, 2024
1 parent 6d862d2 commit 6597bd5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
12 changes: 11 additions & 1 deletion cmd/hcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions internal/state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
toml "github.com/pelletier/go-toml/v2"
)

var DefaultConfigPath string

type Config struct {
Path string
Endpoint string
Expand Down
8 changes: 4 additions & 4 deletions internal/state/config_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !windows
// +build !windows

package state

Expand All @@ -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 ""
}
6 changes: 3 additions & 3 deletions internal/state/config_windows.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build windows
// +build windows

package state

Expand All @@ -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 ""
}
59 changes: 24 additions & 35 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package state

import (
"context"
"fmt"
"log"
"os"
"time"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 6597bd5

Please sign in to comment.