Commit 3a927cbc authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat(init) init logs upon config initialization

TODO allow user dir override

License: MIT
Signed-off-by: default avatarBrian 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: default avatarBrian Tiger Chow <brian.holderchow@gmail.com>
parent 183dd990
......@@ -3,9 +3,9 @@ package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"os"
"path"
"path/filepath"
cmds "github.com/jbenet/go-ipfs/commands"
......@@ -16,6 +16,7 @@ import (
chunk "github.com/jbenet/go-ipfs/importer/chunk"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
errors "github.com/jbenet/go-ipfs/util/debugerror"
)
var initCmd = &cmds.Command{
......@@ -29,6 +30,11 @@ var initCmd = &cmds.Command{
cmds.StringOption("passphrase", "p", "Passphrase for encrypting the private key"),
cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"),
cmds.StringOption("datastore", "d", "Location for the IPFS data store"),
// TODO need to decide whether to expose the override as a file or a
// directory. That is: should we allow the user to also specify the
// name of the file?
// TODO cmds.StringOption("event-logs", "l", "Location for machine-readable event logs"),
},
Run: func(req cmds.Request) (interface{}, error) {
......@@ -97,6 +103,7 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
return nil, err
}
// TODO extract this file creation operation into a function
nd, err := core.NewIpfsNode(conf, false)
if err != nil {
return nil, err
......@@ -150,6 +157,11 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
return nil, err
}
logConfig, err := initLogs("") // TODO allow user to override dir
if err != nil {
return nil, err
}
conf := &config.Config{
// setup the node addresses.
......@@ -168,6 +180,8 @@ func initConfig(configFilename string, dspathOverride string, nBitsForKeypair in
Datastore: ds,
Logs: logConfig,
Identity: identity,
// setup the node mount points.
......@@ -214,10 +228,30 @@ func identityConfig(nbits int) (config.Identity, error) {
return ident, err
}
ident.PeerID = id.Pretty()
fmt.Println("generating key pair...")
return ident, nil
}
func initLogs(logpath string) (config.Logs, error) {
if len(logpath) == 0 {
var err error
logpath, err = config.LogsPath("")
if err != nil {
return config.Logs{}, errors.Wrap(err)
}
}
err := initCheckDir(logpath)
if err != nil {
return config.Logs{}, errors.Errorf("logs: %s", err)
}
return config.Logs{
Filename: path.Join(logpath, "events.log"),
}, nil
}
// initCheckDir ensures the directory exists and is writable
func initCheckDir(path string) error {
// Construct the path if missing
......
......@@ -20,6 +20,14 @@ type Identity struct {
PrivKey string
}
// Logs tracks the configuration of the event logger
type Logs struct {
Filename string
MaxSizeMB uint64
MaxBackups uint64
MaxAgeDays uint64
}
// Datastore tracks the configuration of the datastore.
type Datastore struct {
Type string
......@@ -63,6 +71,7 @@ type Config struct {
Version Version // local node's version management
Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
Tour Tour // local node's tour position
Logs Logs // local node's event log configuration
}
// DefaultPathRoot is the path to the default config dir location.
......@@ -77,6 +86,9 @@ const DefaultDataStoreDirectory = "datastore"
// EnvDir is the environment variable used to change the path root.
const EnvDir = "IPFS_DIR"
// LogsDefaultDirectory is the directory to store all IPFS event logs.
var LogsDefaultDirectory = "logs"
// PathRoot returns the default configuration root directory
func PathRoot() (string, error) {
dir := os.Getenv(EnvDir)
......@@ -107,6 +119,12 @@ func DataStorePath(configroot string) (string, error) {
return Path(configroot, DefaultDataStoreDirectory)
}
// LogsPath returns the default path for event logs given a configuration root
// (set an empty string to have the default configuration root)
func LogsPath(configroot string) (string, error) {
return Path(configroot, LogsDefaultDirectory)
}
// Filename returns the configuration file path given a configuration root
// directory. If the configuration root directory is empty, use the default one
func Filename(configroot string) (string, error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment