2019-03-15 16:02:16 +00:00
|
|
|
package config
|
|
|
|
|
2019-03-15 16:50:16 +00:00
|
|
|
import (
|
2019-03-25 08:31:04 +00:00
|
|
|
"fmt"
|
2019-03-15 16:50:16 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-03-25 08:31:04 +00:00
|
|
|
"os/user"
|
|
|
|
"path"
|
2019-03-15 16:50:16 +00:00
|
|
|
|
2019-03-28 08:21:46 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2019-03-15 16:50:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2019-03-28 08:21:46 +00:00
|
|
|
Logger Logger `toml:"logger"`
|
|
|
|
Network Network `toml:"network"`
|
2019-03-15 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Logger struct {
|
2019-03-28 08:21:46 +00:00
|
|
|
Type string `toml:"type"`
|
|
|
|
Dest string `toml:"dest"`
|
2019-03-15 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 16:02:16 +00:00
|
|
|
type Network struct {
|
2019-03-28 08:21:46 +00:00
|
|
|
Name string `toml:"name"`
|
|
|
|
Type string `toml:"type"`
|
|
|
|
Address string `toml:"address"`
|
|
|
|
Cert string `toml:"cert"`
|
|
|
|
Macaroon string `toml:"macaroon"`
|
|
|
|
MacaroonTimeOut int64 `toml:"macaroon_timeout"`
|
|
|
|
MacaroonIP string `toml:"macaroon_ip"`
|
|
|
|
MaxMsgRecvSize int `toml:"max_msg_recv_size"`
|
|
|
|
ConnTimeout int `toml:"conn_timeout"`
|
|
|
|
PoolCapacity int `toml:"pool_capacity"`
|
2019-03-15 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load(path string) (*Config, error) {
|
|
|
|
c := &Config{}
|
|
|
|
|
2019-03-25 08:31:04 +00:00
|
|
|
if path == "" {
|
|
|
|
dir, err := getAppDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-30 16:46:39 +00:00
|
|
|
path = fmt.Sprintf("%s/config.toml", dir)
|
2019-03-25 08:31:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 16:50:16 +00:00
|
|
|
err := loadFromPath(path, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadFromPath loads the configuration from configuration file path.
|
|
|
|
func loadFromPath(path string, out interface{}) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if f != nil {
|
|
|
|
defer func() {
|
|
|
|
ferr := f.Close()
|
|
|
|
if ferr != nil {
|
|
|
|
log.Println(ferr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-28 08:21:46 +00:00
|
|
|
return toml.Unmarshal(data, out)
|
2019-03-15 16:02:16 +00:00
|
|
|
}
|
2019-03-25 08:31:04 +00:00
|
|
|
|
|
|
|
// getappDir creates if not exists the app directory where the config file
|
|
|
|
// as well as the log file will be stored. In case of failure the current dir
|
|
|
|
// will be used.
|
|
|
|
func getAppDir() (string, error) {
|
|
|
|
usr, _ := user.Current()
|
|
|
|
dir := path.Join(usr.HomeDir, ".lntop")
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2019-04-05 15:00:25 +00:00
|
|
|
err := os.Mkdir(dir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(dir+"/config.toml",
|
|
|
|
[]byte(DefaultFileContent()), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2019-03-25 08:31:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|