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