Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Add additional publish function for more fine grained control of IPNS record publishing #91

Merged
merged 7 commits into from
Jul 6, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions ipns.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package shell

import (
"bytes"
"context"
"encoding/json"
"errors"
)

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}
Expand All @@ -25,6 +32,43 @@ 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 string, lifetime string, ttl string, key string, resolve bool) (*PublishResponse, error) {
var pubResp PublishResponse
Copy link
Member

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 line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! :D

var resolveString string
if contentHash == "" {
return nil, errors.New("empty contentHash provided")
}
if lifetime == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pass this as time.Duration

return nil, errors.New("empty lifetime provided")
}
if ttl == "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with time.Duration

return nil, errors.New("empty ttl provided")
}
if key == "" {
return nil, errors.New("empty key provided")
}
if resolve {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use strconv.FormatBool

resolveString = "true"
} else {
resolveString = "false"
}
args := []string{contentHash, resolveString, lifetime, ttl, key}
resp, err := s.newRequest(context.TODO(), "name/publish", args...).Send(s.httpcli)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be context.Background() as in other places

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) {
Expand Down