@cryptotaxi247 / kubo / commits / 57b3ffa53

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

Brian Tiger Chow committed Jan 10, 2015 at 17:23 UTC 57b3ffa533026d0597426f4f335ad85c93217e35
4 files changed +35 -4
cmd/ipfs/init.go
+1 -1
@@ -121,7 +121,7 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
121 func addTheWelcomeFile(conf *config.Config) error {
122 // TODO extract this file creation operation into a function
123 ctx, cancel := context.WithCancel(context.Background())
124 - nd, err := core.NewIpfsNode(ctx, conf, false)
124 + nd, err := core.NewIPFSNode(ctx, core.Offline(conf))
125 if err != nil {
126 return err
127 }
cmd/ipfs/main.go
+1 -1
@@ -188,7 +188,7 @@ func (i *cmdInvocation) constructNodeFunc(ctx context.Context) func() (*core.Ipf
188
189 // ok everything is good. set it on the invocation (for ownership)
190 // and return it.
191 - i.node, err = core.NewIpfsNode(ctx, cfg, cmdctx.Online)
191 + i.node, err = core.NewIPFSNode(ctx, core.Standard(cfg, cmdctx.Online))
192 return i.node, err
193 }
194 }
core/core.go
+31
@@ -1,6 +1,7 @@
1 package core
2
3 import (
4 + "errors"
5 "fmt"
6
7 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
@@ -75,7 +76,37 @@ type Mounts struct {
76 Ipns mount.Mount
77 }
78
79 +var errTODO = errors.New("TODO")
80 +
81 +type Configuration *IpfsNode // define a different type
82 +
83 +type ConfigOption func(ctx context.Context) (Configuration, error)
84 +
85 +func NewIPFSNode(ctx context.Context, option ConfigOption) (*IpfsNode, error) {
86 + config, err := option(ctx)
87 + if err != nil {
88 + return nil, err
89 + }
90 + return config, nil
91 +}
92 +
93 +func Offline(cfg *config.Config) ConfigOption {
94 + return Standard(cfg, false)
95 +}
96 +
97 +func Online(cfg *config.Config) ConfigOption {
98 + return Standard(cfg, true)
99 +}
100 +
101 +// DEPRECATED: use Online, Offline functions
102 +func Standard(cfg *config.Config, online bool) ConfigOption {
103 + return func(ctx context.Context) (Configuration, error) {
104 + return NewIpfsNode(ctx, cfg, online)
105 + }
106 +}
107 +
108 // NewIpfsNode constructs a new IpfsNode based on the given config.
109 +// DEPRECATED: use `NewIPFSNode`
110 func NewIpfsNode(ctx context.Context, cfg *config.Config, online bool) (n *IpfsNode, err error) {
111 success := false // flip to true after all sub-system inits succeed
112 defer func() {
core/core_test.go
+2 -2
@@ -45,14 +45,14 @@ func TestInitialization(t *testing.T) {
45 }
46
47 for i, c := range good {
48 - n, err := NewIpfsNode(ctx, c, false)
48 + n, err := NewIPFSNode(ctx, Standard(c, false))
49 if n == nil || err != nil {
50 t.Error("Should have constructed.", i, err)
51 }
52 }
53
54 for i, c := range bad {
55 - n, err := NewIpfsNode(ctx, c, false)
55 + n, err := NewIPFSNode(ctx, Standard(c, false))
56 if n != nil || err == nil {
57 t.Error("Should have failed to construct.", i)
58 }