-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
65 lines (54 loc) · 1.26 KB
/
config.go
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
package config
import (
u "github.com/jbenet/go-ipfs/util"
"os"
)
// Identity tracks the configuration of the local node's identity.
type Identity struct {
PeerID string
}
// Datastore tracks the configuration of the datastore.
type Datastore struct {
Type string
Path string
}
// Config is used to load IPFS config files.
type Config struct {
Identity *Identity
Datastore *Datastore
}
var DefaultConfigFilePath = "~/.go-ipfs/config"
var defaultConfigFile = `{
"identity": {},
"datastore": {
"type": "leveldb",
"path": "~/.go-ipfs/datastore"
}
}
`
// LoadConfig reads given file and returns the read config, or error.
func LoadConfig(filename string) (*Config, error) {
if len(filename) == 0 {
filename = DefaultConfigFilePath
}
// tilde expansion on config file
filename, err := u.TildeExpansion(filename)
if err != nil {
return nil, err
}
// if nothing is there, write first config file.
if _, err := os.Stat(filename); os.IsNotExist(err) {
WriteFile(filename, []byte(defaultConfigFile))
}
var cfg Config
err = ReadConfigFile(filename, &cfg)
if err != nil {
return nil, err
}
// tilde expansion on datastore path
cfg.Datastore.Path, err = u.TildeExpansion(cfg.Datastore.Path)
if err != nil {
return nil, err
}
return &cfg, err
}