From ebe2b3a830a8586363dcd7f8c52105e97d18a0b0 Mon Sep 17 00:00:00 2001 From: Stanislav Seletskiy Date: Wed, 31 Jul 2019 01:35:17 +0300 Subject: [PATCH] config: use ko --- auth.go | 52 ++++++++++++++++------------------------------------ config.go | 27 +++++++++++++++++++++++++++ main.go | 24 ++---------------------- 3 files changed, 45 insertions(+), 58 deletions(-) create mode 100644 config.go diff --git a/auth.go b/auth.go index aebdb269..b453ea56 100644 --- a/auth.go +++ b/auth.go @@ -2,12 +2,10 @@ package main import ( "errors" - "fmt" "net/url" "strings" "github.com/reconquest/karma-go" - "github.com/zazab/zhash" ) type Credentials struct { @@ -19,7 +17,7 @@ type Credentials struct { func GetCredentials( args map[string]interface{}, - config zhash.Hash, + config *Config, ) (*Credentials, error) { var ( username, _ = args["-u"].(string) @@ -30,33 +28,21 @@ func GetCredentials( var err error if username == "" { - username, err = config.GetString("username") - if err != nil { - if zhash.IsNotFound(err) { - return nil, errors.New( - "Confluence username should be specified using -u " + - "flag or be stored in configuration file", - ) - } - - return nil, fmt.Errorf( - "can't read username configuration variable: %s", err, + username = config.Username + if username == "" { + return nil, errors.New( + "Confluence username should be specified using -u " + + "flag or be stored in configuration file", ) } } if password == "" { - password, err = config.GetString("password") - if err != nil { - if zhash.IsNotFound(err) { - return nil, errors.New( - "Confluence password should be specified using -p " + - "flag or be stored in configuration file", - ) - } - - return nil, fmt.Errorf( - "can't read password configuration variable: %s", err, + password = config.Password + if password == "" { + return nil, errors.New( + "Confluence password should be specified using -p " + + "flag or be stored in configuration file", ) } } @@ -75,17 +61,11 @@ func GetCredentials( var ok bool baseURL, ok = args["--base-url"].(string) if !ok { - baseURL, err = config.GetString("base_url") - if err != nil { - if zhash.IsNotFound(err) { - return nil, errors.New( - "Confluence base URL should be specified using -l " + - "flag or be stored in configuration file", - ) - } - - return nil, fmt.Errorf( - "can't read base_url configuration variable: %s", err, + baseURL = config.BaseURL + if baseURL == "" { + return nil, errors.New( + "Confluence base URL should be specified using -l " + + "flag or be stored in configuration file", ) } } diff --git a/config.go b/config.go new file mode 100644 index 00000000..53c72f7a --- /dev/null +++ b/config.go @@ -0,0 +1,27 @@ +package main + +import ( + "os" + + "github.com/kovetskiy/ko" +) + +type Config struct { + Username string `env:"MARK_USERNAME" toml:"username"` + Password string `env:"MARK_PASSWORD" toml:"password"` + BaseURL string `env:"MARK_BASE_URL" toml:"base_url"` +} + +func LoadConfig(path string) (*Config, error) { + config := &Config{} + err := ko.Load(path, config) + if err != nil { + if os.IsNotExist(err) { + return config, nil + } + + return nil, err + } + + return config, nil +} diff --git a/main.go b/main.go index 0012ebde..56919096 100644 --- a/main.go +++ b/main.go @@ -7,14 +7,12 @@ import ( "path/filepath" "strings" - "github.com/BurntSushi/toml" "github.com/kovetskiy/godocs" "github.com/kovetskiy/lorg" "github.com/kovetskiy/mark/pkg/confluence" "github.com/kovetskiy/mark/pkg/mark" "github.com/reconquest/cog" "github.com/reconquest/karma-go" - "github.com/zazab/zhash" ) const ( @@ -121,8 +119,8 @@ func main() { initlog(args["--debug"].(bool), args["--trace"].(bool)) - config, err := getConfig(filepath.Join(os.Getenv("HOME"), ".config/mark")) - if err != nil && !os.IsNotExist(err) { + config, err := LoadConfig(filepath.Join(os.Getenv("HOME"), ".config/mark")) + if err != nil { log.Fatal(err) } @@ -321,21 +319,3 @@ func resolvePage( return page, nil } - -func getConfig(path string) (zhash.Hash, error) { - configData := map[string]interface{}{} - _, err := toml.DecodeFile(path, &configData) - if err != nil { - if os.IsNotExist(err) { - return zhash.NewHash(), err - } - - return zhash.NewHash(), karma.Format( - err, - "can't decode toml file: %s", - path, - ) - } - - return zhash.HashFromMap(configData), nil -}