Files
nigiri/cli/config/main.go
Pietralberto Mazza e02c2fdd0d Changes for new version of esplora image (#62)
* changes for new version of esplora image

* add electrs port and esplora url env vars in compose yaml files

* wrap viper methods into Config type and use constants package

* add controller to interact with nigiri resources:
 * .env for docker-compose
 * docker daemon
 * json config file

* add use of constants and config packages and change start flag from --port to --env

* add package for global constants and variables

* add use of controller and constants packages instead of local methods and vars

* bump version

* use contants in logs command tests
2019-12-09 15:58:32 +01:00

65 lines
1.2 KiB
Go

package config
import (
"path/filepath"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"github.com/vulpemventures/nigiri/cli/constants"
)
var vip *viper.Viper
func init() {
vip = viper.New()
defaults := viper.New()
newDefaultConfig(defaults)
setConfigFromDefaults(vip, defaults)
}
type Config struct{}
func (c *Config) Viper() *viper.Viper {
return vip
}
func (c *Config) ReadFromFile(path string) error {
vip.SetConfigFile(filepath.Join(path, constants.Filename))
return vip.ReadInConfig()
}
func (c *Config) WriteConfig(path string) error {
vip.SetConfigFile(path)
return vip.WriteConfig()
}
func (c *Config) GetString(str string) string {
return vip.GetString(str)
}
func (c *Config) GetBool(str string) bool {
return vip.GetBool(str)
}
func (c *Config) GetPath() string {
return getPath()
}
func getPath() string {
home, _ := homedir.Expand("~")
return filepath.Join(home, ".nigiri")
}
func newDefaultConfig(v *viper.Viper) {
v.SetDefault(constants.Datadir, getPath())
v.SetDefault(constants.Network, "regtest")
v.SetDefault(constants.AttachLiquid, false)
}
func setConfigFromDefaults(v *viper.Viper, d *viper.Viper) {
for key, value := range d.AllSettings() {
v.SetDefault(key, value)
}
}