ConfigManager (transitional)
- remove clientstring handling from ethutil.Config - ReadConfig takes no Identifier argument - members Ver, ClientString, ClientIdentifier removed from Config - type ConfValue removed - expose public type ethutil.ConfigManager - Set -> Save(key string, value interface{}) now takes any value to allow for persisting non-string values directly - TODO: eliminate all eth specific configs, just a wrapper around globalconf
This commit is contained in:
parent
90c2064640
commit
f02602d02d
|
@ -5,29 +5,25 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rakyll/globalconf"
|
"github.com/rakyll/globalconf"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config struct
|
// Config struct
|
||||||
type config struct {
|
type ConfigManager struct {
|
||||||
Db Database
|
Db Database
|
||||||
|
|
||||||
ExecPath string
|
ExecPath string
|
||||||
Debug bool
|
Debug bool
|
||||||
Paranoia bool
|
Paranoia bool
|
||||||
Ver string
|
|
||||||
ClientString string
|
|
||||||
Identifier string
|
|
||||||
|
|
||||||
conf *globalconf.GlobalConf
|
conf *globalconf.GlobalConf
|
||||||
}
|
}
|
||||||
|
|
||||||
var Config *config
|
var Config *ConfigManager
|
||||||
|
|
||||||
// Read config
|
// Read config
|
||||||
//
|
//
|
||||||
// Initialize Config from Config File
|
// Initialize Config from Config File
|
||||||
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
|
func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager {
|
||||||
if Config == nil {
|
if Config == nil {
|
||||||
// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
|
// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
|
||||||
_, err := os.Stat(ConfigFile)
|
_, err := os.Stat(ConfigFile)
|
||||||
|
@ -44,34 +40,30 @@ func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix
|
||||||
} else {
|
} else {
|
||||||
g.ParseAll()
|
g.ParseAll()
|
||||||
}
|
}
|
||||||
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.16", conf: g, Identifier: Identifier, Paranoia: true}
|
Config = &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
|
||||||
Config.SetClientString("Ethereum(G)")
|
|
||||||
}
|
}
|
||||||
return Config
|
return Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set client string
|
|
||||||
//
|
|
||||||
func (c *config) SetClientString(str string) {
|
|
||||||
os := runtime.GOOS
|
|
||||||
cust := c.Identifier
|
|
||||||
Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *config) SetIdentifier(id string) {
|
|
||||||
c.Identifier = id
|
|
||||||
c.Set("id", id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// provides persistence for flags
|
// provides persistence for flags
|
||||||
func (c *config) Set(key, value string) {
|
func (c *ConfigManager) Save(key string, value interface{}) {
|
||||||
f := &flag.Flag{Name: key, Value: &confValue{value}}
|
f := &flag.Flag{Name: key, Value: newConfValue(value)}
|
||||||
c.conf.Set("", f)
|
c.conf.Set("", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ConfigManager) Delete(key string) {
|
||||||
|
c.conf.Delete("", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// private type implementing flag.Value
|
||||||
type confValue struct {
|
type confValue struct {
|
||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generic constructor to allow persising non-string values directly
|
||||||
|
func newConfValue(value interface{}) *confValue {
|
||||||
|
return &confValue{fmt.Sprintf("%v", value)}
|
||||||
|
}
|
||||||
|
|
||||||
func (self confValue) String() string { return self.value }
|
func (self confValue) String() string { return self.value }
|
||||||
func (self confValue) Set(s string) error { self.value = s; return nil }
|
func (self confValue) Set(s string) error { self.value = s; return nil }
|
||||||
|
|
Loading…
Reference in New Issue