From d4b8ebb4b2759b71628110c604b2b19777e08d92 Mon Sep 17 00:00:00 2001 From: Christoph Polcin Date: Thu, 23 Oct 2014 14:10:31 +0200 Subject: [PATCH] implement Remove --- client.go | 14 ++++++++++++++ main/client.go | 9 ++++++++- requests.go | 9 +++++++++ utils.go | 7 +++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 9aa8ed8..ec99a0f 100644 --- a/client.go +++ b/client.go @@ -117,6 +117,20 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) { return files, err } +func (c *Client) Remove(path string) error { + rs, err := c.reqDo("DELETE", path, nil) + if err != nil { + return err + } + defer rs.Body.Close() + + if rs.StatusCode == 200 { + return nil + } else { + return Error(rs) + } +} + func (c *Client) Read(path string) { fmt.Println("Read " + path) } diff --git a/main/client.go b/main/client.go index f7248bc..499ee8f 100644 --- a/main/client.go +++ b/main/client.go @@ -22,7 +22,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") + m := flag.String("X", "GET", "Method: LIST aka PROPFIND, GET, DELETE") flag.Parse() if *root == "URL" { @@ -46,7 +46,14 @@ func main() { } else { fmt.Println(err) } + case "GET": c.Read(path) + + case "DELETE", "RM": + if err := c.Remove(path); err != nil { + fmt.Println(err) + } + default: Fail(nil) } } else { diff --git a/requests.go b/requests.go index 3b881f3..22b7743 100644 --- a/requests.go +++ b/requests.go @@ -21,6 +21,15 @@ func (c *Client) req(method string, path string, body io.Reader) (req *http.Requ return req, nil } +func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Response, error) { + rq, err := c.req(method, path, body) + if err != nil { + return nil, err + } + + return c.c.Do(rq) +} + func (c *Client) Options(path string) (*http.Response, error) { rq, err := c.req("OPTIONS", path, nil) if err != nil { diff --git a/utils.go b/utils.go index 8246f10..ea2e053 100644 --- a/utils.go +++ b/utils.go @@ -3,12 +3,19 @@ package gowebdav import ( "bytes" "encoding/xml" + "errors" + "fmt" "io" + "net/http" "strconv" "strings" "time" ) +func Error(r *http.Response) error { + return errors.New(fmt.Sprintf("%s - %s %s", r.Status, r.Request.Method, r.Request.URL.String())) +} + func FixSlash(s string) string { if !strings.HasSuffix(s, "/") { s += "/"