2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
package common
|
2014-02-14 22:56:09 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-30 17:51:19 +00:00
|
|
|
"flag"
|
2014-02-22 00:53:09 +00:00
|
|
|
"fmt"
|
2014-06-23 11:55:38 +00:00
|
|
|
"os"
|
2014-07-26 09:24:44 +00:00
|
|
|
|
|
|
|
"github.com/rakyll/globalconf"
|
2014-02-14 22:56:09 +00:00
|
|
|
)
|
|
|
|
|
2014-04-27 15:15:44 +00:00
|
|
|
// Config struct
|
2014-07-03 16:30:51 +00:00
|
|
|
type ConfigManager struct {
|
|
|
|
ExecPath string
|
|
|
|
Debug bool
|
2014-07-11 14:04:09 +00:00
|
|
|
Diff bool
|
2014-07-13 22:37:50 +00:00
|
|
|
DiffType string
|
2014-07-03 16:30:51 +00:00
|
|
|
Paranoia bool
|
2014-10-14 09:48:52 +00:00
|
|
|
VmType int
|
2014-05-30 17:51:19 +00:00
|
|
|
|
|
|
|
conf *globalconf.GlobalConf
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 15:15:44 +00:00
|
|
|
// Read config
|
|
|
|
//
|
2014-06-23 11:55:38 +00:00
|
|
|
// Initialize Config from Config File
|
2014-07-03 16:30:51 +00:00
|
|
|
func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager {
|
2015-03-06 01:46:56 +00:00
|
|
|
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()
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
2015-03-06 01:46:56 +00:00
|
|
|
cfg := &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
|
|
|
|
return cfg
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 11:55:38 +00:00
|
|
|
// provides persistence for flags
|
2014-07-03 16:30:51 +00:00
|
|
|
func (c *ConfigManager) Save(key string, value interface{}) {
|
|
|
|
f := &flag.Flag{Name: key, Value: newConfValue(value)}
|
2014-05-30 17:51:19 +00:00
|
|
|
c.conf.Set("", f)
|
|
|
|
}
|
|
|
|
|
2014-07-03 16:30:51 +00:00
|
|
|
func (c *ConfigManager) Delete(key string) {
|
|
|
|
c.conf.Delete("", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// private type implementing flag.Value
|
2014-05-30 17:51:19 +00:00
|
|
|
type confValue struct {
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2014-07-03 16:30:51 +00:00
|
|
|
// generic constructor to allow persising non-string values directly
|
|
|
|
func newConfValue(value interface{}) *confValue {
|
|
|
|
return &confValue{fmt.Sprintf("%v", value)}
|
|
|
|
}
|
|
|
|
|
2014-05-30 17:51:19 +00:00
|
|
|
func (self confValue) String() string { return self.value }
|
|
|
|
func (self confValue) Set(s string) error { self.value = s; return nil }
|