local_refs.go 653 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package ipfs_api
  2. import (
  3. "encoding/json"
  4. "context"
  5. "github.com/ipfs/go-ipfs-api"
  6. )
  7. func (s *MyShell) LocalRefs() (<-chan string, error) {
  8. ctx := context.Background()
  9. req := shell.NewRequest(ctx, s.Url, "refs/local")
  10. resp, err := req.Send(s.Client)
  11. if err != nil {
  12. return nil, err
  13. }
  14. if resp.Error != nil {
  15. return nil, resp.Error
  16. }
  17. out := make(chan string)
  18. go func() {
  19. var ref struct {
  20. Ref string
  21. }
  22. defer resp.Close()
  23. defer close(out)
  24. dec := json.NewDecoder(resp.Output)
  25. for {
  26. err := dec.Decode(&ref)
  27. if err != nil {
  28. return
  29. }
  30. if len(ref.Ref) > 0 {
  31. out <- ref.Ref
  32. }
  33. }
  34. }()
  35. return out, nil
  36. }