Official Go implementation of the Ethereum protocol
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.
go-ethereum/ethutil/config.go

78 lines
1.7 KiB

11 years ago
package ethutil
import (
10 years ago
"flag"
"fmt"
10 years ago
"github.com/rakyll/globalconf"
"os"
10 years ago
"runtime"
11 years ago
)
// Config struct
11 years ago
type config struct {
Db Database
ExecPath string
Debug bool
Ver string
ClientString string
Pubkey []byte
Identifier string
10 years ago
conf *globalconf.GlobalConf
11 years ago
}
var Config *config
// Read config
//
// Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
11 years ago
if Config == nil {
10 years ago
// 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)
10 years ago
os.Create(ConfigFile)
}
g, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: ConfigFile,
EnvPrefix: EnvPrefix,
})
if err != nil {
10 years ago
fmt.Println(err)
} else {
g.ParseAll()
}
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.14", conf: g, Identifier: Identifier}
10 years ago
Config.SetClientString("Ethereum(G)")
11 years ago
}
return Config
}
// Set client string
//
func (c *config) SetClientString(str string) {
10 years ago
os := runtime.GOOS
cust := c.Identifier
Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
}
10 years ago
func (c *config) SetIdentifier(id string) {
c.Identifier = id
c.Set("id", id)
}
// provides persistence for flags
10 years ago
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 }