-
Notifications
You must be signed in to change notification settings - Fork 178
Add additional publish function for more fine grained control of IPNS record publishing #91
Changes from 4 commits
a9ebe95
1c2767e
d29f92e
18615a8
980c8a5
a67bcdb
739f959
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
package shell | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type PublishResponse struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} | ||
|
||
// Publish updates a mutable name to point to a given value | ||
func (s *Shell) Publish(node string, value string) error { | ||
args := []string{value} | ||
|
@@ -25,6 +33,32 @@ func (s *Shell) Publish(node string, value string) error { | |
return nil | ||
} | ||
|
||
// PublishWithDetails is used for fine grained control over record publishing | ||
func (s *Shell) PublishWithDetails(contentHash, key string, lifetime, ttl time.Duration, resolve bool) (*PublishResponse, error) { | ||
var pubResp PublishResponse | ||
args := []string{contentHash} | ||
req := s.newRequest(context.Background(), "name/publish", args...) | ||
if key == "" { | ||
key = "self" | ||
} | ||
req.Opts["key"] = key | ||
req.Opts["lifetime"] = lifetime.String() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd wrap this and go-ipfs cares whether or not a option is present - https://github.com/ipfs/go-ipfs/blob/master/core/commands/publish.go#L117 |
||
req.Opts["ttl"] = ttl.String() | ||
req.Opts["resolve"] = strconv.FormatBool(resolve) | ||
resp, err := req.Send(s.httpcli) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Close() | ||
if resp.Error != nil { | ||
return nil, resp.Error | ||
} | ||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(resp.Output) | ||
json.Unmarshal(buf.Bytes(), &pubResp) | ||
return &pubResp, nil | ||
} | ||
|
||
// Resolve gets resolves the string provided to an /ipfs/[hash]. If asked to | ||
// resolve an empty string, resolve instead resolves the node's own /ipns value. | ||
func (s *Shell) Resolve(id string) (string, error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package shell_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
ipfsapi "github.com/RTradeLtd/go-ipfs-api" | ||
) | ||
|
||
var examplesHash = "/ipfs/Qmbu7x6gJbsKDcseQv66pSbUcAA3Au6f7MfTYVXwvBxN2K" | ||
|
||
func TestPublishDetailsWithKey(t *testing.T) { | ||
shell := ipfsapi.NewShell("localhost:5001") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. other tests do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Note that you'll need to change the package to just |
||
|
||
resp, err := shell.PublishWithDetails(examplesHash, "key1", time.Hour, time.Hour, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This expects |
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if resp.Value != examplesHash { | ||
t.Fatalf(fmt.Sprintf("Expected to receive %s but got %s", examplesHash, resp.Value)) | ||
} | ||
} | ||
|
||
func TestPublishDetailsWithoutKey(t *testing.T) { | ||
shell := ipfsapi.NewShell("localhost:5001") | ||
|
||
resp, err := shell.PublishWithDetails(examplesHash, "", time.Hour, time.Hour, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will publish over whatever is published on the machine it's invoked on, which is.. suboptimal |
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if resp.Value != examplesHash { | ||
t.Fatalf(fmt.Sprintf("Expected to receive %s but got %s", examplesHash, resp.Value)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put it above/closer to
json.Unmarshal
lineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! :D