mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-23 21:32:41 +08:00
config: use ko
This commit is contained in:
parent
4fc1aa6553
commit
ebe2b3a830
52
auth.go
52
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",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
27
config.go
Normal file
27
config.go
Normal 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
24
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user