This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpin.go
159 lines (131 loc) · 3.13 KB
/
pin.go
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
package httpapi
import (
"context"
"encoding/json"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)
type PinAPI HttpApi
type pinRefKeyObject struct {
Type string
}
type pinRefKeyList struct {
Keys map[string]pinRefKeyObject
}
type pin struct {
path iface.ResolvedPath
typ string
}
func (p *pin) Path() iface.ResolvedPath {
return p.path
}
func (p *pin) Type() string {
return p.typ
}
func (api *PinAPI) Add(ctx context.Context, p iface.Path, opts ...caopts.PinAddOption) error {
options, err := caopts.PinAddOptions(opts...)
if err != nil {
return err
}
return api.core().request("pin/add", p.String()).
Option("recursive", options.Recursive).Exec(ctx, nil)
}
func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]iface.Pin, error) {
options, err := caopts.PinLsOptions(opts...)
if err != nil {
return nil, err
}
var out pinRefKeyList
err = api.core().request("pin/ls").
Option("type", options.Type).Exec(ctx, &out)
if err != nil {
return nil, err
}
pins := make([]iface.Pin, 0, len(out.Keys))
for hash, p := range out.Keys {
c, err := cid.Parse(hash)
if err != nil {
return nil, err
}
pins = append(pins, &pin{typ: p.Type, path: iface.IpldPath(c)})
}
return pins, nil
}
func (api *PinAPI) Rm(ctx context.Context, p iface.Path) error {
return api.core().request("pin/rm", p.String()).Exec(ctx, nil)
}
func (api *PinAPI) Update(ctx context.Context, from iface.Path, to iface.Path, opts ...caopts.PinUpdateOption) error {
options, err := caopts.PinUpdateOptions(opts...)
if err != nil {
return err
}
return api.core().request("pin/update").
Option("unpin", options.Unpin).Exec(ctx, nil)
}
type pinVerifyRes struct {
Cid string
JOk bool `json:"Ok"`
JBadNodes []*badNode `json:"BadNodes,omitempty"`
}
func (r *pinVerifyRes) Ok() bool {
return r.JOk
}
func (r *pinVerifyRes) BadNodes() []iface.BadPinNode {
out := make([]iface.BadPinNode, len(r.JBadNodes))
for i, n := range r.JBadNodes {
out[i] = n
}
return out
}
type badNode struct {
Cid string
JErr string `json:"Err"`
}
func (n *badNode) Path() iface.ResolvedPath {
c, err := cid.Parse(n.Cid)
if err != nil {
return nil // todo: handle this better
}
return iface.IpldPath(c)
}
func (n *badNode) Err() error {
if n.JErr != "" {
return errors.New(n.JErr)
}
if _, err := cid.Parse(n.Cid); err != nil {
return err
}
return nil
}
func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) {
resp, err := api.core().request("pin/verify").Option("verbose", true).Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
res := make(chan iface.PinStatus)
go func() {
defer resp.Close()
defer close(res)
dec := json.NewDecoder(resp.Output)
for {
var out pinVerifyRes
if err := dec.Decode(&out); err != nil {
return // todo: handle non io.EOF somehow
}
select {
case res <- &out:
case <-ctx.Done():
return
}
}
}()
return res, nil
}
func (api *PinAPI) core() *HttpApi {
return (*HttpApi)(api)
}