Skip to content

Commit

Permalink
Merge pull request #1984 from rht/util/path
Browse files Browse the repository at this point in the history
Remove hardcoded "/"
  • Loading branch information
jbenet committed Nov 30, 2015
2 parents e04a31d + 2ffef14 commit 28fa917
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 69 deletions.
2 changes: 1 addition & 1 deletion commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func parseOpts(args []string, root *cmds.Command) (
}
}
var end bool
end, err = parseFlag(arg[0:1], rest, mustUse)
end, err = parseFlag(arg[:1], rest, mustUse)
if err != nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"fmt"
"io"
"reflect"
"strings"

"github.com/ipfs/go-ipfs/path"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
)

Expand Down Expand Up @@ -143,16 +143,16 @@ func (c *Command) Call(req Request) Response {
}

// Resolve gets the subcommands at the given path
func (c *Command) Resolve(path []string) ([]*Command, error) {
cmds := make([]*Command, len(path)+1)
func (c *Command) Resolve(pth []string) ([]*Command, error) {
cmds := make([]*Command, len(pth)+1)
cmds[0] = c

cmd := c
for i, name := range path {
for i, name := range pth {
cmd = cmd.Subcommand(name)

if cmd == nil {
pathS := strings.Join(path[0:i], "/")
pathS := path.Join(pth[:i])
return nil, fmt.Errorf("Undefined command: '%s'", pathS)
}

Expand Down
5 changes: 3 additions & 2 deletions commands/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

cmds "github.com/ipfs/go-ipfs/commands"
path "github.com/ipfs/go-ipfs/path"
config "github.com/ipfs/go-ipfs/repo/config"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
Expand Down Expand Up @@ -85,8 +86,8 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
reader = strings.NewReader("")
}

path := strings.Join(req.Path(), "/")
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, path, query)
pth := path.Join(req.Path())
url := fmt.Sprintf(ApiUrlFormat, c.serverAddress, ApiPath, pth, query)

httpReq, err := http.NewRequest("POST", url, reader)
if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions commands/http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (

cmds "github.com/ipfs/go-ipfs/commands"
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
func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
if !strings.HasPrefix(r.URL.Path, ApiPath) {
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)

cmd, err := root.Get(path[:len(path)-1])
cmd, err := root.Get(pth[:len(pth)-1])
if err != nil {
// 404 if there is no command at that path
return nil, ErrNotFound

} else if sub := cmd.Subcommand(path[len(path)-1]); sub == nil {
if len(path) <= 1 {
} else if sub := cmd.Subcommand(pth[len(pth)-1]); sub == nil {
if len(pth) <= 1 {
return nil, ErrNotFound
}

// 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)
stringArgs = append(stringArgs, path[len(path)-1])
path = path[:len(path)-1]
stringArgs = append(stringArgs, pth[len(pth)-1])
pth = pth[:len(pth)-1]

} else {
cmd = sub
Expand Down Expand Up @@ -86,7 +87,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 {
return nil, err
}
Expand All @@ -109,7 +110,7 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
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 {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions core/commands/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"errors"
"fmt"
"io"
"strings"
"time"

key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
notif "github.com/ipfs/go-ipfs/notifications"
peer "github.com/ipfs/go-ipfs/p2p/peer"
path "github.com/ipfs/go-ipfs/path"
ipdht "github.com/ipfs/go-ipfs/routing/dht"
u "github.com/ipfs/go-ipfs/util"
)
Expand Down Expand Up @@ -599,13 +599,13 @@ PutValue will store the given key value pair in the dht.
}

func escapeDhtKey(s string) (key.Key, error) {
parts := strings.Split(s, "/")
parts := path.SplitList(s)
switch len(parts) {
case 1:
return key.B58KeyDecode(s), nil
case 3:
k := key.B58KeyDecode(parts[2])
return key.Key(strings.Join(append(parts[:2], string(k)), "/")), nil
return key.Key(path.Join(append(parts[:2], k.String()))), nil
default:
return "", errors.New("invalid key")
}
Expand Down
3 changes: 1 addition & 2 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ Examples:
}
return
case *mfs.File:
parts := strings.Split(path, "/")
name := parts[len(parts)-1]
_, name := gopath.Split(path)
out := &FilesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name, Type: 1}}}
res.SetOutput(out)
return
Expand Down
8 changes: 4 additions & 4 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
var backLink string = urlPath

// don't go further up than /ipfs/$hash/
pathSplit := strings.Split(backLink, "/")
pathSplit := path.SplitList(backLink)
switch {
// keep backlink
case len(pathSplit) == 3: // url: /ipfs/$hash
Expand All @@ -247,7 +247,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
if len(pathSplit) > 5 {
// also strip the trailing segment, because it's a backlink
backLinkParts := pathSplit[3 : len(pathSplit)-2]
backLink += strings.Join(backLinkParts, "/") + "/"
backLink += path.Join(backLinkParts) + "/"
}
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {

var newPath string
if len(rsegs) > 1 {
newPath = strings.Join(rsegs[2:], "/")
newPath = path.Join(rsegs[2:])
}

var newkey key.Key
Expand Down Expand Up @@ -440,7 +440,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {

i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("IPFS-Hash", key.String())
http.Redirect(w, r, ipfsPathPrefix+key.String()+"/"+strings.Join(components[:len(components)-1], "/"), http.StatusCreated)
http.Redirect(w, r, gopath.Join(ipfsPathPrefix+key.String(), path.Join(components[:len(components)-1])), http.StatusCreated)
}

func (i *gatewayHandler) addUserHeaders(w http.ResponseWriter) {
Expand Down
3 changes: 1 addition & 2 deletions fuse/ipns/ipns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"strings"

fuse "github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
fs "github.com/ipfs/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
Expand Down Expand Up @@ -194,7 +193,7 @@ func (s *Root) Lookup(ctx context.Context, name string) (fs.Node, error) {

segments := resolved.Segments()
if segments[0] == "ipfs" {
p := strings.Join(resolved.Segments()[1:], "/")
p := path.Join(resolved.Segments()[1:])
return &Link{s.IpfsRoot + "/" + p}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions merkledag/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dagutils

import (
"errors"
"strings"

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"
Expand All @@ -12,6 +11,7 @@ import (
bserv "github.com/ipfs/go-ipfs/blockservice"
offline "github.com/ipfs/go-ipfs/exchange/offline"
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
)

type Editor struct {
Expand Down Expand Up @@ -76,8 +76,8 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
return root, nil
}

func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *dag.Node, create func() *dag.Node) error {
splpath := strings.Split(path, "/")
func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert *dag.Node, create func() *dag.Node) error {
splpath := path.SplitList(pth)
nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
if err != nil {
return err
Expand Down Expand Up @@ -130,8 +130,8 @@ func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.Node, path []st
return root, nil
}

func (e *Editor) RmLink(ctx context.Context, path string) error {
splpath := strings.Split(path, "/")
func (e *Editor) RmLink(ctx context.Context, pth string) error {
splpath := path.SplitList(pth)
nd, err := e.rmLink(ctx, e.root, splpath)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions merkledag/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dagutils

import (
"strings"
"testing"

key "github.com/ipfs/go-ipfs/blocks/key"
dag "github.com/ipfs/go-ipfs/merkledag"
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"
)
Expand Down Expand Up @@ -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) {
parts := strings.Split(path, "/")
func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, pth string, exp key.Key) {
parts := path.SplitList(pth)
cur := root
for _, e := range parts {
nxt, err := cur.GetLinkedNode(context.Background(), ds, e)
Expand Down
20 changes: 10 additions & 10 deletions mfs/mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"io/ioutil"
"os"
"sort"
"strings"
"testing"

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"
"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"
key "github.com/ipfs/go-ipfs/blocks/key"
Expand Down Expand Up @@ -43,8 +43,8 @@ func getRandFile(t *testing.T, ds dag.DAGService, size int64) *dag.Node {
return nd
}

func mkdirP(t *testing.T, root *Directory, path string) *Directory {
dirs := strings.Split(path, "/")
func mkdirP(t *testing.T, root *Directory, pth string) *Directory {
dirs := path.SplitList(pth)
cur := root
for _, d := range dirs {
n, err := cur.Mkdir(d)
Expand All @@ -69,15 +69,15 @@ func mkdirP(t *testing.T, root *Directory, path string) *Directory {
return cur
}

func assertDirAtPath(root *Directory, path string, children []string) error {
fsn, err := DirLookup(root, path)
func assertDirAtPath(root *Directory, pth string, children []string) error {
fsn, err := DirLookup(root, pth)
if err != nil {
return err
}

dir, ok := fsn.(*Directory)
if !ok {
return fmt.Errorf("%s was not a directory", path)
return fmt.Errorf("%s was not a directory", pth)
}

listing, err := dir.List()
Expand Down Expand Up @@ -113,13 +113,13 @@ func compStrArrs(a, b []string) bool {
return true
}

func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path string) error {
parts := strings.Split(path, "/")
func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, pth string) error {
parts := path.SplitList(pth)
cur := root
for i, d := range parts[:len(parts)-1] {
next, err := cur.Child(d)
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)
Expand All @@ -138,7 +138,7 @@ func assertFileAtPath(ds dag.DAGService, root *Directory, exp *dag.Node, path st

file, ok := finaln.(*File)
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)
Expand Down
Loading

0 comments on commit 28fa917

Please sign in to comment.