go-dvote/data/data.go
data/data.goRaw
1package data
2
3import (
4 "os"
5 "fmt"
6 "bytes"
7 "io/ioutil"
8 shell "github.com/ipfs/go-ipfs-api"
9)
10
11func publish(object []byte) string {
12 sh := shell.NewShell("localhost:5001")
13 cid, err := sh.Add(bytes.NewBuffer(object))
14 if err != nil {
15 fmt.Fprintf(os.Stderr, "error: %s", err)
16 os.Exit(1)
17 }
18 return cid
19}
20
21func pin(path string) {
22 sh := shell.NewShell("localhost:5001")
23 err := sh.Pin(path)
24 if err != nil{
25 fmt.Fprintf(os.Stderr, "error: %s", err)
26 os.Exit(1)
27 }
28}
29
30
31func retrieve(hash string) []byte {
32 sh := shell.NewShell("localhost:5001")
33 reader, err := sh.Cat(hash)
34 if err != nil {
35 fmt.Fprintf(os.Stderr, "error: %s", err)
36 os.Exit(1)
37 }
38 content, err := ioutil.ReadAll(reader)
39 if err != nil {
40 fmt.Fprintf(os.Stderr, "error: %s", err)
41 os.Exit(1)
42 }
43 return content
44}
45
46func PsSubscribe(topic string) *shell.PubSubSubscription {
47 sh := shell.NewShell("localhost:5001")
48 sub, err := sh.PubSubSubscribe(topic)
49 if err != nil {
50 fmt.Fprintf(os.Stderr, "error: %s", err)
51 os.Exit(1)
52 }
53 return sub
54}
55
56func PsPublish(topic, data string) {
57 sh := shell.NewShell("localhost:5001")
58 err := sh.PubSubPublish(topic, data)
59 if err != nil {
60 fmt.Fprintf(os.Stderr, "error: %s", err)
61 os.Exit(1)
62 }
63}