You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.2 KiB

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