add ability to read login / pw from ~/.netrc

This commit is contained in:
Aaron Bieber
2017-10-05 09:07:51 -06:00
committed by Christoph Polcin
parent fbcb29d33e
commit a33240e4ab
3 changed files with 82 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ import (
d "github.com/studio-b12/gowebdav"
"io"
"os"
"os/user"
"path/filepath"
"strings"
)
@@ -15,6 +16,7 @@ func main() {
root := flag.String("root", os.Getenv("ROOT"), "WebDAV Endpoint [ENV.ROOT]")
usr := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
pw := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
netrc := flag.String("netrc-file", filepath.Join(getHome(), ".netrc"), "read login from netrc file")
method := flag.String("X", "", `Method:
LS <PATH>
STAT <PATH>
@@ -40,6 +42,13 @@ func main() {
fail("Unsupported arguments")
}
if *pw == "" {
if u, p := d.ReadConfig(*root, *netrc); u != "" && p != "" {
usr = &u
pw = &p
}
}
c := d.NewClient(*root, *usr, *pw)
if err := c.Connect(); err != nil {
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
@@ -59,6 +68,13 @@ func fail(err interface{}) {
os.Exit(-1)
}
func getHome() string {
if u, e := user.Current(); e != nil {
return u.HomeDir
}
return os.Getenv("HOME")
}
func getCmd(method string) func(c *d.Client, p0, p1 string) error {
switch strings.ToUpper(method) {
case "LS", "LIST", "PROPFIND":