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

Commit

Permalink
some initial context wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Apr 16, 2017
1 parent 458b0e6 commit e577e10
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
5 changes: 3 additions & 2 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package shell

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -12,7 +13,7 @@ import (
)

func (s *Shell) DagGet(ref string, out interface{}) error {
req := s.newRequest("dag/get")
req := s.newRequest(context.Background(), "dag/get")
req.Args = []string{ref}

resp, err := req.Send(s.httpcli)
Expand All @@ -29,7 +30,7 @@ func (s *Shell) DagGet(ref string, out interface{}) error {
}

func (s *Shell) DagPut(data interface{}, ienc, kind string) (string, error) {
req := s.newRequest("dag/put")
req := s.newRequest(context.Background(), "dag/put")
req.Opts = map[string]string{
"input-enc": ienc,
"format": kind,
Expand Down
7 changes: 4 additions & 3 deletions ipns.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shell

import (
"context"
"encoding/json"
)

Expand All @@ -11,7 +12,7 @@ func (s *Shell) Publish(node string, value string) error {
args = []string{node, value}
}

resp, err := s.newRequest("name/publish", args...).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "name/publish", args...).Send(s.httpcli)
if err != nil {
return err
}
Expand All @@ -30,9 +31,9 @@ func (s *Shell) Resolve(id string) (string, error) {
var resp *Response
var err error
if id != "" {
resp, err = s.newRequest("name/resolve", id).Send(s.httpcli)
resp, err = s.newRequest(context.Background(), "name/resolve", id).Send(s.httpcli)
} else {
resp, err = s.newRequest("name/resolve").Send(s.httpcli)
resp, err = s.newRequest(context.Background(), "name/resolve").Send(s.httpcli)
}
if err != nil {
return "", err
Expand Down
3 changes: 2 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shell

import (
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -22,7 +23,7 @@ type Request struct {
Headers map[string]string
}

func NewRequest(url, command string, args ...string) *Request {
func NewRequest(ctx context.Context, url, command string, args ...string) *Request {
if !strings.HasPrefix(url, "http") {
url = "http://" + url
}
Expand Down
59 changes: 30 additions & 29 deletions shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shell

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -52,8 +53,8 @@ func (s *Shell) SetTimeout(d time.Duration) {
s.httpcli.Timeout = d
}

func (s *Shell) newRequest(command string, args ...string) *Request {
return NewRequest(s.url, command, args...)
func (s *Shell) newRequest(ctx context.Context, command string, args ...string) *Request {
return NewRequest(ctx, s.url, command, args...)
}

type IdOutput struct {
Expand All @@ -73,7 +74,7 @@ func (s *Shell) ID(peer ...string) (*IdOutput, error) {
return nil, fmt.Errorf("Too many peer arguments")
}

resp, err := NewRequest(s.url, "id", peer...).Send(s.httpcli)
resp, err := NewRequest(context.Background(), s.url, "id", peer...).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand All @@ -95,7 +96,7 @@ func (s *Shell) ID(peer ...string) (*IdOutput, error) {

// Cat the content at the given path. Callers need to drain and close the returned reader after usage.
func (s *Shell) Cat(path string) (io.ReadCloser, error) {
resp, err := NewRequest(s.url, "cat", path).Send(s.httpcli)
resp, err := NewRequest(context.Background(), s.url, "cat", path).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,7 +134,7 @@ func (s *Shell) addWithOpts(r io.Reader, pin bool) (string, error) {
slf := files.NewSliceFile("", "", []files.File{fr})
fileReader := files.NewMultiFileReader(slf, true)

req := NewRequest(s.url, "add")
req := NewRequest(context.Background(), s.url, "add")
req.Body = fileReader
req.Opts["progress"] = "false"
if !pin {
Expand Down Expand Up @@ -163,7 +164,7 @@ func (s *Shell) AddLink(target string) (string, error) {
slf := files.NewSliceFile("", "", []files.File{link})
reader := files.NewMultiFileReader(slf, true)

req := s.newRequest("add")
req := s.newRequest(context.Background(), "add")
req.Body = reader

resp, err := req.Send(s.httpcli)
Expand Down Expand Up @@ -198,7 +199,7 @@ func (s *Shell) AddDir(dir string) (string, error) {
slf := files.NewSliceFile("", dir, []files.File{sf})
reader := files.NewMultiFileReader(slf, true)

req := NewRequest(s.url, "add")
req := NewRequest(context.Background(), s.url, "add")
req.Opts["r"] = "true"
req.Body = reader

Expand Down Expand Up @@ -242,7 +243,7 @@ const (

// List entries at the given path
func (s *Shell) List(path string) ([]*LsLink, error) {
resp, err := NewRequest(s.url, "ls", path).Send(s.httpcli)
resp, err := NewRequest(context.Background(), s.url, "ls", path).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -275,7 +276,7 @@ type LsObject struct {

// Pin the given path
func (s *Shell) Pin(path string) error {
req := NewRequest(s.url, "pin/add", path)
req := NewRequest(context.Background(), s.url, "pin/add", path)
req.Opts["r"] = "true"

resp, err := req.Send(s.httpcli)
Expand All @@ -292,7 +293,7 @@ func (s *Shell) Pin(path string) error {

// Unpin the given path
func (s *Shell) Unpin(path string) error {
req := NewRequest(s.url, "pin/rm", path)
req := NewRequest(context.Background(), s.url, "pin/rm", path)
req.Opts["r"] = "true"

resp, err := req.Send(s.httpcli)
Expand Down Expand Up @@ -323,7 +324,7 @@ type PinInfo struct {
// than unordered array searching. The map is likely to be more useful to a
// client than a flat list.
func (s *Shell) Pins() (map[string]PinInfo, error) {
resp, err := s.newRequest("pin/ls").Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "pin/ls").Send(s.httpcli)
if err != nil {
return nil, err
}
Expand All @@ -348,7 +349,7 @@ type PeerInfo struct {
}

func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
resp, err := s.newRequest("dht/findpeer", peer).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "dht/findpeer", peer).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand All @@ -372,7 +373,7 @@ func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
}

func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {
req := s.newRequest("refs", hash)
req := s.newRequest(context.Background(), "refs", hash)
if recursive {
req.Opts["r"] = "true"
}
Expand Down Expand Up @@ -410,7 +411,7 @@ func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {

func (s *Shell) Patch(root, action string, args ...string) (string, error) {
cmdargs := append([]string{root}, args...)
resp, err := s.newRequest("object/patch/"+action, cmdargs...).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "object/patch/"+action, cmdargs...).Send(s.httpcli)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -452,7 +453,7 @@ func (s *Shell) PatchData(root string, set bool, data interface{}) (string, erro
slf := files.NewSliceFile("", "", []files.File{fr})
fileReader := files.NewMultiFileReader(slf, true)

req := s.newRequest("object/patch/"+cmd, root)
req := s.newRequest(context.Background(), "object/patch/"+cmd, root)
req.Body = fileReader

resp, err := req.Send(s.httpcli)
Expand All @@ -478,7 +479,7 @@ func (s *Shell) PatchData(root string, set bool, data interface{}) (string, erro
func (s *Shell) PatchLink(root, path, childhash string, create bool) (string, error) {
cmdargs := []string{root, path, childhash}

req := s.newRequest("object/patch/add-link", cmdargs...)
req := s.newRequest(context.Background(), "object/patch/add-link", cmdargs...)
if create {
req.Opts["create"] = "true"
}
Expand All @@ -503,7 +504,7 @@ func (s *Shell) PatchLink(root, path, childhash string, create bool) (string, er
}

func (s *Shell) Get(hash, outdir string) error {
resp, err := s.newRequest("get", hash).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "get", hash).Send(s.httpcli)
if err != nil {
return err
}
Expand All @@ -523,7 +524,7 @@ func (s *Shell) NewObject(template string) (string, error) {
args = []string{template}
}

resp, err := s.newRequest("object/new", args...).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "object/new", args...).Send(s.httpcli)
if err != nil {
return "", err
}
Expand All @@ -543,7 +544,7 @@ func (s *Shell) NewObject(template string) (string, error) {
}

func (s *Shell) ResolvePath(path string) (string, error) {
resp, err := s.newRequest("object/stat", path).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "object/stat", path).Send(s.httpcli)
if err != nil {
return "", err
}
Expand All @@ -564,7 +565,7 @@ func (s *Shell) ResolvePath(path string) (string, error) {

// returns ipfs version and commit sha
func (s *Shell) Version() (string, string, error) {
resp, err := s.newRequest("version").Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "version").Send(s.httpcli)
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -593,7 +594,7 @@ func (s *Shell) IsUp() bool {
}

func (s *Shell) BlockStat(path string) (string, int, error) {
resp, err := s.newRequest("block/stat", path).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "block/stat", path).Send(s.httpcli)
if err != nil {
return "", 0, err
}
Expand All @@ -617,7 +618,7 @@ func (s *Shell) BlockStat(path string) (string, int, error) {
}

func (s *Shell) BlockGet(path string) ([]byte, error) {
resp, err := s.newRequest("block/get", path).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "block/get", path).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand All @@ -637,7 +638,7 @@ func (s *Shell) BlockPut(block []byte) (string, error) {
slf := files.NewSliceFile("", "", []files.File{fr})
fileReader := files.NewMultiFileReader(slf, true)

req := s.newRequest("block/put")
req := s.newRequest(context.Background(), "block/put")
req.Body = fileReader
resp, err := req.Send(s.httpcli)
if err != nil {
Expand Down Expand Up @@ -671,7 +672,7 @@ type ObjectLink struct {
}

func (s *Shell) ObjectGet(path string) (*IpfsObject, error) {
resp, err := s.newRequest("object/get", path).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "object/get", path).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -703,7 +704,7 @@ func (s *Shell) ObjectPut(obj *IpfsObject) (string, error) {
slf := files.NewSliceFile("", "", []files.File{fr})
fileReader := files.NewMultiFileReader(slf, true)

req := s.newRequest("object/put")
req := s.newRequest(context.Background(), "object/put")
req.Body = fileReader
resp, err := req.Send(s.httpcli)
if err != nil {
Expand All @@ -726,7 +727,7 @@ func (s *Shell) ObjectPut(obj *IpfsObject) (string, error) {

func (s *Shell) PubSubSubscribe(topic string) (*PubSubSubscription, error) {
// connect
req := s.newRequest("pubsub/sub", topic)
req := s.newRequest(context.Background(), "pubsub/sub", topic)

resp, err := req.Send(s.httpcli)
if err != nil {
Expand All @@ -737,7 +738,7 @@ func (s *Shell) PubSubSubscribe(topic string) (*PubSubSubscription, error) {
}

func (s *Shell) PubSubPublish(topic, data string) error {
_, err := s.newRequest("pubsub/pub", topic, data).Send(s.httpcli)
_, err := s.newRequest(context.Background(), "pubsub/pub", topic, data).Send(s.httpcli)
if err != nil {
return err
}
Expand All @@ -757,7 +758,7 @@ type ObjectStats struct {
// ObjectStat gets stats for the DAG object named by key. It returns
// the stats of the requested Object or an error.
func (s *Shell) ObjectStat(key string) (*ObjectStats, error) {
resp, err := s.newRequest("object/stat", key).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "object/stat", key).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand All @@ -780,7 +781,7 @@ func (s *Shell) ObjectStat(key string) (*ObjectStats, error) {
func (s *Shell) DiagNet(format string) ([]byte, error) {
var result = new(bytes.Buffer)

req := s.newRequest("diag/net")
req := s.newRequest(context.Background(), "diag/net")
req.Opts["vis"] = format

resp, err := req.Send(s.httpcli)
Expand Down
3 changes: 2 additions & 1 deletion unixfs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shell

import (
"context"
"encoding/json"
"fmt"
)
Expand All @@ -25,7 +26,7 @@ type lsOutput struct {

// FileList entries at the given path using the UnixFS commands
func (s *Shell) FileList(path string) (*UnixLsObject, error) {
resp, err := s.newRequest("file/ls", path).Send(s.httpcli)
resp, err := s.newRequest(context.Background(), "file/ls", path).Send(s.httpcli)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e577e10

Please sign in to comment.