Skip to content

Commit

Permalink
Merge pull request #158 from jmank88/webfile-fpath
Browse files Browse the repository at this point in the history
set WebFile fpath to URL base
  • Loading branch information
Stebalien committed Mar 22, 2019
2 parents 066003e + a6ef022 commit 03b1446
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
return err
}
} else if u := isURL(fpath); u != nil {
base := urlBase(u)
fpath = base
if _, ok := fileArgs[fpath]; ok {
// Ensure a unique fpath by suffixing ' (n)'.
for i := 1; ; i++ {
fpath = fmt.Sprintf("%s (%d)", base, i)
if _, ok := fileArgs[fpath]; !ok {
break
}
}
}
file = files.NewWebFile(u)
} else {
fpath = filepath.ToSlash(filepath.Clean(fpath))
Expand Down Expand Up @@ -363,6 +374,13 @@ func isURL(path string) *url.URL {
}
}

func urlBase(u *url.URL) string {
if u.Path == "" {
return u.Host
}
return path.Base(u.Path)
}

func splitkv(opt string) (k, v string, ok bool) {
split := strings.SplitN(opt, "=", 2)
if len(split) == 2 {
Expand Down
19 changes: 19 additions & 0 deletions cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -534,3 +535,21 @@ func Test_isURL(t *testing.T) {
}
}
}

func Test_urlBase(t *testing.T) {
for _, test := range []struct{ url, base string }{
{"http://host", "host"},
{"http://host/test", "test"},
{"http://host/test?param=val", "test"},
{"http://host/test?param=val&param2=val", "test"},
} {
u, err := url.Parse(test.url)
if err != nil {
t.Errorf("failed to parse %q: %v", test.url, err)
continue
}
if got := urlBase(u); got != test.base {
t.Errorf("expected %q but got %q", test.base, got)
}
}
}

0 comments on commit 03b1446

Please sign in to comment.