提交 743f3edc 作者: rht 提交者: Jeromy

strings.Split -> path.SplitList

License: MIT
Signed-off-by: 's avatarrht <rhtbot@gmail.com>
上级 ffd85923
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
cmds "github.com/ipfs/go-ipfs/commands" cmds "github.com/ipfs/go-ipfs/commands"
files "github.com/ipfs/go-ipfs/commands/files" files "github.com/ipfs/go-ipfs/commands/files"
path "github.com/ipfs/go-ipfs/path"
) )
// Parse parses the data in a http.Request and returns a command Request object // 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) { ...@@ -16,32 +17,33 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
if !strings.HasPrefix(r.URL.Path, ApiPath) { if !strings.HasPrefix(r.URL.Path, ApiPath) {
return nil, errors.New("Unexpected path prefix") return nil, errors.New("Unexpected path prefix")
} }
path := strings.Split(strings.TrimPrefix(r.URL.Path, ApiPath+"/"), "/") pth := path.SplitList(strings.TrimPrefix(r.URL.Path, ApiPath+"/"))
stringArgs := make([]string, 0) stringArgs := make([]string, 0)
if err := apiVersionMatches(r); err != nil { if err := apiVersionMatches(r); err != nil {
if path[0] != "version" { // compatibility with previous version check if pth[0] != "version" { // compatibility with previous version check
return nil, err return nil, err
} }
} }
cmd, err := root.Get(path[:len(path)-1]) cmd, err := root.Get(pth[:len(pth)-1])
if err != nil { if err != nil {
// 404 if there is no command at that path // 404 if there is no command at that path
return nil, ErrNotFound return nil, ErrNotFound
} }
if sub := cmd.Subcommand(path[len(path)-1]); sub == nil { if sub := cmd.Subcommand(pth[len(pth)-1]); sub == nil {
if len(path) <= 1 { if len(pth) <= 1 {
return nil, ErrNotFound return nil, ErrNotFound
} }
// if the last string in the path isn't a subcommand, use it as an argument // if the last string in the path isn't a subcommand, use it as an argument
// e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command) // e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
stringArgs = append(stringArgs, path[len(path)-1]) stringArgs = append(stringArgs, pth[len(pth)-1])
path = path[:len(path)-1] pth = pth[:len(pth)-1]
} else { } else {
cmd = sub cmd = sub
} }
...@@ -93,7 +95,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { ...@@ -93,7 +95,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
} }
} }
optDefs, err := root.GetOptions(path) optDefs, err := root.GetOptions(pth)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -116,7 +118,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) { ...@@ -116,7 +118,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
return nil, fmt.Errorf("File argument '%s' is required", requiredFile) return nil, fmt.Errorf("File argument '%s' is required", requiredFile)
} }
req, err := cmds.NewRequest(path, opts, args, f, cmd, optDefs) req, err := cmds.NewRequest(pth, opts, args, f, cmd, optDefs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"strings"
"time" "time"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
...@@ -600,7 +599,7 @@ PutValue will store the given key value pair in the dht. ...@@ -600,7 +599,7 @@ PutValue will store the given key value pair in the dht.
} }
func escapeDhtKey(s string) (key.Key, error) { func escapeDhtKey(s string) (key.Key, error) {
parts := strings.Split(s, "/") parts := path.SplitList(s)
switch len(parts) { switch len(parts) {
case 1: case 1:
return key.B58KeyDecode(s), nil return key.B58KeyDecode(s), nil
......
...@@ -245,8 +245,7 @@ Examples: ...@@ -245,8 +245,7 @@ Examples:
res.SetOutput(&FilesLsOutput{listing}) res.SetOutput(&FilesLsOutput{listing})
return return
case *mfs.File: case *mfs.File:
parts := strings.Split(path, "/") _, name := gopath.Split(path)
name := parts[len(parts)-1]
out := &FilesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}} out := &FilesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}}
res.SetOutput(out) res.SetOutput(out)
return return
......
...@@ -246,7 +246,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request ...@@ -246,7 +246,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
var backLink string = prefix + urlPath var backLink string = prefix + urlPath
// don't go further up than /ipfs/$hash/ // don't go further up than /ipfs/$hash/
pathSplit := strings.Split(backLink, "/") pathSplit := path.SplitList(backLink)
switch { switch {
// keep backlink // keep backlink
case len(pathSplit) == 3: // url: /ipfs/$hash case len(pathSplit) == 3: // url: /ipfs/$hash
......
...@@ -2,7 +2,6 @@ package dagutils ...@@ -2,7 +2,6 @@ package dagutils
import ( import (
"errors" "errors"
"strings"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
...@@ -12,6 +11,7 @@ import ( ...@@ -12,6 +11,7 @@ import (
bserv "github.com/ipfs/go-ipfs/blockservice" bserv "github.com/ipfs/go-ipfs/blockservice"
offline "github.com/ipfs/go-ipfs/exchange/offline" offline "github.com/ipfs/go-ipfs/exchange/offline"
dag "github.com/ipfs/go-ipfs/merkledag" dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
) )
type Editor struct { type Editor struct {
...@@ -76,8 +76,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s ...@@ -76,8 +76,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
return root, nil return root, nil
} }
func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *dag.Node, create func() *dag.Node) error { func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert *dag.Node, create func() *dag.Node) error {
splpath := strings.Split(path, "/") splpath := path.SplitList(pth)
nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create) nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
if err != nil { if err != nil {
return err return err
...@@ -130,8 +130,8 @@ func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.Node, path []st ...@@ -130,8 +130,8 @@ func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.Node, path []st
return root, nil return root, nil
} }
func (e *Editor) RmLink(ctx context.Context, path string) error { func (e *Editor) RmLink(ctx context.Context, pth string) error {
splpath := strings.Split(path, "/") splpath := path.SplitList(pth)
nd, err := e.rmLink(ctx, e.root, splpath) nd, err := e.rmLink(ctx, e.root, splpath)
if err != nil { if err != nil {
return err return err
......
package dagutils package dagutils
import ( import (
"strings"
"testing" "testing"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
dag "github.com/ipfs/go-ipfs/merkledag" dag "github.com/ipfs/go-ipfs/merkledag"
mdtest "github.com/ipfs/go-ipfs/merkledag/test" mdtest "github.com/ipfs/go-ipfs/merkledag/test"
path "github.com/ipfs/go-ipfs/path"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
) )
...@@ -43,8 +43,8 @@ func TestAddLink(t *testing.T) { ...@@ -43,8 +43,8 @@ func TestAddLink(t *testing.T) {
} }
} }
func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path string, exp key.Key) { func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, pth string, exp key.Key) {
parts := strings.Split(path, "/") parts := path.SplitList(pth)
cur := root cur := root
for _, e := range parts { for _, e := range parts {
nxt, err := cur.GetLinkedNode(context.Background(), ds, e) nxt, err := cur.GetLinkedNode(context.Background(), ds, e)
......
...@@ -8,12 +8,12 @@ import ( ...@@ -8,12 +8,12 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"sort" "sort"
"strings"
"testing" "testing"
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/ipfs/go-ipfs/path"
bstore "github.com/ipfs/go-ipfs/blocks/blockstore" bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
key "github.com/ipfs/go-ipfs/blocks/key" 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,8 +43,8 @@ func getRandFile(t *testing.T, ds dag.DAGService, size int64) *dag.Node {
return nd return nd
} }
func mkdirP(t *testing.T, root *Directory, path string) *Directory { func mkdirP(t *testing.T, root *Directory, pth string) *Directory {
dirs := strings.Split(path, "/") dirs := path.SplitList(pth)
cur := root cur := root
for _, d := range dirs { for _, d := range dirs {
n, err := cur.Mkdir(d) n, err := cur.Mkdir(d)
...@@ -69,15 +69,15 @@ func mkdirP(t *testing.T, root *Directory, path string) *Directory { ...@@ -69,15 +69,15 @@ func mkdirP(t *testing.T, root *Directory, path string) *Directory {
return cur return cur
} }
func assertDirAtPath(root *Directory, path string, children []string) error { func assertDirAtPath(root *Directory, pth string, children []string) error {
fsn, err := DirLookup(root, path) fsn, err := DirLookup(root, pth)
if err != nil { if err != nil {
return err return err
} }
dir, ok := fsn.(*Directory) dir, ok := fsn.(*Directory)
if !ok { if !ok {
return fmt.Errorf("%s was not a directory", path) return fmt.Errorf("%s was not a directory", pth)
} }
listing, err := dir.List() listing, err := dir.List()
...@@ -113,13 +113,13 @@ func compStrArrs(a, b []string) bool { ...@@ -113,13 +113,13 @@ func compStrArrs(a, b []string) bool {
return true return true
} }
func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path string) error { func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, pth string) error {
parts := strings.Split(path, "/") parts := path.SplitList(pth)
cur := root cur := root
for i, d := range parts[:len(parts)-1] { for i, d := range parts[:len(parts)-1] {
next, err := cur.Child(d) next, err := cur.Child(d)
if err != nil { if err != nil {
return fmt.Errorf("looking for %s failed: %s", path, err) return fmt.Errorf("looking for %s failed: %s", pth, err)
} }
nextDir, ok := next.(*Directory) nextDir, ok := next.(*Directory)
...@@ -138,7 +138,7 @@ func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path st ...@@ -138,7 +138,7 @@ func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path st
file, ok := finaln.(*File) file, ok := finaln.(*File)
if !ok { if !ok {
return fmt.Errorf("%s was not a file!", path) return fmt.Errorf("%s was not a file!", pth)
} }
out, err := ioutil.ReadAll(file) out, err := ioutil.ReadAll(file)
......
...@@ -101,7 +101,7 @@ func PutNode(r *Root, path string, nd *dag.Node) error { ...@@ -101,7 +101,7 @@ func PutNode(r *Root, path string, nd *dag.Node) error {
// Mkdir creates a directory at 'path' under the directory 'd', creating // Mkdir creates a directory at 'path' under the directory 'd', creating
// intermediary directories as needed if 'parents' is set to true // intermediary directories as needed if 'parents' is set to true
func Mkdir(r *Root, pth string, parents bool) error { func Mkdir(r *Root, pth string, parents bool) error {
parts := strings.Split(pth, "/") parts := path.SplitList(pth)
if parts[0] == "" { if parts[0] == "" {
parts = parts[1:] parts = parts[1:]
} }
...@@ -159,7 +159,7 @@ func Lookup(r *Root, path string) (FSNode, error) { ...@@ -159,7 +159,7 @@ func Lookup(r *Root, path string) (FSNode, error) {
// under the directory 'd' // under the directory 'd'
func DirLookup(d *Directory, pth string) (FSNode, error) { func DirLookup(d *Directory, pth string) (FSNode, error) {
pth = strings.Trim(pth, "/") pth = strings.Trim(pth, "/")
parts := strings.Split(pth, "/") parts := path.SplitList(pth)
if len(parts) == 1 && parts[0] == "" { if len(parts) == 1 && parts[0] == "" {
return d, nil return d, nil
} }
......
...@@ -106,3 +106,7 @@ func (p *Path) IsValid() error { ...@@ -106,3 +106,7 @@ func (p *Path) IsValid() error {
func Join(pths []string) string { func Join(pths []string) string {
return strings.Join(pths, "/") return strings.Join(pths, "/")
} }
func SplitList(pth string) []string {
return strings.Split(pth, "/")
}
...@@ -2,9 +2,9 @@ package record ...@@ -2,9 +2,9 @@ package record
import ( import (
"errors" "errors"
"strings"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
path "github.com/ipfs/go-ipfs/path"
) )
// A SelectorFunc selects the best value for the given key from // 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,7 +18,7 @@ func (s Selector) BestRecord(k key.Key, recs [][]byte) (int, error) {
return 0, errors.New("no records given!") return 0, errors.New("no records given!")
} }
parts := strings.Split(string(k), "/") parts := path.SplitList(string(k))
if len(parts) < 3 { if len(parts) < 3 {
log.Infof("Record key does not have selectorfunc: %s", k) log.Infof("Record key does not have selectorfunc: %s", k)
return 0, errors.New("record key does not have selectorfunc") return 0, errors.New("record key does not have selectorfunc")
......
...@@ -3,10 +3,10 @@ package record ...@@ -3,10 +3,10 @@ package record
import ( import (
"bytes" "bytes"
"errors" "errors"
"strings"
key "github.com/ipfs/go-ipfs/blocks/key" key "github.com/ipfs/go-ipfs/blocks/key"
ci "github.com/ipfs/go-ipfs/p2p/crypto" ci "github.com/ipfs/go-ipfs/p2p/crypto"
path "github.com/ipfs/go-ipfs/path"
pb "github.com/ipfs/go-ipfs/routing/dht/pb" pb "github.com/ipfs/go-ipfs/routing/dht/pb"
u "github.com/ipfs/go-ipfs/util" u "github.com/ipfs/go-ipfs/util"
) )
...@@ -37,7 +37,7 @@ type ValidChecker struct { ...@@ -37,7 +37,7 @@ type ValidChecker struct {
// It runs needed validators // It runs needed validators
func (v Validator) VerifyRecord(r *pb.Record) error { func (v Validator) VerifyRecord(r *pb.Record) error {
// Now, check validity func // Now, check validity func
parts := strings.Split(r.GetKey(), "/") parts := path.SplitList(r.GetKey())
if len(parts) < 3 { if len(parts) < 3 {
log.Infof("Record key does not have validator: %s", key.Key(r.GetKey())) log.Infof("Record key does not have validator: %s", key.Key(r.GetKey()))
return nil return nil
...@@ -54,7 +54,7 @@ func (v Validator) VerifyRecord(r *pb.Record) error { ...@@ -54,7 +54,7 @@ func (v Validator) VerifyRecord(r *pb.Record) error {
func (v Validator) IsSigned(k key.Key) (bool, error) { func (v Validator) IsSigned(k key.Key) (bool, error) {
// Now, check validity func // Now, check validity func
parts := strings.Split(string(k), "/") parts := path.SplitList(string(k))
if len(parts) < 3 { if len(parts) < 3 {
log.Infof("Record key does not have validator: %s", k) log.Infof("Record key does not have validator: %s", k)
return false, nil return false, nil
......
...@@ -98,7 +98,7 @@ func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) { ...@@ -98,7 +98,7 @@ func ImportTar(r io.Reader, ds dag.DAGService) (*dag.Node, error) {
// adds a '-' to the beginning of each path element so we can use 'data' as a // adds a '-' to the beginning of each path element so we can use 'data' as a
// special link in the structure without having to worry about // special link in the structure without having to worry about
func escapePath(pth string) string { func escapePath(pth string) string {
elems := strings.Split(strings.Trim(pth, "/"), "/") elems := path.SplitList(strings.Trim(pth, "/"))
for i, e := range elems { for i, e := range elems {
elems[i] = "-" + e elems[i] = "-" + e
} }
......
...@@ -2,11 +2,11 @@ package ipfsaddr ...@@ -2,11 +2,11 @@ package ipfsaddr
import ( import (
"errors" "errors"
"strings"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
peer "github.com/ipfs/go-ipfs/p2p/peer" peer "github.com/ipfs/go-ipfs/p2p/peer"
path "github.com/ipfs/go-ipfs/path"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log" logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
) )
...@@ -94,7 +94,7 @@ func ParseMultiaddr(m ma.Multiaddr) (a IPFSAddr, err error) { ...@@ -94,7 +94,7 @@ func ParseMultiaddr(m ma.Multiaddr) (a IPFSAddr, err error) {
} }
// make sure ipfs id parses as a peer.ID // make sure ipfs id parses as a peer.ID
peerIdParts := strings.Split(ipfspart.String(), "/") peerIdParts := path.SplitList(ipfspart.String())
peerIdStr := peerIdParts[len(peerIdParts)-1] peerIdStr := peerIdParts[len(peerIdParts)-1]
id, err := peer.IDB58Decode(peerIdStr) id, err := peer.IDB58Decode(peerIdStr)
if err != nil { if err != nil {
......
package ipfsaddr package ipfsaddr
import ( import (
"strings"
"testing" "testing"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
peer "github.com/ipfs/go-ipfs/p2p/peer" peer "github.com/ipfs/go-ipfs/p2p/peer"
path "github.com/ipfs/go-ipfs/path"
) )
var good = []string{ var good = []string{
...@@ -87,7 +87,7 @@ func TestIDMatches(t *testing.T) { ...@@ -87,7 +87,7 @@ func TestIDMatches(t *testing.T) {
continue continue
} }
sp := strings.Split(g, "/") sp := path.SplitList(g)
sid := sp[len(sp)-1] sid := sp[len(sp)-1]
id, err := peer.IDB58Decode(sid) id, err := peer.IDB58Decode(sid)
if err != nil { if err != nil {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论