Official Go implementation of the Ethereum protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-ethereum/ethutil/config.go

71 lines
1.6 KiB

11 years ago
package ethutil
import (
11 years ago
"flag"
"fmt"
"os"
"github.com/rakyll/globalconf"
11 years ago
)
// Config struct
type ConfigManager struct {
ExecPath string
Debug bool
Diff bool
10 years ago
DiffType string
Paranoia bool
VmType int
11 years ago
conf *globalconf.GlobalConf
11 years ago
}
var Config *ConfigManager
11 years ago
// Read config
//
// Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager {
11 years ago
if Config == nil {
11 years ago
// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
if !FileExist(ConfigFile) {
fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile)
11 years ago
os.Create(ConfigFile)
}
g, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: ConfigFile,
EnvPrefix: EnvPrefix,
})
if err != nil {
11 years ago
fmt.Println(err)
} else {
g.ParseAll()
}
Config = &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
11 years ago
}
return Config
}
// provides persistence for flags
func (c *ConfigManager) Save(key string, value interface{}) {
f := &flag.Flag{Name: key, Value: newConfValue(value)}
11 years ago
c.conf.Set("", f)
}
func (c *ConfigManager) Delete(key string) {
c.conf.Delete("", key)
}
// private type implementing flag.Value
11 years ago
type confValue struct {
value string
}
// generic constructor to allow persising non-string values directly
func newConfValue(value interface{}) *confValue {
return &confValue{fmt.Sprintf("%v", value)}
}
11 years ago
func (self confValue) String() string { return self.value }
func (self confValue) Set(s string) error { self.value = s; return nil }