core: get cmd outPath removes trailing slash - fixes #3729
When using path with trailing slashes, ipfs get command had issues with trying to store content into '.' folder (as the file part of the path was empty). Now, it correctly stores the file into desired folder, thanks to the removal of trailing slash by introducing a getOutPath function in get command, which does proper parsing Of the output path. BEFORE: $ .pfs get /ipns/multiformats.io/ Saving file(s) to . NOW: $ ./ipfs get /ipns/multiformats.io/ Saving file(s) to multiformats.io License: MIT Signed-off-by: adamliesko <adamliesko@gmail.com>
adamliesko committed
Nov 18, 2017 at 20:04 UTC
a09d97465270ef441791b65bca98d37c25eb0564
2 files changed
+72
-5
core/commands/get.go
+11
-5
@@ -120,11 +120,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
120
return
121
}
122
123
- outPath, _, _ := req.Option("output").String()
124
- if len(outPath) == 0 {
125
- _, outPath = gopath.Split(req.Arguments()[0])
126
- outPath = gopath.Clean(outPath)
127
- }
123
+ outPath := getOutPath(req)
124
125
cmplvl, err := getCompressOptions(req)
126
if err != nil {
@@ -188,6 +184,16 @@ func makeProgressBar(out io.Writer, l int64) *pb.ProgressBar {
184
return bar
185
}
186
187
+func getOutPath(req cmds.Request) string {
188
+ outPath, _, _ := req.Option("output").String()
189
+ if outPath == "" {
190
+ trimmed := strings.TrimRight(req.Arguments()[0], "/")
191
+ _, outPath = gopath.Split(trimmed)
192
+ outPath = gopath.Clean(outPath)
193
+ }
194
+ return outPath
195
+}
196
+
197
type getWriter struct {
198
Out io.Writer // for output to user
199
Err io.Writer // for progress bar output
core/commands/get_test.go
new
+61
@@ -0,0 +1,61 @@
1
+package commands
2
+
3
+import (
4
+ "testing"
5
+
6
+ "gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
7
+ "gx/ipfs/QmUgr8HrEkQqXfBPtj1A2UEg1V7cvhUhDsmL44wFPCJk5k/go-ipfs-cmds"
8
+)
9
+
10
+func TestGetOutputPath(t *testing.T) {
11
+ cases := []struct {
12
+ args []string
13
+ opts cmdkit.OptMap
14
+ outPath string
15
+ }{
16
+ {
17
+ args: []string{"/ipns/multiformats.io/"},
18
+ opts: map[string]interface{}{
19
+ "output": "takes-precedence",
20
+ },
21
+ outPath: "takes-precedence",
22
+ },
23
+ {
24
+ args: []string{"/ipns/multiformats.io/", "some-other-arg-to-be-ignored"},
25
+ opts: cmdkit.OptMap{
26
+ "output": "takes-precedence",
27
+ },
28
+ outPath: "takes-precedence",
29
+ },
30
+ {
31
+ args: []string{"/ipns/multiformats.io/"},
32
+ outPath: "multiformats.io",
33
+ opts: cmdkit.OptMap{},
34
+ },
35
+ {
36
+ args: []string{"/ipns/multiformats.io/logo.svg/"},
37
+ outPath: "logo.svg",
38
+ opts: cmdkit.OptMap{},
39
+ },
40
+ {
41
+ args: []string{"/ipns/multiformats.io", "some-other-arg-to-be-ignored"},
42
+ outPath: "multiformats.io",
43
+ opts: cmdkit.OptMap{},
44
+ },
45
+ }
46
+
47
+ defOpts, err := GetCmd.GetOptions([]string{})
48
+ if err != nil {
49
+ t.Fatalf("error getting default command options: %v", err)
50
+ }
51
+
52
+ for _, tc := range cases {
53
+ req, err := cmds.NewRequest([]string{}, tc.opts, tc.args, nil, GetCmd, defOpts)
54
+ if err != nil {
55
+ t.Fatalf("error creating a command request: %v", err)
56
+ }
57
+ if outPath := getOutPath(req); outPath != tc.outPath {
58
+ t.Errorf("expected outPath %s to be %s", outPath, tc.outPath)
59
+ }
60
+ }
61
+}