2014-02-15 00:56:09 +02:00
|
|
|
package ethutil
|
|
|
|
|
|
|
|
import (
|
2014-05-30 20:51:19 +03:00
|
|
|
"flag"
|
2014-02-22 02:53:09 +02:00
|
|
|
"fmt"
|
2014-05-30 20:51:19 +03:00
|
|
|
"github.com/rakyll/globalconf"
|
2014-06-23 14:55:38 +03:00
|
|
|
"os"
|
2014-06-26 20:45:57 +03:00
|
|
|
"runtime"
|
2014-02-15 00:56:09 +02:00
|
|
|
)
|
|
|
|
|
2014-04-27 18:15:44 +03:00
|
|
|
// Config struct
|
2014-02-15 00:56:09 +02:00
|
|
|
type config struct {
|
|
|
|
Db Database
|
|
|
|
|
2014-03-17 13:08:16 +02:00
|
|
|
ExecPath string
|
|
|
|
Debug bool
|
2014-07-01 10:56:10 +03:00
|
|
|
Paranoia bool
|
2014-03-17 13:08:16 +02:00
|
|
|
Ver string
|
|
|
|
ClientString string
|
2014-05-22 18:35:26 +03:00
|
|
|
Identifier string
|
2014-05-30 20:51:19 +03:00
|
|
|
|
|
|
|
conf *globalconf.GlobalConf
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var Config *config
|
|
|
|
|
2014-04-27 18:15:44 +03:00
|
|
|
// Read config
|
|
|
|
//
|
2014-06-23 14:55:38 +03:00
|
|
|
// Initialize Config from Config File
|
|
|
|
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
|
2014-02-15 00:56:09 +02:00
|
|
|
if Config == nil {
|
2014-06-26 20:45:57 +03: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) {
|
2014-06-23 14:55:38 +03:00
|
|
|
fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile)
|
2014-06-26 20:45:57 +03:00
|
|
|
os.Create(ConfigFile)
|
|
|
|
}
|
|
|
|
g, err := globalconf.NewWithOptions(&globalconf.Options{
|
|
|
|
Filename: ConfigFile,
|
|
|
|
EnvPrefix: EnvPrefix,
|
2014-06-23 14:55:38 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-06-26 20:45:57 +03:00
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
g.ParseAll()
|
|
|
|
}
|
2014-07-01 17:28:17 +03:00
|
|
|
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.16", conf: g, Identifier: Identifier, Paranoia: true}
|
2014-06-12 11:07:27 +03:00
|
|
|
Config.SetClientString("Ethereum(G)")
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
return Config
|
|
|
|
}
|
|
|
|
|
2014-04-27 18:15:44 +03:00
|
|
|
// Set client string
|
|
|
|
//
|
2014-03-20 18:27:48 +02:00
|
|
|
func (c *config) SetClientString(str string) {
|
2014-06-12 11:07:27 +03: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 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 20:51:19 +03:00
|
|
|
func (c *config) SetIdentifier(id string) {
|
|
|
|
c.Identifier = id
|
|
|
|
c.Set("id", id)
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:55:38 +03:00
|
|
|
// provides persistence for flags
|
2014-05-30 20:51:19 +03: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 }
|