Skip to content

Commit 03b1446

Browse files
authored
Merge pull request #158 from jmank88/webfile-fpath
set WebFile fpath to URL base
2 parents 066003e + a6ef022 commit 03b1446

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

cli/parse.go

+18
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
276276
return err
277277
}
278278
} else if u := isURL(fpath); u != nil {
279+
base := urlBase(u)
280+
fpath = base
281+
if _, ok := fileArgs[fpath]; ok {
282+
// Ensure a unique fpath by suffixing ' (n)'.
283+
for i := 1; ; i++ {
284+
fpath = fmt.Sprintf("%s (%d)", base, i)
285+
if _, ok := fileArgs[fpath]; !ok {
286+
break
287+
}
288+
}
289+
}
279290
file = files.NewWebFile(u)
280291
} else {
281292
fpath = filepath.ToSlash(filepath.Clean(fpath))
@@ -363,6 +374,13 @@ func isURL(path string) *url.URL {
363374
}
364375
}
365376

377+
func urlBase(u *url.URL) string {
378+
if u.Path == "" {
379+
return u.Host
380+
}
381+
return path.Base(u.Path)
382+
}
383+
366384
func splitkv(opt string) (k, v string, ok bool) {
367385
split := strings.SplitN(opt, "=", 2)
368386
if len(split) == 2 {

cli/parse_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"io/ioutil"
8+
"net/url"
89
"os"
910
"strings"
1011
"testing"
@@ -534,3 +535,21 @@ func Test_isURL(t *testing.T) {
534535
}
535536
}
536537
}
538+
539+
func Test_urlBase(t *testing.T) {
540+
for _, test := range []struct{ url, base string }{
541+
{"http://host", "host"},
542+
{"http://host/test", "test"},
543+
{"http://host/test?param=val", "test"},
544+
{"http://host/test?param=val&param2=val", "test"},
545+
} {
546+
u, err := url.Parse(test.url)
547+
if err != nil {
548+
t.Errorf("failed to parse %q: %v", test.url, err)
549+
continue
550+
}
551+
if got := urlBase(u); got != test.base {
552+
t.Errorf("expected %q but got %q", test.base, got)
553+
}
554+
}
555+
}

0 commit comments

Comments
 (0)