diff --git a/cmd/lucy/config.go b/cmd/lucy/config.go new file mode 100644 index 0000000..b64f6b3 --- /dev/null +++ b/cmd/lucy/config.go @@ -0,0 +1,31 @@ +package main + +import ( + "gopkg.in/yaml.v2" + "os" + "path" +) + +type Config struct { + Remote string `yaml:"remote"` +} + +func loadConfig() (*Config, error) { + homedir, err := os.UserHomeDir() + if err != nil { + return nil, err + } + + f, err := os.Open(path.Join(homedir, ".config/lucy.yaml")) + if err != nil { + return nil, err + } + + cfg := Config{} + err = yaml.NewDecoder(f).Decode(&cfg) + if err != nil { + return nil, err + } + + return &cfg, nil +} diff --git a/cmd/lucy/main.go b/cmd/lucy/main.go index c87ef3a..0a1b601 100644 --- a/cmd/lucy/main.go +++ b/cmd/lucy/main.go @@ -11,7 +11,13 @@ import ( ) func main() { - c := client.Client{APIRoot: "http://10.24.4.1:8000"} + cfg, err := loadConfig() + if err != nil { + log.Println("Config at $HOME/.config/lucy.yaml failed:", err) + cfg = &Config{Remote: "http://localhost:8000"} + } + + c := client.Client{APIRoot: cfg.Remote} cmd := parseCommand(os.Args[1:]) switch cmd.Name { diff --git a/go.mod b/go.mod index 54d1ef7..948dce9 100644 --- a/go.mod +++ b/go.mod @@ -15,4 +15,5 @@ require ( github.com/pressly/goose v2.7.0+incompatible golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c + gopkg.in/yaml.v2 v2.2.8 )