-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation-pin-ls.go
91 lines (79 loc) · 2.04 KB
/
operation-pin-ls.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
package component
import (
"context"
"encoding/json"
"fmt"
"strings"
ipfsOptions "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/metadata"
)
// Handler for the "pin-ls" operation, which removes a pin
func (b *IPFSBinding) pinLsOperation(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
reqMetadata := &pinLsRequestMetadata{}
err := reqMetadata.FromMap(req.Metadata)
if err != nil {
return nil, err
}
opts, err := reqMetadata.PinLsOptions()
if err != nil {
return nil, err
}
ls, err := b.ipfsAPI.Pin().Ls(ctx, opts...)
if err != nil {
return nil, err
}
res := pinLsOperationResponse{}
for e := range ls {
err = e.Err()
if err != nil {
return nil, err
}
res = append(res, pinLsOperationResponseItem{
Type: e.Type(),
Cid: e.Path().Cid().String(),
})
}
j, _ := json.Marshal(res)
return &bindings.InvokeResponse{
Data: j,
Metadata: nil,
}, nil
}
type pinLsOperationResponseItem struct {
Cid string `json:"cid,omitempty"`
Type string `json:"type,omitempty"`
}
type pinLsOperationResponse []pinLsOperationResponseItem
type pinLsRequestMetadata struct {
Type *string `mapstructure:"type"`
}
func (m *pinLsRequestMetadata) FromMap(mp map[string]string) (err error) {
if len(mp) > 0 {
err = metadata.DecodeMetadata(mp, m)
if err != nil {
return err
}
}
return nil
}
func (m *pinLsRequestMetadata) PinLsOptions() ([]ipfsOptions.PinLsOption, error) {
opts := []ipfsOptions.PinLsOption{}
if m.Type != nil {
switch strings.ToLower(*m.Type) {
case "direct":
opts = append(opts, ipfsOptions.Pin.Ls.Direct())
case "recursive":
opts = append(opts, ipfsOptions.Pin.Ls.Recursive())
case "indirect":
opts = append(opts, ipfsOptions.Pin.Ls.Indirect())
case "all":
opts = append(opts, ipfsOptions.Pin.Ls.All())
default:
return nil, fmt.Errorf("invalid value for metadata property 'type'")
}
} else {
opts = append(opts, ipfsOptions.Pin.Ls.All())
}
return opts, nil
}