mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-24 20:07:45 +08:00
go fmt
This commit is contained in:
parent
2b9d67df81
commit
2d7f2473a5
74
core/core.go
74
core/core.go
@ -1,62 +1,62 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
ds "github.com/jbenet/datastore.go"
|
||||
"fmt"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
"fmt"
|
||||
ds "github.com/jbenet/datastore.go"
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
)
|
||||
|
||||
// IPFS Core module. It represents an IPFS instance.
|
||||
|
||||
type IpfsNode struct {
|
||||
|
||||
// the node's configuration
|
||||
Config *config.Config
|
||||
// the node's configuration
|
||||
Config *config.Config
|
||||
|
||||
// the local node's identity
|
||||
Identity *peer.Peer
|
||||
// the local node's identity
|
||||
Identity *peer.Peer
|
||||
|
||||
// the book of other nodes (a map of Peer instances)
|
||||
PeerBook *peer.PeerBook
|
||||
// the book of other nodes (a map of Peer instances)
|
||||
PeerBook *peer.PeerBook
|
||||
|
||||
// the local datastore
|
||||
Datastore ds.Datastore
|
||||
// the local datastore
|
||||
Datastore ds.Datastore
|
||||
|
||||
// the network message stream
|
||||
// Network *netmux.Netux
|
||||
// the network message stream
|
||||
// Network *netmux.Netux
|
||||
|
||||
// the routing system. recommend ipfs-dht
|
||||
// Routing *routing.Routing
|
||||
// the routing system. recommend ipfs-dht
|
||||
// Routing *routing.Routing
|
||||
|
||||
// the block exchange + strategy (bitswap)
|
||||
// BitSwap *bitswap.BitSwap
|
||||
// the block exchange + strategy (bitswap)
|
||||
// BitSwap *bitswap.BitSwap
|
||||
|
||||
// the block service, get/add blocks.
|
||||
// Blocks *blocks.BlockService
|
||||
// the block service, get/add blocks.
|
||||
// Blocks *blocks.BlockService
|
||||
|
||||
// the path resolution system
|
||||
// Resolver *resolver.PathResolver
|
||||
// the path resolution system
|
||||
// Resolver *resolver.PathResolver
|
||||
|
||||
// the name system, resolves paths to hashes
|
||||
// Namesys *namesys.Namesys
|
||||
// the name system, resolves paths to hashes
|
||||
// Namesys *namesys.Namesys
|
||||
}
|
||||
|
||||
func NewIpfsNode(cfg *config.Config) (*IpfsNode, error) {
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("configuration required.")
|
||||
}
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("configuration required.")
|
||||
}
|
||||
|
||||
d, err := makeDatastore(cfg.Datastore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d, err := makeDatastore(cfg.Datastore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
n := &IpfsNode{
|
||||
Config: cfg,
|
||||
PeerBook: &peer.PeerBook{},
|
||||
Datastore: d,
|
||||
}
|
||||
n := &IpfsNode{
|
||||
Config: cfg,
|
||||
PeerBook: &peer.PeerBook{},
|
||||
Datastore: d,
|
||||
}
|
||||
|
||||
return n, nil
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@ -1,35 +1,35 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDatastores(t *testing.T) {
|
||||
|
||||
good := []*config.Config {
|
||||
&config.Config{ Datastore: &config.Datastore{Type: "memory"} },
|
||||
&config.Config{ Datastore: &config.Datastore{Type: "leveldb", Path: ".testdb"} },
|
||||
}
|
||||
good := []*config.Config{
|
||||
&config.Config{Datastore: &config.Datastore{Type: "memory"}},
|
||||
&config.Config{Datastore: &config.Datastore{Type: "leveldb", Path: ".testdb"}},
|
||||
}
|
||||
|
||||
bad := []*config.Config {
|
||||
&config.Config{ Datastore: &config.Datastore{} },
|
||||
&config.Config{ Datastore: &config.Datastore{Type: "badtype"} },
|
||||
&config.Config{ },
|
||||
nil,
|
||||
}
|
||||
bad := []*config.Config{
|
||||
&config.Config{Datastore: &config.Datastore{}},
|
||||
&config.Config{Datastore: &config.Datastore{Type: "badtype"}},
|
||||
&config.Config{},
|
||||
nil,
|
||||
}
|
||||
|
||||
for i, c := range(good) {
|
||||
n, err := NewIpfsNode(c)
|
||||
if n == nil || err != nil {
|
||||
t.Error("Should have constructed.", i, err)
|
||||
}
|
||||
}
|
||||
for i, c := range good {
|
||||
n, err := NewIpfsNode(c)
|
||||
if n == nil || err != nil {
|
||||
t.Error("Should have constructed.", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range(bad) {
|
||||
n, err := NewIpfsNode(c)
|
||||
if n != nil || err == nil {
|
||||
t.Error("Should have failed to construct.", i)
|
||||
}
|
||||
}
|
||||
for i, c := range bad {
|
||||
n, err := NewIpfsNode(c)
|
||||
if n != nil || err == nil {
|
||||
t.Error("Should have failed to construct.", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,31 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
ds "github.com/jbenet/datastore.go"
|
||||
"fmt"
|
||||
lds "github.com/jbenet/datastore.go/leveldb"
|
||||
"fmt"
|
||||
ds "github.com/jbenet/datastore.go"
|
||||
lds "github.com/jbenet/datastore.go/leveldb"
|
||||
config "github.com/jbenet/go-ipfs/config"
|
||||
)
|
||||
|
||||
func makeDatastore(cfg *config.Datastore) (ds.Datastore, error) {
|
||||
if cfg == nil || len(cfg.Type) == 0 {
|
||||
return nil, fmt.Errorf("config datastore.type required")
|
||||
}
|
||||
if cfg == nil || len(cfg.Type) == 0 {
|
||||
return nil, fmt.Errorf("config datastore.type required")
|
||||
}
|
||||
|
||||
switch cfg.Type {
|
||||
case "leveldb": return makeLevelDBDatastore(cfg)
|
||||
case "memory": return ds.NewMapDatastore(), nil
|
||||
}
|
||||
switch cfg.Type {
|
||||
case "leveldb":
|
||||
return makeLevelDBDatastore(cfg)
|
||||
case "memory":
|
||||
return ds.NewMapDatastore(), nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
|
||||
return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
|
||||
}
|
||||
|
||||
func makeLevelDBDatastore(cfg *config.Datastore) (ds.Datastore, error) {
|
||||
if len(cfg.Path) == 0 {
|
||||
return nil, fmt.Errorf("config datastore.path required for leveldb")
|
||||
}
|
||||
if len(cfg.Path) == 0 {
|
||||
return nil, fmt.Errorf("config datastore.path required for leveldb")
|
||||
}
|
||||
|
||||
return lds.NewDatastore(cfg.Path, nil)
|
||||
return lds.NewDatastore(cfg.Path, nil)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user