Skip to content

Commit

Permalink
config: use ko
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed Jul 31, 2019
1 parent 4fc1aa6 commit ebe2b3a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
52 changes: 16 additions & 36 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package main

import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/reconquest/karma-go"
"github.com/zazab/zhash"
)

type Credentials struct {
Expand All @@ -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)
Expand All @@ -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",
)
}
}
Expand All @@ -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",
)
}
}
Expand Down
27 changes: 27 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}

0 comments on commit ebe2b3a

Please sign in to comment.