From c1fc968bc942adbb7d97ddb6f565339cb02bc384 Mon Sep 17 00:00:00 2001 From: Christoph Polcin Date: Thu, 23 Oct 2014 15:00:20 +0200 Subject: [PATCH] implement MkDir aka MKCOL --- client.go | 14 ++++++++++++++ main/client.go | 19 ++++++++++++++++--- requests.go | 14 ++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index a2543d1..c24991c 100644 --- a/client.go +++ b/client.go @@ -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) { fmt.Println("Read " + path) } diff --git a/main/client.go b/main/client.go index 900a5f5..019414d 100644 --- a/main/client.go +++ b/main/client.go @@ -14,6 +14,10 @@ func Fail(err interface{}) { fmt.Println("Usage: client FLAGS ARGS") fmt.Println("Flags:") flag.PrintDefaults() + fmt.Println("Method:") + fmt.Println(" LIST, PROPFIND:") + fmt.Println(" RM, DELETE, DEL:") + fmt.Println(" MKDIR, MKCOL:") } os.Exit(-1) } @@ -22,7 +26,7 @@ func main() { root := flag.String("root", "URL", "WebDAV Endpoint") usr := flag.String("user", "", "user") pw := flag.String("pw", "", "password") - m := flag.String("X", "GET", "Method: LIST aka PROPFIND, GET, DELETE") + m := flag.String("X", "GET", "Method ...") flag.Parse() if *root == "URL" { @@ -39,7 +43,7 @@ func main() { switch *m { case "LIST", "PROPFIND": 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 { fmt.Println(f) } @@ -49,9 +53,18 @@ func main() { case "GET": c.Read(path) - case "DELETE", "RM": + case "DELETE", "RM", "DEL": if err := c.Remove(path); err != nil { 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) diff --git a/requests.go b/requests.go index 22b7743..3b1aa3b 100644 --- a/requests.go +++ b/requests.go @@ -30,6 +30,20 @@ func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Respon 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) { rq, err := c.req("OPTIONS", path, nil) if err != nil {