go-ipfs-config: refactor(config): break it apart
Brian Tiger Chow committed
Jan 12, 2015 at 19:09 UTC
7882aa98f5b221992e4821081c9cd239fe57c7da
8 files changed
+150
-136
config/addresses.go
new
+8
@@ -0,0 +1,8 @@
1
+package config
2
+
3
+// Addresses stores the (string) multiaddr addresses for the node.
4
+type Addresses struct {
5
+ Swarm []string // addresses for the swarm network
6
+ API string // address for the local API (RPC)
7
+ Gateway string // address to listen on for IPFS HTTP object gateway
8
+}
config/bootstrap_peers.go
new
+62
@@ -0,0 +1,62 @@
1
+package config
2
+
3
+import (
4
+ "errors"
5
+ "strings"
6
+
7
+ ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8
+ mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
9
+)
10
+
11
+// BootstrapPeer is a peer used to bootstrap the network.
12
+type BootstrapPeer struct {
13
+ Address string
14
+ PeerID string // until multiaddr supports ipfs, use another field.
15
+}
16
+
17
+func (bp *BootstrapPeer) String() string {
18
+ return bp.Address + "/" + bp.PeerID
19
+}
20
+
21
+func ParseBootstrapPeer(addr string) (BootstrapPeer, error) {
22
+ // to be replaced with just multiaddr parsing, once ptp is a multiaddr protocol
23
+ idx := strings.LastIndex(addr, "/")
24
+ if idx == -1 {
25
+ return BootstrapPeer{}, errors.New("invalid address")
26
+ }
27
+ addrS := addr[:idx]
28
+ peeridS := addr[idx+1:]
29
+
30
+ // make sure addrS parses as a multiaddr.
31
+ if len(addrS) > 0 {
32
+ maddr, err := ma.NewMultiaddr(addrS)
33
+ if err != nil {
34
+ return BootstrapPeer{}, err
35
+ }
36
+
37
+ addrS = maddr.String()
38
+ }
39
+
40
+ // make sure idS parses as a peer.ID
41
+ _, err := mh.FromB58String(peeridS)
42
+ if err != nil {
43
+ return BootstrapPeer{}, err
44
+ }
45
+
46
+ return BootstrapPeer{
47
+ Address: addrS,
48
+ PeerID: peeridS,
49
+ }, nil
50
+}
51
+
52
+func ParseBootstrapPeers(addrs []string) ([]BootstrapPeer, error) {
53
+ peers := make([]BootstrapPeer, len(addrs))
54
+ var err error
55
+ for i, addr := range addrs {
56
+ peers[i], err = ParseBootstrapPeer(addr)
57
+ if err != nil {
58
+ return nil, err
59
+ }
60
+ }
61
+ return peers, nil
62
+}
config/config.go
+8
-136
@@ -3,115 +3,17 @@ package config
3
4
import (
5
"bytes"
6
- "encoding/base64"
6
"encoding/json"
8
- "errors"
7
"fmt"
8
"os"
9
"path/filepath"
10
"strings"
11
14
- ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
15
- mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
16
-
17
- ic "github.com/jbenet/go-ipfs/p2p/crypto"
12
u "github.com/jbenet/go-ipfs/util"
13
)
14
15
var log = u.Logger("config")
16
23
-// Identity tracks the configuration of the local node's identity.
24
-type Identity struct {
25
- PeerID string
26
- PrivKey string
27
-}
28
-
29
-// Logs tracks the configuration of the event logger
30
-type Logs struct {
31
- Filename string
32
- MaxSizeMB uint64
33
- MaxBackups uint64
34
- MaxAgeDays uint64
35
-}
36
-
37
-// Datastore tracks the configuration of the datastore.
38
-type Datastore struct {
39
- Type string
40
- Path string
41
-}
42
-
43
-// Addresses stores the (string) multiaddr addresses for the node.
44
-type Addresses struct {
45
- Swarm []string // addresses for the swarm network
46
- API string // address for the local API (RPC)
47
- Gateway string // address to listen on for IPFS HTTP object gateway
48
-}
49
-
50
-// Mounts stores the (string) mount points
51
-type Mounts struct {
52
- IPFS string
53
- IPNS string
54
-}
55
-
56
-// BootstrapPeer is a peer used to bootstrap the network.
57
-type BootstrapPeer struct {
58
- Address string
59
- PeerID string // until multiaddr supports ipfs, use another field.
60
-}
61
-
62
-func (bp *BootstrapPeer) String() string {
63
- return bp.Address + "/" + bp.PeerID
64
-}
65
-
66
-func ParseBootstrapPeer(addr string) (BootstrapPeer, error) {
67
- // to be replaced with just multiaddr parsing, once ptp is a multiaddr protocol
68
- idx := strings.LastIndex(addr, "/")
69
- if idx == -1 {
70
- return BootstrapPeer{}, errors.New("invalid address")
71
- }
72
- addrS := addr[:idx]
73
- peeridS := addr[idx+1:]
74
-
75
- // make sure addrS parses as a multiaddr.
76
- if len(addrS) > 0 {
77
- maddr, err := ma.NewMultiaddr(addrS)
78
- if err != nil {
79
- return BootstrapPeer{}, err
80
- }
81
-
82
- addrS = maddr.String()
83
- }
84
-
85
- // make sure idS parses as a peer.ID
86
- _, err := mh.FromB58String(peeridS)
87
- if err != nil {
88
- return BootstrapPeer{}, err
89
- }
90
-
91
- return BootstrapPeer{
92
- Address: addrS,
93
- PeerID: peeridS,
94
- }, nil
95
-}
96
-
97
-func ParseBootstrapPeers(addrs []string) ([]BootstrapPeer, error) {
98
- peers := make([]BootstrapPeer, len(addrs))
99
- var err error
100
- for i, addr := range addrs {
101
- peers[i], err = ParseBootstrapPeer(addr)
102
- if err != nil {
103
- return nil, err
104
- }
105
- }
106
- return peers, nil
107
-}
108
-
109
-// Tour stores the ipfs tour read-list and resume point
110
-type Tour struct {
111
- Last string // last tour topic read
112
- // Done []string // all topics done so far
113
-}
114
-
17
// Config is used to load IPFS config files.
18
type Config struct {
19
Identity Identity // local node's peer identity
@@ -124,20 +26,14 @@ type Config struct {
26
Logs Logs // local node's event log configuration
27
}
28
127
-// DefaultPathRoot is the path to the default config dir location.
128
-const DefaultPathRoot = "~/.go-ipfs"
129
-
130
-// DefaultConfigFile is the filename of the configuration file
131
-const DefaultConfigFile = "config"
132
-
133
-// DefaultDataStoreDirectory is the directory to store all the local IPFS data.
134
-const DefaultDataStoreDirectory = "datastore"
135
-
136
-// EnvDir is the environment variable used to change the path root.
137
-const EnvDir = "IPFS_DIR"
138
-
139
-// LogsDefaultDirectory is the directory to store all IPFS event logs.
140
-var LogsDefaultDirectory = "logs"
29
+const (
30
+ // DefaultPathRoot is the path to the default config dir location.
31
+ DefaultPathRoot = "~/.go-ipfs"
32
+ // DefaultConfigFile is the filename of the configuration file
33
+ DefaultConfigFile = "config"
34
+ // EnvDir is the environment variable used to change the path root.
35
+ EnvDir = "IPFS_DIR"
36
+)
37
38
// PathRoot returns the default configuration root directory
39
func PathRoot() (string, error) {
@@ -163,36 +59,12 @@ func Path(configroot, extension string) (string, error) {
59
return filepath.Join(configroot, extension), nil
60
}
61
166
-// DataStorePath returns the default data store path given a configuration root
167
-// (set an empty string to have the default configuration root)
168
-func DataStorePath(configroot string) (string, error) {
169
- return Path(configroot, DefaultDataStoreDirectory)
170
-}
171
-
172
-// LogsPath returns the default path for event logs given a configuration root
173
-// (set an empty string to have the default configuration root)
174
-func LogsPath(configroot string) (string, error) {
175
- return Path(configroot, LogsDefaultDirectory)
176
-}
177
-
62
// Filename returns the configuration file path given a configuration root
63
// directory. If the configuration root directory is empty, use the default one
64
func Filename(configroot string) (string, error) {
65
return Path(configroot, DefaultConfigFile)
66
}
67
184
-// DecodePrivateKey is a helper to decode the users PrivateKey
185
-func (i *Identity) DecodePrivateKey(passphrase string) (ic.PrivKey, error) {
186
- pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)
187
- if err != nil {
188
- return nil, err
189
- }
190
-
191
- // currently storing key unencrypted. in the future we need to encrypt it.
192
- // TODO(security)
193
- return ic.UnmarshalPrivateKey(pkb)
194
-}
195
-
68
// HumanOutput gets a config value ready for printing
69
func HumanOutput(value interface{}) ([]byte, error) {
70
s, ok := value.(string)
config/datastore.go
new
+16
@@ -0,0 +1,16 @@
1
+package config
2
+
3
+// DefaultDataStoreDirectory is the directory to store all the local IPFS data.
4
+const DefaultDataStoreDirectory = "datastore"
5
+
6
+// Datastore tracks the configuration of the datastore.
7
+type Datastore struct {
8
+ Type string
9
+ Path string
10
+}
11
+
12
+// DataStorePath returns the default data store path given a configuration root
13
+// (set an empty string to have the default configuration root)
14
+func DataStorePath(configroot string) (string, error) {
15
+ return Path(configroot, DefaultDataStoreDirectory)
16
+}
config/identity.go
new
+24
@@ -0,0 +1,24 @@
1
+package config
2
+
3
+import (
4
+ "encoding/base64"
5
+ ic "github.com/jbenet/go-ipfs/p2p/crypto"
6
+)
7
+
8
+// Identity tracks the configuration of the local node's identity.
9
+type Identity struct {
10
+ PeerID string
11
+ PrivKey string
12
+}
13
+
14
+// DecodePrivateKey is a helper to decode the users PrivateKey
15
+func (i *Identity) DecodePrivateKey(passphrase string) (ic.PrivKey, error) {
16
+ pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)
17
+ if err != nil {
18
+ return nil, err
19
+ }
20
+
21
+ // currently storing key unencrypted. in the future we need to encrypt it.
22
+ // TODO(security)
23
+ return ic.UnmarshalPrivateKey(pkb)
24
+}
config/logs.go
new
+18
@@ -0,0 +1,18 @@
1
+package config
2
+
3
+// LogsDefaultDirectory is the directory to store all IPFS event logs.
4
+var LogsDefaultDirectory = "logs"
5
+
6
+// Logs tracks the configuration of the event logger
7
+type Logs struct {
8
+ Filename string
9
+ MaxSizeMB uint64
10
+ MaxBackups uint64
11
+ MaxAgeDays uint64
12
+}
13
+
14
+// LogsPath returns the default path for event logs given a configuration root
15
+// (set an empty string to have the default configuration root)
16
+func LogsPath(configroot string) (string, error) {
17
+ return Path(configroot, LogsDefaultDirectory)
18
+}
config/mounts.go
new
+7
@@ -0,0 +1,7 @@
1
+package config
2
+
3
+// Mounts stores the (string) mount points
4
+type Mounts struct {
5
+ IPFS string
6
+ IPNS string
7
+}
config/tour.go
new
+7
@@ -0,0 +1,7 @@
1
+package config
2
+
3
+// Tour stores the ipfs tour read-list and resume point
4
+type Tour struct {
5
+ Last string // last tour topic read
6
+ // Done []string // all topics done so far
7
+}