-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathipfs.go
More file actions
181 lines (164 loc) · 5.66 KB
/
ipfs.go
File metadata and controls
181 lines (164 loc) · 5.66 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package harness
import (
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strings"
. "github.com/ipfs/kubo/test/cli/testutils"
)
func (n *Node) IPFSCommands() []string {
res := n.IPFS("commands").Stdout.String()
res = strings.TrimSpace(res)
split := SplitLines(res)
var cmds []string
for _, line := range split {
trimmed := strings.TrimSpace(line)
if trimmed == "ipfs" {
continue
}
cmds = append(cmds, trimmed)
}
return cmds
}
func (n *Node) SetIPFSConfig(key string, val any, flags ...string) {
valBytes, err := json.Marshal(val)
if err != nil {
log.Panicf("marshling config for key '%s': %s", key, err)
}
valStr := string(valBytes)
args := []string{"config", "--json"}
args = append(args, flags...)
args = append(args, key, valStr)
n.IPFS(args...)
// validate the config was set correctly
// Create a new value which is a pointer to the same type as the source.
var newVal any
if val != nil {
// If it is not nil grab the type with reflect.
newVal = reflect.New(reflect.TypeOf(val)).Interface()
} else {
// else just set a pointer to an any.
var anything any
newVal = &anything
}
n.GetIPFSConfig(key, newVal)
// dereference newVal using reflect to load the resulting value
if !reflect.DeepEqual(val, reflect.ValueOf(newVal).Elem().Interface()) {
log.Panicf("key '%s' did not retain value '%s' after it was set, got '%s'", key, val, newVal)
}
}
func (n *Node) GetIPFSConfig(key string, val any) {
res := n.IPFS("config", key)
valStr := strings.TrimSpace(res.Stdout.String())
// only when the result is a string is the result not well-formed JSON,
// so check the value type and add quotes if it's expected to be a string
reflectVal := reflect.ValueOf(val)
if reflectVal.Kind() == reflect.Pointer && reflectVal.Elem().Kind() == reflect.String {
valStr = fmt.Sprintf(`"%s"`, valStr)
}
err := json.Unmarshal([]byte(valStr), val)
if err != nil {
log.Fatalf("unmarshaling config for key '%s', value '%s': %s", key, valStr, err)
}
}
func (n *Node) IPFSAddStr(content string, args ...string) string {
log.Debugf("node %d adding content '%s' with args: %v", n.ID, PreviewStr(content), args)
return n.IPFSAdd(strings.NewReader(content), args...)
}
// IPFSAddDeterministic produces a CID of a file of a certain size, filled with deterministically generated bytes based on some seed.
// Size is specified as a humanize string (e.g., "256KiB", "1MiB").
// This ensures deterministic CID on the other end, that can be used in tests.
func (n *Node) IPFSAddDeterministic(size string, seed string, args ...string) string {
log.Debugf("node %d adding %s of deterministic pseudo-random data with seed %q and args: %v", n.ID, size, seed, args)
reader, err := DeterministicRandomReader(size, seed)
if err != nil {
panic(err)
}
return n.IPFSAdd(reader, args...)
}
// IPFSAddDeterministicBytes produces a CID of a file of exactly `size` bytes, filled with deterministically generated bytes based on some seed.
// Use this when exact byte precision is needed (e.g., threshold tests at T and T+1 bytes).
func (n *Node) IPFSAddDeterministicBytes(size int64, seed string, args ...string) string {
log.Debugf("node %d adding %d bytes of deterministic pseudo-random data with seed %q and args: %v", n.ID, size, seed, args)
reader, err := DeterministicRandomReaderBytes(size, seed)
if err != nil {
panic(err)
}
return n.IPFSAdd(reader, args...)
}
func (n *Node) IPFSAdd(content io.Reader, args ...string) string {
log.Debugf("node %d adding with args: %v", n.ID, args)
fullArgs := []string{"add", "-q"}
fullArgs = append(fullArgs, args...)
res := n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: fullArgs,
CmdOpts: []CmdOpt{RunWithStdin(content)},
})
out := strings.TrimSpace(res.Stdout.String())
log.Debugf("add result: %q", out)
return out
}
func (n *Node) IPFSBlockPut(content io.Reader, args ...string) string {
log.Debugf("node %d block put with args: %v", n.ID, args)
fullArgs := []string{"block", "put"}
fullArgs = append(fullArgs, args...)
res := n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: fullArgs,
CmdOpts: []CmdOpt{RunWithStdin(content)},
})
out := strings.TrimSpace(res.Stdout.String())
log.Debugf("block put result: %q", out)
return out
}
func (n *Node) IPFSDAGPut(content io.Reader, args ...string) string {
log.Debugf("node %d dag put with args: %v", n.ID, args)
fullArgs := []string{"dag", "put"}
fullArgs = append(fullArgs, args...)
res := n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: fullArgs,
CmdOpts: []CmdOpt{RunWithStdin(content)},
})
out := strings.TrimSpace(res.Stdout.String())
log.Debugf("dag put result: %q", out)
return out
}
func (n *Node) IPFSDagImport(content io.Reader, cid string, args ...string) error {
log.Debugf("node %d dag import with args: %v", n.ID, args)
fullArgs := []string{"dag", "import", "--pin-roots=false"}
fullArgs = append(fullArgs, args...)
res := n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: fullArgs,
CmdOpts: []CmdOpt{RunWithStdin(content)},
})
if res.Err != nil {
return res.Err
}
res = n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: []string{"block", "stat", "--offline", cid},
})
return res.Err
}
// IPFSDagExport exports a DAG rooted at cid to a CAR file at carPath.
func (n *Node) IPFSDagExport(cid string, carPath string, args ...string) error {
log.Debugf("node %d dag export of %s to %q with args: %v", n.ID, cid, carPath, args)
car, err := os.Create(carPath)
if err != nil {
return err
}
defer car.Close()
fullArgs := append([]string{"dag", "export"}, args...)
fullArgs = append(fullArgs, cid)
res := n.Runner.MustRun(RunRequest{
Path: n.IPFSBin,
Args: fullArgs,
CmdOpts: []CmdOpt{RunWithStdout(car)},
})
return res.Err
}