-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathnode.go
More file actions
132 lines (106 loc) · 2.84 KB
/
node.go
File metadata and controls
132 lines (106 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
config "github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"
iface "github.com/ipfs/kubo/core/coreiface"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/ipfs/kubo/core/node/libp2p"
"github.com/ipfs/kubo/plugin/loader"
"github.com/ipfs/kubo/repo/fsrepo"
)
type CfgOpt func(*config.Config)
func spawn(ctx context.Context) (iface.CoreAPI, error) {
fmt.Fprint(os.Stderr, "Downloading from IPFS node with existing repo... ")
defaultPath, err := config.PathRoot()
if err != nil {
// shouldn't be possible
return nil, err
}
if err := setupPlugins(defaultPath); err != nil {
return nil, err
}
ipfs, err := open(ctx, defaultPath)
if err != nil {
fmt.Fprintln(os.Stderr, "failed")
return tmpNode(ctx)
}
return ipfs, nil
}
func setupPlugins(path string) error {
// Load plugins. This will skip the repo if not available.
plugins, err := loader.NewPluginLoader(filepath.Join(path, "plugins"))
if err != nil {
return fmt.Errorf("error loading plugins: %s", err)
}
if err := plugins.Initialize(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
if err := plugins.Inject(); err != nil {
return fmt.Errorf("error initializing plugins: %s", err)
}
return nil
}
func open(ctx context.Context, repoPath string) (iface.CoreAPI, error) {
// Open the repo
r, err := fsrepo.Open(repoPath)
if err != nil {
return nil, err
}
// Construct the node
node, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Routing: libp2p.DHTClientOption,
Repo: r,
})
if err != nil {
return nil, err
}
return coreapi.NewCoreAPI(node)
}
func temp(ctx context.Context) (iface.CoreAPI, error) {
defaultPath, err := config.PathRoot()
if err != nil {
// shouldn't be possible
return nil, err
}
if err := setupPlugins(defaultPath); err != nil {
return nil, err
}
return tmpNode(ctx)
}
func tmpNode(ctx context.Context) (iface.CoreAPI, error) {
fmt.Fprint(os.Stderr, "Downloading from IPFS node with temporary repo... ")
dir, err := os.MkdirTemp("", "ipfs-shell")
if err != nil {
return nil, fmt.Errorf("failed to get temp dir: %s", err)
}
// Cleanup temp dir on exit
addCleanup(func() error {
return os.RemoveAll(dir)
})
identity, err := config.CreateIdentity(io.Discard, []options.KeyGenerateOption{
options.Key.Type(options.Ed25519Key),
})
if err != nil {
return nil, err
}
cfg, err := config.InitWithIdentity(identity)
if err != nil {
return nil, err
}
// configure the temporary node
cfg.Addresses.Announce = nil // Don't announce any addresses
cfg.Routing.Type = config.NewOptionalString("dhtclient")
cfg.Datastore.NoSync = true
err = fsrepo.Init(dir, cfg)
if err != nil {
return nil, fmt.Errorf("failed to init ephemeral node: %s", err)
}
return open(ctx, dir)
}