refac config

pull/1/head
Edouard Paris 5 years ago
parent b0c5e32e3b
commit c0f4352a84

@ -1,9 +1,12 @@
package config
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"gopkg.in/yaml.v2"
)
@ -18,9 +21,8 @@ type Logger struct {
}
type Network struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Type string `yaml:"type"`
Status string `yaml:"status"`
Address string `yaml:"address"`
Cert string `yaml:"cert"`
Macaroon string `yaml:"macaroon"`
@ -34,6 +36,14 @@ type Network struct {
func Load(path string) (*Config, error) {
c := &Config{}
if path == "" {
dir, err := getAppDir()
if err != nil {
return nil, err
}
path = fmt.Sprintf("%s/config.yml", dir)
}
err := loadFromPath(path, c)
if err != nil {
return nil, err
@ -67,3 +77,23 @@ func loadFromPath(path string, out interface{}) error {
return yaml.Unmarshal(data, out)
}
// 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) {
oserr := os.Mkdir(dir, 0700)
if oserr != nil {
return "", oserr
}
} else {
return "", err
}
}
return dir, nil
}

@ -0,0 +1,5 @@
package config
func NewDefault() *Config {
return &Config{}
}

@ -36,7 +36,7 @@ type Backend struct {
}
func (l Backend) NodeName() string {
return l.cfg.ID
return l.cfg.Name
}
func (l Backend) Info(ctx context.Context) (*models.Info, error) {
@ -273,7 +273,7 @@ func New(c *config.Network, logger logging.Logger) (*Backend, error) {
backend := &Backend{
cfg: c,
logger: logger.With(logging.String("id", c.ID)),
logger: logger.With(logging.String("name", c.Name)),
}
backend.pool, err = pool.New(backend.NewClientConn, c.PoolCapacity, time.Duration(c.ConnTimeout))

@ -31,7 +31,7 @@ func (l *Backend) SendPayment(ctx context.Context, payreq *models.PayReq) (*mode
}
func (b *Backend) NodeName() string {
return b.cfg.ID
return b.cfg.Name
}
func (b *Backend) SubscribeInvoice(ctx context.Context, ChannelInvoice chan *models.Invoice) error {

Loading…
Cancel
Save