github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/ethutil/config.go (about) 1 package ethutil 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 8 "github.com/rakyll/globalconf" 9 ) 10 11 // Config struct 12 type ConfigManager struct { 13 ExecPath string 14 Debug bool 15 Diff bool 16 DiffType string 17 Paranoia bool 18 VmType int 19 20 conf *globalconf.GlobalConf 21 } 22 23 var Config *ConfigManager 24 25 // Read config 26 // 27 // Initialize Config from Config File 28 func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager { 29 if Config == nil { 30 // create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags 31 if !FileExist(ConfigFile) { 32 fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile) 33 os.Create(ConfigFile) 34 } 35 g, err := globalconf.NewWithOptions(&globalconf.Options{ 36 Filename: ConfigFile, 37 EnvPrefix: EnvPrefix, 38 }) 39 if err != nil { 40 fmt.Println(err) 41 } else { 42 g.ParseAll() 43 } 44 Config = &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true} 45 } 46 return Config 47 } 48 49 // provides persistence for flags 50 func (c *ConfigManager) Save(key string, value interface{}) { 51 f := &flag.Flag{Name: key, Value: newConfValue(value)} 52 c.conf.Set("", f) 53 } 54 55 func (c *ConfigManager) Delete(key string) { 56 c.conf.Delete("", key) 57 } 58 59 // private type implementing flag.Value 60 type confValue struct { 61 value string 62 } 63 64 // generic constructor to allow persising non-string values directly 65 func newConfValue(value interface{}) *confValue { 66 return &confValue{fmt.Sprintf("%v", value)} 67 } 68 69 func (self confValue) String() string { return self.value } 70 func (self confValue) Set(s string) error { self.value = s; return nil }