diff options
author | zelig <viktor.tron@gmail.com> | 2015-03-16 23:46:29 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2015-03-16 23:46:29 +0800 |
commit | 5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb (patch) | |
tree | d5ba6197a8c0c8e36bb92ee9fc8aad717cb2d178 /common/config.go | |
parent | 8393dab470c40678caf36ada82e312d29c4cf5c4 (diff) | |
parent | 22893b7ac925c49168c119f293ea8befc3aff5cc (diff) | |
download | go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.tar go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.tar.gz go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.tar.bz2 go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.tar.lz go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.tar.xz go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.tar.zst go-tangerine-5e7702fd050ed5520a03cc2cfc8b1f45a70f35fb.zip |
Merge remote-tracking branch 'upstream/develop' into frontier/js
Conflicts:
cmd/ethereum/js.go
javascript/types.go
Diffstat (limited to 'common/config.go')
-rw-r--r-- | common/config.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/common/config.go b/common/config.go new file mode 100644 index 000000000..23a902bc1 --- /dev/null +++ b/common/config.go @@ -0,0 +1,67 @@ +package common + +import ( + "flag" + "fmt" + "os" + + "github.com/rakyll/globalconf" +) + +// Config struct +type ConfigManager struct { + ExecPath string + Debug bool + Diff bool + DiffType string + Paranoia bool + VmType int + + conf *globalconf.GlobalConf +} + +// Read config +// +// Initialize Config from Config File +func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager { + if !FileExist(ConfigFile) { + // create ConfigFile if it does not exist, otherwise + // globalconf will panic when trying to persist flags. + 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() + } + cfg := &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true} + return cfg +} + +// provides persistence for flags +func (c *ConfigManager) Save(key string, value interface{}) { + f := &flag.Flag{Name: key, Value: newConfValue(value)} + c.conf.Set("", f) +} + +func (c *ConfigManager) Delete(key string) { + c.conf.Delete("", key) +} + +// private type implementing flag.Value +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)} +} + +func (self confValue) String() string { return self.value } +func (self confValue) Set(s string) error { self.value = s; return nil } |