mirror of https://github.com/ethereum/go-ethereum
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.
68 lines
1.5 KiB
68 lines
1.5 KiB
10 years ago
|
package common
|
||
11 years ago
|
|
||
|
import (
|
||
11 years ago
|
"flag"
|
||
11 years ago
|
"fmt"
|
||
11 years ago
|
"os"
|
||
10 years ago
|
|
||
|
"github.com/rakyll/globalconf"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
// Config struct
|
||
11 years ago
|
type ConfigManager struct {
|
||
|
ExecPath string
|
||
|
Debug bool
|
||
11 years ago
|
Diff bool
|
||
11 years ago
|
DiffType string
|
||
11 years ago
|
Paranoia bool
|
||
10 years ago
|
VmType int
|
||
11 years ago
|
|
||
|
conf *globalconf.GlobalConf
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// Read config
|
||
|
//
|
||
11 years ago
|
// Initialize Config from Config File
|
||
11 years ago
|
func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager {
|
||
10 years ago
|
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()
|
||
11 years ago
|
}
|
||
10 years ago
|
cfg := &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
|
||
|
return cfg
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// provides persistence for flags
|
||
11 years ago
|
func (c *ConfigManager) Save(key string, value interface{}) {
|
||
|
f := &flag.Flag{Name: key, Value: newConfValue(value)}
|
||
11 years ago
|
c.conf.Set("", f)
|
||
|
}
|
||
|
|
||
11 years ago
|
func (c *ConfigManager) Delete(key string) {
|
||
|
c.conf.Delete("", key)
|
||
|
}
|
||
|
|
||
|
// private type implementing flag.Value
|
||
11 years ago
|
type confValue struct {
|
||
|
value string
|
||
|
}
|
||
|
|
||
11 years ago
|
// 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 }
|