feat(init) init logs upon config initialization
TODO allow user dir override License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com> # TYPES # feat # fix # docs # style (formatting, missing semi colons, etc; no code change): # refactor # test (adding missing tests, refactoring tests; no production code change) # chore (updating grunt tasks etc; no production code change) Signed-off-by: Brian Tiger Chow <brian.holderchow@gmail.com>
Brian Tiger Chow committed
Nov 16, 2014 at 03:56 UTC
3a927cbcec96952145da8f7d630a91f965948ed1
2 files changed
+53
-1
cmd/ipfs2/init.go
+35
-1
@@ -3,9 +3,9 @@ package main
3
import (
4
"bytes"
5
"encoding/base64"
6
- "errors"
6
"fmt"
7
"os"
8
+ "path"
9
"path/filepath"
10
11
cmds "github.com/jbenet/go-ipfs/commands"
@@ -16,6 +16,7 @@ import (
16
chunk "github.com/jbenet/go-ipfs/importer/chunk"
17
peer "github.com/jbenet/go-ipfs/peer"
18
u "github.com/jbenet/go-ipfs/util"
19
+ errors "github.com/jbenet/go-ipfs/util/debugerror"
20
)
21
22
var initCmd = &cmds.Command{
@@ -29,6 +30,11 @@ var initCmd = &cmds.Command{
30
cmds.StringOption("passphrase", "p", "Passphrase for encrypting the private key"),
31
cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"),
32
cmds.StringOption("datastore", "d", "Location for the IPFS data store"),
33
+
34
+ // TODO need to decide whether to expose the override as a file or a
35
+ // directory. That is: should we allow the user to also specify the
36
+ // name of the file?
37
+ // TODO cmds.StringOption("event-logs", "l", "Location for machine-readable event logs"),
38
},
39
Run: func(req cmds.Request) (interface{}, error) {
40
@@ -97,6 +103,7 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
103
return nil, err
104
}
105
106
+ // TODO extract this file creation operation into a function
107
nd, err := core.NewIpfsNode(conf, false)
108
if err != nil {
109
return nil, err
@@ -150,6 +157,11 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
157
return nil, err
158
}
159
160
+ logConfig, err := initLogs("") // TODO allow user to override dir
161
+ if err != nil {
162
+ return nil, err
163
+ }
164
+
165
conf := &config.Config{
166
167
// setup the node addresses.
@@ -168,6 +180,8 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
180
181
Datastore: ds,
182
183
+ Logs: logConfig,
184
+
185
Identity: identity,
186
187
// setup the node mount points.
@@ -214,10 +228,30 @@ func identityConfig(nbits int) (config.Identity, error) {
228
return ident, err
229
}
230
ident.PeerID = id.Pretty()
231
+ fmt.Println("generating key pair...")
232
233
return ident, nil
234
}
235
236
+func initLogs(logpath string) (config.Logs, error) {
237
+ if len(logpath) == 0 {
238
+ var err error
239
+ logpath, err = config.LogsPath("")
240
+ if err != nil {
241
+ return config.Logs{}, errors.Wrap(err)
242
+ }
243
+ }
244
+
245
+ err := initCheckDir(logpath)
246
+ if err != nil {
247
+ return config.Logs{}, errors.Errorf("logs: %s", err)
248
+ }
249
+
250
+ return config.Logs{
251
+ Filename: path.Join(logpath, "events.log"),
252
+ }, nil
253
+}
254
+
255
// initCheckDir ensures the directory exists and is writable
256
func initCheckDir(path string) error {
257
// Construct the path if missing
config/config.go
+18
@@ -20,6 +20,14 @@ type Identity struct {
20
PrivKey string
21
}
22
23
+// Logs tracks the configuration of the event logger
24
+type Logs struct {
25
+ Filename string
26
+ MaxSizeMB uint64
27
+ MaxBackups uint64
28
+ MaxAgeDays uint64
29
+}
30
+
31
// Datastore tracks the configuration of the datastore.
32
type Datastore struct {
33
Type string
@@ -63,6 +71,7 @@ type Config struct {
71
Version Version // local node's version management
72
Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
73
Tour Tour // local node's tour position
74
+ Logs Logs // local node's event log configuration
75
}
76
77
// DefaultPathRoot is the path to the default config dir location.
@@ -77,6 +86,9 @@ const DefaultDataStoreDirectory = "datastore"
86
// EnvDir is the environment variable used to change the path root.
87
const EnvDir = "IPFS_DIR"
88
89
+// LogsDefaultDirectory is the directory to store all IPFS event logs.
90
+var LogsDefaultDirectory = "logs"
91
+
92
// PathRoot returns the default configuration root directory
93
func PathRoot() (string, error) {
94
dir := os.Getenv(EnvDir)
@@ -107,6 +119,12 @@ func DataStorePath(configroot string) (string, error) {
119
return Path(configroot, DefaultDataStoreDirectory)
120
}
121
122
+// LogsPath returns the default path for event logs given a configuration root
123
+// (set an empty string to have the default configuration root)
124
+func LogsPath(configroot string) (string, error) {
125
+ return Path(configroot, LogsDefaultDirectory)
126
+}
127
+
128
// Filename returns the configuration file path given a configuration root
129
// directory. If the configuration root directory is empty, use the default one
130
func Filename(configroot string) (string, error) {