OSDN Git Service

add ipfs package
[bytom/vapor.git] / vendor / github.com / ipfs / go-ipfs-api / ipns.go
1 package shell
2
3 import (
4         "context"
5         "time"
6 )
7
8 type PublishResponse struct {
9         Name  string `json:"name"`
10         Value string `json:"value"`
11 }
12
13 // Publish updates a mutable name to point to a given value
14 func (s *Shell) Publish(node string, value string) error {
15         var pubResp PublishResponse
16         req := s.Request("name/publish")
17         if node != "" {
18                 req.Arguments(node)
19         }
20         req.Arguments(value)
21
22         return req.Exec(context.Background(), &pubResp)
23 }
24
25 // PublishWithDetails is used for fine grained control over record publishing
26 func (s *Shell) PublishWithDetails(contentHash, key string, lifetime, ttl time.Duration, resolve bool) (*PublishResponse, error) {
27         var pubResp PublishResponse
28         req := s.Request("name/publish", contentHash).Option("resolve", resolve)
29         if key != "" {
30                 req.Option("key", key)
31         }
32         if lifetime != 0 {
33                 req.Option("lifetime", lifetime)
34         }
35         if ttl.Seconds() > 0 {
36                 req.Option("ttl", ttl)
37         }
38         err := req.Exec(context.Background(), &pubResp)
39         if err != nil {
40                 return nil, err
41         }
42         return &pubResp, nil
43 }
44
45 // Resolve gets resolves the string provided to an /ipns/[name]. If asked to
46 // resolve an empty string, resolve instead resolves the node's own /ipns value.
47 func (s *Shell) Resolve(id string) (string, error) {
48         req := s.Request("name/resolve")
49         if id != "" {
50                 req.Arguments(id)
51         }
52         var out struct{ Path string }
53         err := req.Exec(context.Background(), &out)
54         return out.Path, err
55 }