strings.Split -> path.SplitList
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Nov 24, 2015 at 13:59 UTC
743f3edcbb261d5cd889d037d79fdf0a8b4e7dad
14 files changed
+48
-44
commands/http/parse.go
+11
-9
@@ -9,6 +9,7 @@ import (
9
10
cmds "github.com/ipfs/go-ipfs/commands"
11
files "github.com/ipfs/go-ipfs/commands/files"
12
+ path "github.com/ipfs/go-ipfs/path"
13
)
14
15
// Parse parses the data in a http.Request and returns a command Request object
@@ -16,32 +17,33 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
17
if !strings.HasPrefix(r.URL.Path, ApiPath) {
18
return nil, errors.New("Unexpected path prefix")
19
}
19
- path := strings.Split(strings.TrimPrefix(r.URL.Path, ApiPath+"/"), "/")
20
+ pth := path.SplitList(strings.TrimPrefix(r.URL.Path, ApiPath+"/"))
21
22
stringArgs := make([]string, 0)
23
24
if err := apiVersionMatches(r); err != nil {
24
- if path[0] != "version" { // compatibility with previous version check
25
+ if pth[0] != "version" { // compatibility with previous version check
26
return nil, err
27
}
28
}
29
29
- cmd, err := root.Get(path[:len(path)-1])
30
+ cmd, err := root.Get(pth[:len(pth)-1])
31
if err != nil {
32
// 404 if there is no command at that path
33
return nil, ErrNotFound
34
35
}
36
36
- if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
37
- if len(path) <= 1 {
37
+ if sub := cmd.Subcommand(pth[len(pth)-1]); sub == nil {
38
+ if len(pth) <= 1 {
39
return nil, ErrNotFound
40
}
41
42
// if the last string in the path isn't a subcommand, use it as an argument
43
// e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
43
- stringArgs = append(stringArgs, path[len(path)-1])
44
- path = path[:len(path)-1]
44
+ stringArgs = append(stringArgs, pth[len(pth)-1])
45
+ pth = pth[:len(pth)-1]
46
+
47
} else {
48
cmd = sub
49
}
@@ -93,7 +95,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
95
}
96
}
97
96
- optDefs, err := root.GetOptions(path)
98
+ optDefs, err := root.GetOptions(pth)
99
if err != nil {
100
return nil, err
101
}
@@ -116,7 +118,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
118
return nil, fmt.Errorf("File argument '%s' is required", requiredFile)
119
}
120
119
- req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs)
121
+ req, err := cmds.NewRequest(pth, opts, args, f, cmd, optDefs)
122
if err != nil {
123
return nil, err
124
}
core/commands/dht.go
+1
-2
@@ -5,7 +5,6 @@ import (
5
"errors"
6
"fmt"
7
"io"
8
- "strings"
8
"time"
9
10
key "github.com/ipfs/go-ipfs/blocks/key"
@@ -600,7 +599,7 @@ PutValue will store the given key value pair in the dht.
599
}
600
601
func escapeDhtKey(s string) (key.Key, error) {
603
- parts := strings.Split(s, "/")
602
+ parts := path.SplitList(s)
603
switch len(parts) {
604
case 1:
605
return key.B58KeyDecode(s), nil
core/commands/files/files.go
+1
-2
@@ -245,8 +245,7 @@ Examples:
245
res.SetOutput(&FilesLsOutput{listing})
246
return
247
case *mfs.File:
248
- parts := strings.Split(path, "/")
249
- name := parts[len(parts)-1]
248
+ _, name := gopath.Split(path)
249
out := &FilesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}}
250
res.SetOutput(out)
251
return
core/corehttp/gateway_handler.go
+1
-1
@@ -246,7 +246,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
246
var backLink string = prefix + urlPath
247
248
// don't go further up than /ipfs/$hash/
249
- pathSplit := strings.Split(backLink, "/")
249
+ pathSplit := path.SplitList(backLink)
250
switch {
251
// keep backlink
252
case len(pathSplit) == 3: // url: /ipfs/$hash
merkledag/utils/utils.go
+5
-5
@@ -2,7 +2,6 @@ package dagutils
2
3
import (
4
"errors"
5
- "strings"
5
6
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
7
syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
@@ -12,6 +11,7 @@ import (
11
bserv "github.com/ipfs/go-ipfs/blockservice"
12
offline "github.com/ipfs/go-ipfs/exchange/offline"
13
dag "github.com/ipfs/go-ipfs/merkledag"
14
+ path "github.com/ipfs/go-ipfs/path"
15
)
16
17
type Editor struct {
@@ -76,8 +76,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
76
return root, nil
77
}
78
79
-func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *dag.Node, create func() *dag.Node) error {
80
- splpath := strings.Split(path, "/")
79
+func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert *dag.Node, create func() *dag.Node) error {
80
+ splpath := path.SplitList(pth)
81
nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
82
if err != nil {
83
return err
@@ -130,8 +130,8 @@ func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.Node, path []st
130
return root, nil
131
}
132
133
-func (e *Editor) RmLink(ctx context.Context, path string) error {
134
- splpath := strings.Split(path, "/")
133
+func (e *Editor) RmLink(ctx context.Context, pth string) error {
134
+ splpath := path.SplitList(pth)
135
nd, err := e.rmLink(ctx, e.root, splpath)
136
if err != nil {
137
return err
merkledag/utils/utils_test.go
+3
-3
@@ -1,12 +1,12 @@
1
package dagutils
2
3
import (
4
- "strings"
4
"testing"
5
6
key "github.com/ipfs/go-ipfs/blocks/key"
7
dag "github.com/ipfs/go-ipfs/merkledag"
8
mdtest "github.com/ipfs/go-ipfs/merkledag/test"
9
+ path "github.com/ipfs/go-ipfs/path"
10
11
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12
)
@@ -43,8 +43,8 @@ func TestAddLink(t *testing.T) {
43
}
44
}
45
46
-func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path string, exp key.Key) {
47
- parts := strings.Split(path, "/")
46
+func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, pth string, exp key.Key) {
47
+ parts := path.SplitList(pth)
48
cur := root
49
for _, e := range parts {
50
nxt, err := cur.GetLinkedNode(context.Background(), ds, e)
mfs/mfs_test.go
+10
-10
@@ -8,12 +8,12 @@ import (
8
"io/ioutil"
9
"os"
10
"sort"
11
- "strings"
11
"testing"
12
13
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
14
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
15
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
16
+ "github.com/ipfs/go-ipfs/path"
17
18
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
19
key "github.com/ipfs/go-ipfs/blocks/key"
@@ -43,8 +43,8 @@ func getRandFile(t *testing.T, ds dag.DAGService, size int64) *dag.Node {
43
return nd
44
}
45
46
-func mkdirP(t *testing.T, root *Directory, path string) *Directory {
47
- dirs := strings.Split(path, "/")
46
+func mkdirP(t *testing.T, root *Directory, pth string) *Directory {
47
+ dirs := path.SplitList(pth)
48
cur := root
49
for _, d := range dirs {
50
n, err := cur.Mkdir(d)
@@ -69,15 +69,15 @@ func mkdirP(t *testing.T, root *Directory, path string) *Directory {
69
return cur
70
}
71
72
-func assertDirAtPath(root *Directory, path string, children []string) error {
73
- fsn, err := DirLookup(root, path)
72
+func assertDirAtPath(root *Directory, pth string, children []string) error {
73
+ fsn, err := DirLookup(root, pth)
74
if err != nil {
75
return err
76
}
77
78
dir, ok := fsn.(*Directory)
79
if !ok {
80
- return fmt.Errorf("%s was not a directory", path)
80
+ return fmt.Errorf("%s was not a directory", pth)
81
}
82
83
listing, err := dir.List()
@@ -113,13 +113,13 @@ func compStrArrs(a, b []string) bool {
113
return true
114
}
115
116
-func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path string) error {
117
- parts := strings.Split(path, "/")
116
+func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, pth string) error {
117
+ parts := path.SplitList(pth)
118
cur := root
119
for i, d := range parts[:len(parts)-1] {
120
next, err := cur.Child(d)
121
if err != nil {
122
- return fmt.Errorf("looking for %s failed: %s", path, err)
122
+ return fmt.Errorf("looking for %s failed: %s", pth, err)
123
}
124
125
nextDir, ok := next.(*Directory)
@@ -138,7 +138,7 @@ func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path st
138
139
file, ok := finaln.(*File)
140
if !ok {
141
- return fmt.Errorf("%s was not a file!", path)
141
+ return fmt.Errorf("%s was not a file!", pth)
142
}
143
144
out, err := ioutil.ReadAll(file)
mfs/ops.go
+2
-2
@@ -101,7 +101,7 @@ func PutNode(r *Root, path string, nd *dag.Node) error {
101
// Mkdir creates a directory at 'path' under the directory 'd', creating
102
// intermediary directories as needed if 'parents' is set to true
103
func Mkdir(r *Root, pth string, parents bool) error {
104
- parts := strings.Split(pth, "/")
104
+ parts := path.SplitList(pth)
105
if parts[0] == "" {
106
parts = parts[1:]
107
}
@@ -159,7 +159,7 @@ func Lookup(r *Root, path string) (FSNode, error) {
159
// under the directory 'd'
160
func DirLookup(d *Directory, pth string) (FSNode, error) {
161
pth = strings.Trim(pth, "/")
162
- parts := strings.Split(pth, "/")
162
+ parts := path.SplitList(pth)
163
if len(parts) == 1 && parts[0] == "" {
164
return d, nil
165
}
path/path.go
+4
@@ -106,3 +106,7 @@ func (p *Path) IsValid() error {
106
func Join(pths []string) string {
107
return strings.Join(pths, "/")
108
}
109
+
110
+func SplitList(pth string) []string {
111
+ return strings.Split(pth, "/")
112
+}
routing/record/selection.go
+2
-2
@@ -2,9 +2,9 @@ package record
2
3
import (
4
"errors"
5
- "strings"
5
6
key "github.com/ipfs/go-ipfs/blocks/key"
7
+ path "github.com/ipfs/go-ipfs/path"
8
)
9
10
// A SelectorFunc selects the best value for the given key from
@@ -18,7 +18,7 @@ func (s Selector) BestRecord(k key.Key, recs [][]byte) (int, error) {
18
return 0, errors.New("no records given!")
19
}
20
21
- parts := strings.Split(string(k), "/")
21
+ parts := path.SplitList(string(k))
22
if len(parts) < 3 {
23
log.Infof("Record key does not have selectorfunc: %s", k)
24
return 0, errors.New("record key does not have selectorfunc")
routing/record/validation.go
+3
-3
@@ -3,10 +3,10 @@ package record
3
import (
4
"bytes"
5
"errors"
6
- "strings"
6
7
key "github.com/ipfs/go-ipfs/blocks/key"
8
ci "github.com/ipfs/go-ipfs/p2p/crypto"
9
+ path "github.com/ipfs/go-ipfs/path"
10
pb "github.com/ipfs/go-ipfs/routing/dht/pb"
11
u "github.com/ipfs/go-ipfs/util"
12
)
@@ -37,7 +37,7 @@ type ValidChecker struct {
37
// It runs needed validators
38
func (v Validator) VerifyRecord(r *pb.Record) error {
39
// Now, check validity func
40
- parts := strings.Split(r.GetKey(), "/")
40
+ parts := path.SplitList(r.GetKey())
41
if len(parts) < 3 {
42
log.Infof("Record key does not have validator: %s", key.Key(r.GetKey()))
43
return nil
@@ -54,7 +54,7 @@ func (v Validator) VerifyRecord(r *pb.Record) error {
54
55
func (v Validator) IsSigned(k key.Key) (bool, error) {
56
// Now, check validity func
57
- parts := strings.Split(string(k), "/")
57
+ parts := path.SplitList(string(k))
58
if len(parts) < 3 {
59
log.Infof("Record key does not have validator: %s", k)
60
return false, nil
tar/format.go
+1
-1
@@ -98,7 +98,7 @@ func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) {
98
// adds a '-' to the beginning of each path element so we can use 'data' as a
99
// special link in the structure without having to worry about
100
func escapePath(pth string) string {
101
- elems := strings.Split(strings.Trim(pth, "/"), "/")
101
+ elems := path.SplitList(strings.Trim(pth, "/"))
102
for i, e := range elems {
103
elems[i] = "-" + e
104
}
util/ipfsaddr/ipfsaddr.go
+2
-2
@@ -2,11 +2,11 @@ package ipfsaddr
2
3
import (
4
"errors"
5
- "strings"
5
6
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
7
8
peer "github.com/ipfs/go-ipfs/p2p/peer"
9
+ path "github.com/ipfs/go-ipfs/path"
10
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
11
)
12
@@ -94,7 +94,7 @@ func ParseMultiaddr(m ma.Multiaddr) (a IPFSAddr, err error) {
94
}
95
96
// make sure ipfs id parses as a peer.ID
97
- peerIdParts := strings.Split(ipfspart.String(), "/")
97
+ peerIdParts := path.SplitList(ipfspart.String())
98
peerIdStr := peerIdParts[len(peerIdParts)-1]
99
id, err := peer.IDB58Decode(peerIdStr)
100
if err != nil {
util/ipfsaddr/ipfsaddr_test.go
+2
-2
@@ -1,11 +1,11 @@
1
package ipfsaddr
2
3
import (
4
- "strings"
4
"testing"
5
6
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
7
peer "github.com/ipfs/go-ipfs/p2p/peer"
8
+ path "github.com/ipfs/go-ipfs/path"
9
)
10
11
var good = []string{
@@ -87,7 +87,7 @@ func TestIDMatches(t *testing.T) {
87
continue
88
}
89
90
- sp := strings.Split(g, "/")
90
+ sp := path.SplitList(g)
91
sid := sp[len(sp)-1]
92
id, err := peer.IDB58Decode(sid)
93
if err != nil {