1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

Merge pull request #4365 from adamliesko/fix/cmd/path-parsing-trailing-slash

core: get cmd outPath remove trailing slash - fixes #3729
This commit is contained in:
Whyrusleeping
2017-11-18 14:24:13 -08:00
committed by GitHub
2 changed files with 72 additions and 5 deletions

View File

@ -120,11 +120,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
return
}
outPath, _, _ := req.Option("output").String()
if len(outPath) == 0 {
_, outPath = gopath.Split(req.Arguments()[0])
outPath = gopath.Clean(outPath)
}
outPath := getOutPath(req)
cmplvl, err := getCompressOptions(req)
if err != nil {
@ -188,6 +184,16 @@ func makeProgressBar(out io.Writer, l int64) *pb.ProgressBar {
return bar
}
func getOutPath(req cmds.Request) string {
outPath, _, _ := req.Option("output").String()
if outPath == "" {
trimmed := strings.TrimRight(req.Arguments()[0], "/")
_, outPath = gopath.Split(trimmed)
outPath = gopath.Clean(outPath)
}
return outPath
}
type getWriter struct {
Out io.Writer // for output to user
Err io.Writer // for progress bar output

61
core/commands/get_test.go Normal file
View File

@ -0,0 +1,61 @@
package commands
import (
"testing"
"gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
"gx/ipfs/QmUgr8HrEkQqXfBPtj1A2UEg1V7cvhUhDsmL44wFPCJk5k/go-ipfs-cmds"
)
func TestGetOutputPath(t *testing.T) {
cases := []struct {
args []string
opts cmdkit.OptMap
outPath string
}{
{
args: []string{"/ipns/multiformats.io/"},
opts: map[string]interface{}{
"output": "takes-precedence",
},
outPath: "takes-precedence",
},
{
args: []string{"/ipns/multiformats.io/", "some-other-arg-to-be-ignored"},
opts: cmdkit.OptMap{
"output": "takes-precedence",
},
outPath: "takes-precedence",
},
{
args: []string{"/ipns/multiformats.io/"},
outPath: "multiformats.io",
opts: cmdkit.OptMap{},
},
{
args: []string{"/ipns/multiformats.io/logo.svg/"},
outPath: "logo.svg",
opts: cmdkit.OptMap{},
},
{
args: []string{"/ipns/multiformats.io", "some-other-arg-to-be-ignored"},
outPath: "multiformats.io",
opts: cmdkit.OptMap{},
},
}
defOpts, err := GetCmd.GetOptions([]string{})
if err != nil {
t.Fatalf("error getting default command options: %v", err)
}
for _, tc := range cases {
req, err := cmds.NewRequest([]string{}, tc.opts, tc.args, nil, GetCmd, defOpts)
if err != nil {
t.Fatalf("error creating a command request: %v", err)
}
if outPath := getOutPath(req); outPath != tc.outPath {
t.Errorf("expected outPath %s to be %s", outPath, tc.outPath)
}
}
}