11package shell
22
33import (
4- "bytes"
54 "context"
6- "encoding/json"
7- "strconv"
85 "time"
96)
107
@@ -15,79 +12,44 @@ type PublishResponse struct {
1512
1613// Publish updates a mutable name to point to a given value
1714func (s * Shell ) Publish (node string , value string ) error {
18- args := []string {value }
15+ var pubResp PublishResponse
16+ req := s .Request ("name/publish" )
1917 if node != "" {
20- args = [] string { node , value }
18+ req . Arguments ( node )
2119 }
20+ req .Arguments (value )
2221
23- resp , err := s .newRequest (context .Background (), "name/publish" , args ... ).Send (s .httpcli )
24- if err != nil {
25- return err
26- }
27- defer resp .Close ()
28-
29- if resp .Error != nil {
30- return resp .Error
31- }
32-
33- return nil
22+ return req .Exec (context .Background (), & pubResp )
3423}
3524
3625// PublishWithDetails is used for fine grained control over record publishing
3726func (s * Shell ) PublishWithDetails (contentHash , key string , lifetime , ttl time.Duration , resolve bool ) (* PublishResponse , error ) {
38-
39- args := []string {contentHash }
40- req := s .newRequest (context .Background (), "name/publish" , args ... )
41- if key == "" {
42- key = "self"
27+ var pubResp PublishResponse
28+ req := s .Request ("name/publish" , contentHash ).Option ("resolve" , resolve )
29+ if key != "" {
30+ req .Option ("key" , key )
4331 }
44- req .Opts ["key" ] = key
45- if lifetime .Seconds () > 0 {
46- req .Opts ["lifetime" ] = lifetime .String ()
32+ if lifetime != 0 {
33+ req .Option ("lifetime" , lifetime )
4734 }
4835 if ttl .Seconds () > 0 {
49- req .Opts [ "ttl" ] = ttl . String ( )
36+ req .Option ( "ttl" , ttl )
5037 }
51- req .Opts ["resolve" ] = strconv .FormatBool (resolve )
52- resp , err := req .Send (s .httpcli )
38+ err := req .Exec (context .Background (), & pubResp )
5339 if err != nil {
5440 return nil , err
5541 }
56- defer resp .Close ()
57- if resp .Error != nil {
58- return nil , resp .Error
59- }
60- buf := new (bytes.Buffer )
61- buf .ReadFrom (resp .Output )
62- var pubResp PublishResponse
63- json .Unmarshal (buf .Bytes (), & pubResp )
6442 return & pubResp , nil
6543}
6644
6745// Resolve gets resolves the string provided to an /ipns/[name]. If asked to
6846// resolve an empty string, resolve instead resolves the node's own /ipns value.
6947func (s * Shell ) Resolve (id string ) (string , error ) {
70- var resp * Response
71- var err error
48+ req := s .Request ("name/resolve" )
7249 if id != "" {
73- resp , err = s .newRequest (context .Background (), "name/resolve" , id ).Send (s .httpcli )
74- } else {
75- resp , err = s .newRequest (context .Background (), "name/resolve" ).Send (s .httpcli )
76- }
77- if err != nil {
78- return "" , err
79- }
80- defer resp .Close ()
81-
82- if resp .Error != nil {
83- return "" , resp .Error
50+ req .Arguments (id )
8451 }
85-
8652 var out struct { Path string }
87- err = json .NewDecoder (resp .Output ).Decode (& out )
88- if err != nil {
89- return "" , err
90- }
91-
92- return out .Path , nil
53+ err := req .Exec (context .Background (), & out )
54+ return out .Path , err
9355}
0 commit comments