Merge pull request #7 from seletskiy/master

config: use ko
This commit is contained in:
Egor Kovetskiy 2019-08-02 12:17:09 +03:00 committed by GitHub
commit e77e589494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 58 deletions

52
auth.go
View File

@ -2,12 +2,10 @@ package main
import ( import (
"errors" "errors"
"fmt"
"net/url" "net/url"
"strings" "strings"
"github.com/reconquest/karma-go" "github.com/reconquest/karma-go"
"github.com/zazab/zhash"
) )
type Credentials struct { type Credentials struct {
@ -19,7 +17,7 @@ type Credentials struct {
func GetCredentials( func GetCredentials(
args map[string]interface{}, args map[string]interface{},
config zhash.Hash, config *Config,
) (*Credentials, error) { ) (*Credentials, error) {
var ( var (
username, _ = args["-u"].(string) username, _ = args["-u"].(string)
@ -30,33 +28,21 @@ func GetCredentials(
var err error var err error
if username == "" { if username == "" {
username, err = config.GetString("username") username = config.Username
if err != nil { if username == "" {
if zhash.IsNotFound(err) { return nil, errors.New(
return nil, errors.New( "Confluence username should be specified using -u " +
"Confluence username should be specified using -u " + "flag or be stored in configuration file",
"flag or be stored in configuration file",
)
}
return nil, fmt.Errorf(
"can't read username configuration variable: %s", err,
) )
} }
} }
if password == "" { if password == "" {
password, err = config.GetString("password") password = config.Password
if err != nil { if password == "" {
if zhash.IsNotFound(err) { return nil, errors.New(
return nil, errors.New( "Confluence password should be specified using -p " +
"Confluence password should be specified using -p " + "flag or be stored in configuration file",
"flag or be stored in configuration file",
)
}
return nil, fmt.Errorf(
"can't read password configuration variable: %s", err,
) )
} }
} }
@ -75,17 +61,11 @@ func GetCredentials(
var ok bool var ok bool
baseURL, ok = args["--base-url"].(string) baseURL, ok = args["--base-url"].(string)
if !ok { if !ok {
baseURL, err = config.GetString("base_url") baseURL = config.BaseURL
if err != nil { if baseURL == "" {
if zhash.IsNotFound(err) { return nil, errors.New(
return nil, errors.New( "Confluence base URL should be specified using -l " +
"Confluence base URL should be specified using -l " + "flag or be stored in configuration file",
"flag or be stored in configuration file",
)
}
return nil, fmt.Errorf(
"can't read base_url configuration variable: %s", err,
) )
} }
} }

27
config.go Normal file
View File

@ -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
}

24
main.go
View File

@ -7,14 +7,12 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/BurntSushi/toml"
"github.com/kovetskiy/godocs" "github.com/kovetskiy/godocs"
"github.com/kovetskiy/lorg" "github.com/kovetskiy/lorg"
"github.com/kovetskiy/mark/pkg/confluence" "github.com/kovetskiy/mark/pkg/confluence"
"github.com/kovetskiy/mark/pkg/mark" "github.com/kovetskiy/mark/pkg/mark"
"github.com/reconquest/cog" "github.com/reconquest/cog"
"github.com/reconquest/karma-go" "github.com/reconquest/karma-go"
"github.com/zazab/zhash"
) )
const ( const (
@ -121,8 +119,8 @@ func main() {
initlog(args["--debug"].(bool), args["--trace"].(bool)) initlog(args["--debug"].(bool), args["--trace"].(bool))
config, err := getConfig(filepath.Join(os.Getenv("HOME"), ".config/mark")) config, err := LoadConfig(filepath.Join(os.Getenv("HOME"), ".config/mark"))
if err != nil && !os.IsNotExist(err) { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -321,21 +319,3 @@ func resolvePage(
return page, nil 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
}