plugeth/ethutil/config.go
zelig 63157c798d refactor config (transitional). Details:
- ReadConfig initialiser sets up global ethutil.Config via config file passed from wrappers
- does not write out adhoc default (not meant to) but creates empty config file if it does not exist so that globalconf does not complain if persists a flag
- default datadir and default config file set together with other flag defaults in wrappers
- default assetpath set together with other command line flags defaults in gui wrapper (not in ethutil.Config or ui/ui_lib)
- add EnvPrefix, to handle environment variable options too via globalconf
- this is still transitional: global Config should just be a wrapper around globalconfig config handler and should be moved to go-ethereum
- actual eth stack config should not be global instead config handled properly with explicit dependency injectioninto eth stack component instances
2014-06-23 12:55:38 +01:00

78 lines
1.7 KiB
Go

package ethutil
import (
"flag"
"fmt"
"github.com/rakyll/globalconf"
"runtime"
"os"
)
// Config struct
type config struct {
Db Database
ExecPath string
Debug bool
Ver string
ClientString string
Pubkey []byte
Identifier string
conf *globalconf.GlobalConf
}
var Config *config
// Read config
//
// Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
if Config == nil {
// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
_, err := os.Stat(ConfigFile)
if err != nil && os.IsNotExist(err) {
fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile)
os.Create(ConfigFile)
}
g, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: ConfigFile,
EnvPrefix: EnvPrefix,
})
if err != nil {
fmt.Println(err)
} else {
g.ParseAll()
}
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.14", conf: g, Identifier: Identifier}
Config.SetClientString("Ethereum(G)")
}
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
func (c *config) Set(key, value string) {
f := &flag.Flag{Name: key, Value: &confValue{value}}
c.conf.Set("", f)
}
type confValue struct {
value string
}
func (self confValue) String() string { return self.value }
func (self confValue) Set(s string) error { self.value = s; return nil }