forked from ipfs/boxo
/
main.go
92 lines (75 loc) · 1.75 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/ipfs/go-cid"
pinclient "github.com/stateless-minds/boxo/pinning/remote/client"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
url, ok := os.LookupEnv("PS_URL")
if !ok {
panic("No Pinning Service URL found")
}
key, ok := os.LookupEnv("PS_KEY")
if !ok {
panic("No Pinning Service API Key found")
}
c := pinclient.NewClient(url, key)
ipfsPgCid, err := cid.Parse("bafybeiayvrj27f65vbecspbnuavehcb3znvnt2strop2rfbczupudoizya")
if err != nil {
panic(err)
}
libp2pCid, err := cid.Parse("bafybeiejgrxo4p4uofgfzvlg5twrg5w7tfwpf7aciiswfacfbdpevg2xfy")
if err != nil {
panic(err)
}
_ = ipfsPgCid
listPins(ctx, c)
fmt.Println("Adding libp2p home page")
ps, err := c.Add(ctx, libp2pCid, pinclient.PinOpts.WithName("libp2p"))
if err == nil {
fmt.Printf("PinStatus: %v \n", ps)
} else {
fmt.Println(err)
}
listPins(ctx, c)
fmt.Println("Check on pin status")
if ps == nil {
panic("Skipping pin status check because the pin is null")
}
var pinned bool
for !pinned {
status, err := c.GetStatusByID(ctx, ps.GetRequestId())
if err == nil {
fmt.Println(status.GetStatus())
pinned = status.GetStatus() == pinclient.StatusPinned
} else {
fmt.Println(err)
}
time.Sleep(time.Millisecond * 500)
}
listPins(ctx, c)
fmt.Println("Delete pin")
err = c.DeleteByID(ctx, ps.GetRequestId())
if err == nil {
fmt.Println("Successfully deleted pin")
} else {
fmt.Println(err)
}
listPins(ctx, c)
}
func listPins(ctx context.Context, c *pinclient.Client) {
fmt.Println("List all pins")
pins, err := c.LsSync(ctx)
if err != nil {
fmt.Println(err)
} else {
for _, p := range pins {
fmt.Printf("Pin: %v \n", p)
}
}
}