refactor config (transitional). Details:

- ReadConfig initialiser sets up global ethutil.Config via config file passed from wrappers
- does not write out adhoc default (not meant to) but creates empty config file if it does not exist so that globalconf does not complain if persists a flag
- default datadir and default config file set together with other flag defaults in wrappers
- default assetpath set together with other command line flags defaults in gui wrapper (not in ethutil.Config or ui/ui_lib)
- add EnvPrefix, to handle environment variable options too via globalconf
- this is still transitional: global Config should just be a wrapper around globalconfig config handler and should be moved to go-ethereum
- actual eth stack config should not be global instead config handled properly with explicit dependency injectioninto eth stack component instances
This commit is contained in:
zelig 2014-06-23 12:55:38 +01:00
parent b9e8a3e024
commit 63157c798d
1 changed files with 20 additions and 180 deletions

View File

@ -4,18 +4,14 @@ import (
"flag"
"fmt"
"github.com/rakyll/globalconf"
"log"
"os"
"os/user"
"path"
"runtime"
"os"
)
// Config struct
type config struct {
Db Database
Log *Logger
ExecPath string
Debug bool
Ver string
@ -26,62 +22,31 @@ type config struct {
conf *globalconf.GlobalConf
}
const defaultConf = `
id = ""
port = 30303
upnp = true
maxpeer = 10
rpc = false
rpcport = 8080
`
var Config *config
func ApplicationFolder(base string) string {
usr, _ := user.Current()
p := path.Join(usr.HomeDir, base)
if len(base) > 0 {
//Check if the logging directory already exists, create it if not
_, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
log.Printf("Debug logging directory %s doesn't exist, creating it\n", p)
os.Mkdir(p, 0777)
}
}
iniFilePath := path.Join(p, "conf.ini")
_, err = os.Stat(iniFilePath)
if err != nil && os.IsNotExist(err) {
file, err := os.Create(iniFilePath)
if err != nil {
fmt.Println(err)
} else {
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "ethereal", "assets")
file.Write([]byte(defaultConf + "\nasset_path = " + assetPath))
}
}
}
return p
}
// Read config
//
// Initialize the global Config variable with default settings
func ReadConfig(base string, logTypes LoggerType, g *globalconf.GlobalConf, id string) *config {
// Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
if Config == nil {
path := ApplicationFolder(base)
Config = &config{ExecPath: path, Debug: true, Ver: "0.5.14"}
Config.conf = g
Config.Identifier = id
Config.Log = NewLogger(logTypes, LogLevelDebug)
// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
_, err := os.Stat(ConfigFile)
if err != nil && os.IsNotExist(err) {
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()
}
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.14", conf: g, Identifier: Identifier}
Config.SetClientString("Ethereum(G)")
}
return Config
}
@ -98,137 +63,12 @@ func (c *config) SetIdentifier(id string) {
c.Set("id", id)
}
// provides persistence for flags
func (c *config) Set(key, value string) {
f := &flag.Flag{Name: key, Value: &confValue{value}}
c.conf.Set("", f)
}
type LoggerType byte
const (
LogFile = 0x1
LogStd = 0x2
)
type LogSystem interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
}
type Logger struct {
logSys []LogSystem
logLevel int
}
func NewLogger(flag LoggerType, level int) *Logger {
var loggers []LogSystem
flags := log.LstdFlags
if flag&LogFile > 0 {
file, err := os.OpenFile(path.Join(Config.ExecPath, "debug.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
if err != nil {
log.Panic("unable to create file logger", err)
}
log := log.New(file, "", flags)
loggers = append(loggers, log)
}
if flag&LogStd > 0 {
log := log.New(os.Stdout, "", flags)
loggers = append(loggers, log)
}
return &Logger{logSys: loggers, logLevel: level}
}
func (self *Logger) SetLevel(level int) {
self.logLevel = level
}
func (log *Logger) AddLogSystem(logger LogSystem) {
log.logSys = append(log.logSys, logger)
}
const (
LogLevelSystem = iota
LogLevelDebug
LogLevelInfo
)
func (log *Logger) Debugln(v ...interface{}) {
if log.logLevel != LogLevelDebug {
return
}
for _, logger := range log.logSys {
logger.Println(v...)
}
}
func (log *Logger) Debugf(format string, v ...interface{}) {
if log.logLevel != LogLevelDebug {
return
}
for _, logger := range log.logSys {
logger.Printf(format, v...)
}
}
func (log *Logger) Infoln(v ...interface{}) {
if log.logLevel > LogLevelInfo {
return
}
for _, logger := range log.logSys {
logger.Println(v...)
}
}
func (log *Logger) Infof(format string, v ...interface{}) {
if log.logLevel > LogLevelInfo {
return
}
for _, logger := range log.logSys {
logger.Printf(format, v...)
}
}
func (log *Logger) Fatal(v ...interface{}) {
if log.logLevel > LogLevelInfo {
return
}
for _, logger := range log.logSys {
logger.Println(v...)
}
os.Exit(1)
}
func (log *Logger) Println(level int, v ...interface{}) {
if log.logLevel > level {
return
}
for _, logger := range log.logSys {
logger.Println(v...)
}
}
func (log *Logger) Printf(level int, format string, v ...interface{}) {
if log.logLevel > level {
return
}
for _, logger := range log.logSys {
logger.Printf(format, v...)
}
}
type confValue struct {
value string
}