diff --git a/config/config.go b/config/config.go index 73f7010..18cd9ac 100644 --- a/config/config.go +++ b/config/config.go @@ -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 +} diff --git a/config/default.go b/config/default.go new file mode 100644 index 0000000..e5aec05 --- /dev/null +++ b/config/default.go @@ -0,0 +1,5 @@ +package config + +func NewDefault() *Config { + return &Config{} +} diff --git a/network/backend/lnd/lnd.go b/network/backend/lnd/lnd.go index 52205bd..05cfd29 100644 --- a/network/backend/lnd/lnd.go +++ b/network/backend/lnd/lnd.go @@ -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)) diff --git a/network/backend/mock/mock.go b/network/backend/mock/mock.go index 27b2dab..331c159 100644 --- a/network/backend/mock/mock.go +++ b/network/backend/mock/mock.go @@ -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 {