Commit 57b3ffa5 authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat: new core constructor + config options (Standard, Online, Offline)

parent 007ffd40
......@@ -121,7 +121,7 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
func addTheWelcomeFile(conf *config.Config) error {
// TODO extract this file creation operation into a function
ctx, cancel := context.WithCancel(context.Background())
nd, err := core.NewIpfsNode(ctx, conf, false)
nd, err := core.NewIPFSNode(ctx, core.Offline(conf))
if err != nil {
return err
}
......
......@@ -188,7 +188,7 @@ func (i *cmdInvocation) constructNodeFunc(ctx context.Context) func() (*core.Ipf
// ok everything is good. set it on the invocation (for ownership)
// and return it.
i.node, err = core.NewIpfsNode(ctx, cfg, cmdctx.Online)
i.node, err = core.NewIPFSNode(ctx, core.Standard(cfg, cmdctx.Online))
return i.node, err
}
}
......
package core
import (
"errors"
"fmt"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
......@@ -75,7 +76,37 @@ type Mounts struct {
Ipns mount.Mount
}
var errTODO = errors.New("TODO")
type Configuration *IpfsNode // define a different type
type ConfigOption func(ctx context.Context) (Configuration, error)
func NewIPFSNode(ctx context.Context, option ConfigOption) (*IpfsNode, error) {
config, err := option(ctx)
if err != nil {
return nil, err
}
return config, nil
}
func Offline(cfg *config.Config) ConfigOption {
return Standard(cfg, false)
}
func Online(cfg *config.Config) ConfigOption {
return Standard(cfg, true)
}
// DEPRECATED: use Online, Offline functions
func Standard(cfg *config.Config, online bool) ConfigOption {
return func(ctx context.Context) (Configuration, error) {
return NewIpfsNode(ctx, cfg, online)
}
}
// NewIpfsNode constructs a new IpfsNode based on the given config.
// DEPRECATED: use `NewIPFSNode`
func NewIpfsNode(ctx context.Context, cfg *config.Config, online bool) (n *IpfsNode, err error) {
success := false // flip to true after all sub-system inits succeed
defer func() {
......
......@@ -45,14 +45,14 @@ func TestInitialization(t *testing.T) {
}
for i, c := range good {
n, err := NewIpfsNode(ctx, c, false)
n, err := NewIPFSNode(ctx, Standard(c, false))
if n == nil || err != nil {
t.Error("Should have constructed.", i, err)
}
}
for i, c := range bad {
n, err := NewIpfsNode(ctx, c, false)
n, err := NewIPFSNode(ctx, Standard(c, false))
if n != nil || err == nil {
t.Error("Should have failed to construct.", i)
}
......
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