diff --git a/core/commands/get.go b/core/commands/get.go index ec70ab66bfa86522104376fea06e3682c89024a0..1fef99f6414d2a36ea33aed35b371d3455d7afa2 100644 --- a/core/commands/get.go +++ b/core/commands/get.go @@ -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 diff --git a/core/commands/get_test.go b/core/commands/get_test.go new file mode 100644 index 0000000000000000000000000000000000000000..032fc1095877cc4bede629109b1b8208e9b69be8 --- /dev/null +++ b/core/commands/get_test.go @@ -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) + } + } +}