Skip to content

Commit d6b83d2

Browse files
author
postables
authored
Merge branch 'master' into dag/put#option
2 parents 95c8f6b + 8b7099f commit d6b83d2

File tree

350 files changed

+55917
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

350 files changed

+55917
-174
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "testenv"]
2+
path = testenv
3+
url = https://github.com/RTradeLtd/testenv.git

.travis.yml

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
1-
os:
2-
- linux
3-
41
language: go
5-
2+
sudo: required
63
go:
7-
- 1.11.x
8-
4+
- "1.12"
95
services:
10-
- docker
11-
12-
env:
13-
global:
14-
- GOTFLAGS="-race"
15-
- IPFS_PATH=/tmp/ipfs
16-
matrix:
17-
- BUILD_DEPTYPE=gx
18-
- BUILD_DEPTYPE=gomod
6+
- docker
7+
env: GO111MODULE=on
198

20-
before_install:
21-
- docker pull ipfs/go-ipfs:master
22-
- mkdir /tmp/ipfs && chmod 0777 /tmp/ipfs
23-
- docker run -d -v /tmp/ipfs:/data/ipfs -p 8080:8080 -p 4001:4001 -p 5001:5001 ipfs/go-ipfs:master --enable-pubsub-experiment
24-
25-
26-
# disable travis install
27-
install:
28-
- true
9+
before_script:
10+
- go vet ./...
11+
- go build ./...
12+
- go test -run xxxx ./...
13+
- make testenv
2914

3015
script:
31-
- go get -d -t github.com/cheekybits/is/... # remove with gx
32-
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
33-
16+
- go test -race -failfast -coverprofile=coverage.txt ./...
3417

35-
cache:
36-
directories:
37-
- $GOPATH/src/gx
38-
- $GOPATH/pkg/mod
39-
- $HOME/.cache/go-build
18+
after_success:
19+
- bash <(curl -s https://codecov.io/bash)
4020

4121
notifications:
4222
email: false

Makefile

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
all: deps
2-
gx:
3-
go get github.com/whyrusleeping/gx
4-
go get github.com/whyrusleeping/gx-go
5-
deps: gx
6-
gx --verbose install --global
7-
gx-go rewrite
8-
.PHONY: all gx deps
1+
GO=env GO111MODULE=on go
2+
GONOMOD=env GO111MODULE=off go
3+
4+
.PHONY: deps
5+
deps:
6+
$(GO) mod vendor
7+
$(GO) mod verify
8+
9+
# Set up test environment
10+
.PHONY: testenv
11+
WAIT=3
12+
testenv:
13+
@echo "=================== preparing test env ==================="
14+
( cd testenv ; make testenv )
15+
@echo "=================== done ==================="
16+
17+
.PHONY: clean
18+
clean:
19+
@echo "=================== stopping test env ==================="
20+
( cd testenv ; make clean )
21+
@echo "=================== done ==================="

dag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"io"
88
"strings"
99

10-
"github.com/ipfs/go-ipfs-api/options"
11-
"github.com/ipfs/go-ipfs-files"
10+
"github.com/RTradeLtd/go-ipfs-api/options"
11+
files "github.com/ipfs/go-ipfs-files"
1212
)
1313

1414
func (s *Shell) DagGet(ref string, out interface{}) error {

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
module github.com/ipfs/go-ipfs-api
1+
module github.com/RTradeLtd/go-ipfs-api
22

33
require (
44
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927
55
github.com/ipfs/go-ipfs-files v0.0.1
6-
github.com/ipfs/go-ipfs-util v0.0.1
76
github.com/libp2p/go-libp2p-metrics v0.0.1
87
github.com/libp2p/go-libp2p-peer v0.0.1
98
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmv
2323
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
2424
github.com/ipfs/go-ipfs-files v0.0.1 h1:OroTsI58plHGX70HPLKy6LQhPR3HZJ5ip61fYlo6POM=
2525
github.com/ipfs/go-ipfs-files v0.0.1/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
26-
github.com/ipfs/go-ipfs-util v0.0.1 h1:Wz9bL2wB2YBJqggkA4dD7oSmqB4cAnpNbGrlHJulv50=
27-
github.com/ipfs/go-ipfs-util v0.0.1/go.mod h1:spsl5z8KUnrve+73pOhSVZND1SIxPW5RyBCNzQxlJBc=
2826
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
2927
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
3028
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=

pubsub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package shell
33
import (
44
"encoding/json"
55

6-
"github.com/libp2p/go-libp2p-peer"
6+
peer "github.com/libp2p/go-libp2p-peer"
77
)
88

99
// Message is a pubsub message.

request.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func NewRequest(ctx context.Context, url, command string, args ...string) *Reque
2929
if !strings.HasPrefix(url, "http") {
3030
url = "http://" + url
3131
}
32-
3332
opts := map[string]string{
3433
"encoding": "json",
3534
"stream-channels": "true",
@@ -114,14 +113,13 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
114113
if err != nil {
115114
return nil, err
116115
}
117-
116+
118117
req = req.WithContext(r.Ctx)
119118

120119
// Add any headers that were supplied via the RequestBuilder.
121120
for k, v := range r.Headers {
122121
req.Header.Add(k, v)
123122
}
124-
125123
if fr, ok := r.Body.(*files.MultiFileReader); ok {
126124
req.Header.Set("Content-Type", "multipart/form-data; boundary="+fr.Boundary())
127125
req.Header.Set("Content-Disposition", "form-data; name=\"files\"")
@@ -149,18 +147,18 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
149147
case contentType == "text/plain":
150148
out, err := ioutil.ReadAll(resp.Body)
151149
if err != nil {
152-
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response (%d) read error: %s\n", resp.StatusCode, err)
150+
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response read error: %s\n", err)
153151
}
154152
e.Message = string(out)
155153
case contentType == "application/json":
156154
if err = json.NewDecoder(resp.Body).Decode(e); err != nil {
157-
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response (%d) unmarshall error: %s\n", resp.StatusCode, err)
155+
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response unmarshall error: %s\n", err)
158156
}
159157
default:
160-
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! unhandled response (%d) encoding: %s", resp.StatusCode, contentType)
158+
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! unhandled response encoding: %s", contentType)
161159
out, err := ioutil.ReadAll(resp.Body)
162160
if err != nil {
163-
fmt.Fprintf(os.Stderr, "ipfs-shell: response (%d) read error: %s\n", resp.StatusCode, err)
161+
fmt.Fprintf(os.Stderr, "ipfs-shell: response read error: %s\n", err)
164162
}
165163
e.Message = fmt.Sprintf("unknown ipfs-shell error encoding: %q - %q", contentType, out)
166164
}
@@ -187,3 +185,58 @@ func (r *Request) getURL() string {
187185

188186
return fmt.Sprintf("%s/%s?%s", r.ApiBase, r.Command, values.Encode())
189187
}
188+
189+
func (r *Request) SendGET(c *http.Client) (*Response, error) {
190+
url := r.getURLNoEncode()
191+
req, err := http.NewRequest("GET", url, r.Body)
192+
if err != nil {
193+
return nil, err
194+
}
195+
resp, err := c.Do(req)
196+
if err != nil {
197+
return nil, err
198+
}
199+
contentType := resp.Header.Get("Content-Type")
200+
parts := strings.Split(contentType, ";")
201+
contentType = parts[0]
202+
nresp := new(Response)
203+
nresp.Output = resp.Body
204+
if resp.StatusCode >= http.StatusBadRequest {
205+
e := &Error{
206+
Command: r.Command,
207+
}
208+
switch {
209+
case resp.StatusCode == http.StatusNotFound:
210+
e.Message = "command not found"
211+
case contentType == "text/plain":
212+
out, err := ioutil.ReadAll(resp.Body)
213+
if err != nil {
214+
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response read error: %s\n", err)
215+
}
216+
e.Message = string(out)
217+
case contentType == "application/json":
218+
if err = json.NewDecoder(resp.Body).Decode(e); err != nil {
219+
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! response unmarshall error: %s\n", err)
220+
}
221+
default:
222+
fmt.Fprintf(os.Stderr, "ipfs-shell: warning! unhandled response encoding: %s", contentType)
223+
out, err := ioutil.ReadAll(resp.Body)
224+
if err != nil {
225+
fmt.Fprintf(os.Stderr, "ipfs-shell: response read error: %s\n", err)
226+
}
227+
e.Message = fmt.Sprintf("unknown ipfs-shell error encoding: %q - %q", contentType, out)
228+
}
229+
nresp.Error = e
230+
nresp.Output = nil
231+
232+
// drain body and close
233+
ioutil.ReadAll(resp.Body)
234+
resp.Body.Close()
235+
}
236+
237+
return nresp, nil
238+
}
239+
240+
func (r *Request) getURLNoEncode() string {
241+
return fmt.Sprintf("%s/%s/%s", r.ApiBase, r.Command, r.Args[0])
242+
}

shell.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ func NewShell(url string) *Shell {
7272
return NewShellWithClient(url, c)
7373
}
7474

75+
// NewDirectShell creates a new shell that directly uses the provided URL,
76+
// instead of attempting to parse it into a multiaddr.
77+
//
78+
// For Nexus-hosted IPFS nodes, for example, use:
79+
//
80+
// shell := NewDirectShell(fmt.Sprintf("nexus.temporal.cloud/network/%s", networkName))
81+
//
82+
func NewDirectShell(url string) *Shell {
83+
return &Shell{
84+
url: url,
85+
httpcli: gohttp.Client{
86+
Transport: &gohttp.Transport{
87+
Proxy: gohttp.ProxyFromEnvironment,
88+
DisableKeepAlives: true,
89+
},
90+
},
91+
}
92+
}
93+
7594
func NewShellWithClient(url string, c *gohttp.Client) *Shell {
7695
if a, err := ma.NewMultiaddr(url); err == nil {
7796
_, host, err := manet.DialArgs(a)
@@ -89,6 +108,22 @@ func NewShellWithClient(url string, c *gohttp.Client) *Shell {
89108
return &sh
90109
}
91110

111+
// WithAuthorization returns a Shell that sets the provided token to be used as
112+
// an Authorization header in API requests. For example:
113+
//
114+
// resp, err := NewDirectShell(addr).
115+
// WithAuthorization(token).
116+
// Cat(hash)
117+
//
118+
func (s *Shell) WithAuthorization(token string) *Shell {
119+
return &Shell{
120+
url: s.url,
121+
httpcli: gohttp.Client{
122+
Transport: newAuthenticatedTransport(s.httpcli.Transport, token),
123+
},
124+
}
125+
}
126+
92127
func (s *Shell) SetTimeout(d time.Duration) {
93128
s.httpcli.Timeout = d
94129
}
@@ -178,6 +213,15 @@ func (s *Shell) Pin(path string) error {
178213
Exec(context.Background(), nil)
179214
}
180215

216+
// PinUpdate is used to update one pin path to another followed by unpinning
217+
func (s *Shell) PinUpdate(fromPath, toPath string) (map[string][]string, error) {
218+
var out map[string][]string
219+
if err := s.Request("pin/update", fromPath, toPath).Exec(context.Background(), &out); err != nil {
220+
return nil, err
221+
}
222+
return out, nil
223+
}
224+
181225
// Unpin the given path
182226
func (s *Shell) Unpin(path string) error {
183227
return s.Request("pin/rm", path).
@@ -222,9 +266,10 @@ func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
222266
return &peers.Responses[0], nil
223267
}
224268

225-
func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {
269+
func (s *Shell) Refs(hash string, recursive, unique bool) (<-chan string, error) {
226270
resp, err := s.Request("refs", hash).
227271
Option("recursive", recursive).
272+
Option("unique", unique).
228273
Send(context.Background())
229274
if err != nil {
230275
return nil, err

shell_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/RTradeLtd/go-ipfs-api/options"
1516
"github.com/cheekybits/is"
16-
"github.com/ipfs/go-ipfs-api/options"
1717
)
1818

1919
const (
2020
examplesHash = "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv"
21-
shellUrl = "localhost:5001"
21+
shellUrl = "192.168.1.101:5001"
2222
)
2323

2424
func TestAdd(t *testing.T) {
@@ -112,16 +112,6 @@ func TestAddDir(t *testing.T) {
112112
is.Equal(cid, "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
113113
}
114114

115-
func TestLocalShell(t *testing.T) {
116-
is := is.New(t)
117-
s := NewLocalShell()
118-
is.NotNil(s)
119-
120-
mhash, err := s.Add(bytes.NewBufferString("Hello IPFS Shell tests"))
121-
is.Nil(err)
122-
is.Equal(mhash, "QmUfZ9rAdhV5ioBzXKdUTh2ZNsz9bzbkaLVyQ8uc8pj21F")
123-
}
124-
125115
func TestCat(t *testing.T) {
126116
is := is.New(t)
127117
s := NewShell(shellUrl)
@@ -387,7 +377,7 @@ func TestRefs(t *testing.T) {
387377
cid, err := s.AddDir("./testdata")
388378
is.Nil(err)
389379
is.Equal(cid, "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
390-
refs, err := s.Refs(cid, false)
380+
refs, err := s.Refs(cid, false, false)
391381
is.Nil(err)
392382
expected := []string{
393383
"QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V",

0 commit comments

Comments
 (0)