go-ethereum/ethutil/config.go

78 lines
1.7 KiB
Go
Raw Normal View History

2014-02-14 16:56:09 -06:00
package ethutil
import (
2014-05-30 12:51:19 -05:00
"flag"
"fmt"
2014-05-30 12:51:19 -05:00
"github.com/rakyll/globalconf"
"os"
2014-06-26 12:45:57 -05:00
"runtime"
2014-02-14 16:56:09 -06:00
)
// Config struct
2014-02-14 16:56:09 -06:00
type config struct {
Db Database
ExecPath string
Debug bool
Ver string
ClientString string
Pubkey []byte
Identifier string
2014-05-30 12:51:19 -05:00
conf *globalconf.GlobalConf
2014-02-14 16:56:09 -06:00
}
var Config *config
// Read config
//
// Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
2014-02-14 16:56:09 -06:00
if Config == nil {
2014-06-26 12:45:57 -05:00
// 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)
2014-06-26 12:45:57 -05:00
os.Create(ConfigFile)
}
g, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: ConfigFile,
EnvPrefix: EnvPrefix,
})
if err != nil {
2014-06-26 12:45:57 -05:00
fmt.Println(err)
} else {
g.ParseAll()
}
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.14", conf: g, Identifier: Identifier}
2014-06-12 03:07:27 -05:00
Config.SetClientString("Ethereum(G)")
2014-02-14 16:56:09 -06:00
}
return Config
}
// Set client string
//
2014-03-20 11:27:48 -05:00
func (c *config) SetClientString(str string) {
2014-06-12 03:07:27 -05:00
os := runtime.GOOS
cust := c.Identifier
Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
2014-03-20 11:27:48 -05:00
}
2014-05-30 12:51:19 -05:00
func (c *config) SetIdentifier(id string) {
c.Identifier = id
c.Set("id", id)
}
// provides persistence for flags
2014-05-30 12:51:19 -05:00
func (c *config) Set(key, value string) {
f := &flag.Flag{Name: key, Value: &confValue{value}}
c.conf.Set("", f)
}
type confValue struct {
value string
}
func (self confValue) String() string { return self.value }
func (self confValue) Set(s string) error { self.value = s; return nil }