arnaucube
arnaucube/go-dvotePublic Self-hosted repository
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}