aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/config.go
blob: 675175cdd60f7c39a6d1c75037fd86e4c6065bc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package ethutil

import (
    "flag"
    "fmt"
    "github.com/rakyll/globalconf"
    "os"
    "runtime"
)

// Config struct
type config struct {
    Db Database

    ExecPath     string
    Debug        bool
    Paranoia     bool
    Ver          string
    ClientString string
    Identifier   string

    conf *globalconf.GlobalConf
}

var Config *config

// Read config
//
// Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config {
    if Config == nil {
        // 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.15", conf: g, Identifier: Identifier, Paranoia: true}
        Config.SetClientString("Ethereum(G)")
    }
    return Config
}

// Set client string
//
func (c *config) SetClientString(str string) {
    os := runtime.GOOS
    cust := c.Identifier
    Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
}

func (c *config) SetIdentifier(id string) {
    c.Identifier = id
    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 confValue struct {
    value string
}

func (self confValue) String() string     { return self.value }
func (self confValue) Set(s string) error { self.value = s; return nil }