implement MkDir aka MKCOL

This commit is contained in:
Christoph Polcin 2014-10-23 15:00:20 +02:00
parent 7caa8c8b7d
commit c1fc968bc9
3 changed files with 44 additions and 3 deletions

View File

@ -131,6 +131,20 @@ func (c *Client) Remove(path string) error {
} }
} }
func (c *Client) Mkdir(path string) error {
path = FixSlashes(path)
status := c.MkCol(path)
if status == 201 {
return nil
}
if status == 409 {
// TODO
}
return errors.New(fmt.Sprintf("%d", status))
}
func (c *Client) Read(path string) { func (c *Client) Read(path string) {
fmt.Println("Read " + path) fmt.Println("Read " + path)
} }

View File

@ -14,6 +14,10 @@ func Fail(err interface{}) {
fmt.Println("Usage: client FLAGS ARGS") fmt.Println("Usage: client FLAGS ARGS")
fmt.Println("Flags:") fmt.Println("Flags:")
flag.PrintDefaults() flag.PrintDefaults()
fmt.Println("Method:")
fmt.Println(" LIST, PROPFIND:")
fmt.Println(" RM, DELETE, DEL:")
fmt.Println(" MKDIR, MKCOL:")
} }
os.Exit(-1) os.Exit(-1)
} }
@ -22,7 +26,7 @@ func main() {
root := flag.String("root", "URL", "WebDAV Endpoint") root := flag.String("root", "URL", "WebDAV Endpoint")
usr := flag.String("user", "", "user") usr := flag.String("user", "", "user")
pw := flag.String("pw", "", "password") pw := flag.String("pw", "", "password")
m := flag.String("X", "GET", "Method: LIST aka PROPFIND, GET, DELETE") m := flag.String("X", "GET", "Method ...")
flag.Parse() flag.Parse()
if *root == "URL" { if *root == "URL" {
@ -39,7 +43,7 @@ func main() {
switch *m { switch *m {
case "LIST", "PROPFIND": case "LIST", "PROPFIND":
if files, err := c.ReadDir(path); err == nil { if files, err := c.ReadDir(path); err == nil {
fmt.Println(fmt.Sprintf("Resources: %d - %s", len(files), path)) fmt.Println(fmt.Sprintf("ReadDir: %s, entries: ", path, len(files)))
for _, f := range files { for _, f := range files {
fmt.Println(f) fmt.Println(f)
} }
@ -49,9 +53,18 @@ func main() {
case "GET": c.Read(path) case "GET": c.Read(path)
case "DELETE", "RM": case "DELETE", "RM", "DEL":
if err := c.Remove(path); err != nil { if err := c.Remove(path); err != nil {
fmt.Println(err) fmt.Println(err)
} else {
fmt.Println("Remove: " + path)
}
case "MKCOL", "MKDIR":
if err := c.Mkdir(path); err != nil {
fmt.Println(err)
} else {
fmt.Println("MkDir: " + path)
} }
default: Fail(nil) default: Fail(nil)

View File

@ -30,6 +30,20 @@ func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Respon
return c.c.Do(rq) return c.c.Do(rq)
} }
func (c *Client) MkCol(path string) int {
rs, err := c.reqDo("MKCOL", path, nil)
if err != nil {
return 400
}
defer rs.Body.Close()
if rs.StatusCode == 201 || rs.StatusCode == 405 {
return 201
}
return rs.StatusCode
}
func (c *Client) Options(path string) (*http.Response, error) { func (c *Client) Options(path string) (*http.Response, error) {
rq, err := c.req("OPTIONS", path, nil) rq, err := c.req("OPTIONS", path, nil)
if err != nil { if err != nil {