From 2595611d8a4f11abd85a59db6a98f0d5b286bf74 Mon Sep 17 00:00:00 2001 From: joyqat Date: Sun, 17 Feb 2019 17:09:35 +0800 Subject: [PATCH 1/4] impl #14 --- plugin.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin.go b/plugin.go index c5dec25..ca8d7f4 100644 --- a/plugin.go +++ b/plugin.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "path" + "strings" "time" "github.com/jackspirou/syscerts" @@ -45,6 +46,17 @@ func (p Plugin) Exec() error { } destination = path.Base(u.Path) + } else if destination[len(destination)-1] == '/' { + _ = os.MkdirAll(destination, os.ModePerm) + u, err := url.Parse(p.Config.Source) + + if err != nil { + return errors.Wrap(err, "parsing source failed") + } + + destination = destination + path.Base(u.Path) + } else { + _ = os.MkdirAll(destination[0:strings.LastIndex(destination, "/")], os.ModePerm) } log.Printf("downloading to %s", destination) From 3f15869f0e2065acbee3a6525d387248029a869c Mon Sep 17 00:00:00 2001 From: joyqat Date: Wed, 20 Feb 2019 09:23:31 +0800 Subject: [PATCH 2/4] use `filepath` instead of `path` to work on all platform --- plugin.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/plugin.go b/plugin.go index ca8d7f4..97dfe71 100644 --- a/plugin.go +++ b/plugin.go @@ -11,7 +11,7 @@ import ( "net/url" "os" "path" - "strings" + "path/filepath" "time" "github.com/jackspirou/syscerts" @@ -38,25 +38,25 @@ type ( func (p Plugin) Exec() error { destination := p.Config.Destination - if destination == "" { - u, err := url.Parse(p.Config.Source) - - if err != nil { - return errors.Wrap(err, "parsing source failed") - } + u, err := url.Parse(p.Config.Source) + if err != nil { + return errors.Wrap(err, "parsing source failed") + } + switch { + case destination == "": destination = path.Base(u.Path) - } else if destination[len(destination)-1] == '/' { - _ = os.MkdirAll(destination, os.ModePerm) - u, err := url.Parse(p.Config.Source) - + case destination[len(destination)-1] == filepath.Separator: + err = os.MkdirAll(destination, os.ModePerm) if err != nil { - return errors.Wrap(err, "parsing source failed") + return errors.Wrap(err, "parsing destination failed") } - destination = destination + path.Base(u.Path) - } else { - _ = os.MkdirAll(destination[0:strings.LastIndex(destination, "/")], os.ModePerm) + default: + err = os.MkdirAll(filepath.Dir(destination), os.ModePerm) + if err != nil { + return errors.Wrap(err, "parsing destination failed") + } } log.Printf("downloading to %s", destination) From 0081187385759524d8fa45ee524a6d23deb86cb9 Mon Sep 17 00:00:00 2001 From: joyqat Date: Wed, 20 Feb 2019 09:37:02 +0800 Subject: [PATCH 3/4] remove redundant code --- plugin.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/plugin.go b/plugin.go index 97dfe71..06400a9 100644 --- a/plugin.go +++ b/plugin.go @@ -47,11 +47,8 @@ func (p Plugin) Exec() error { case destination == "": destination = path.Base(u.Path) case destination[len(destination)-1] == filepath.Separator: - err = os.MkdirAll(destination, os.ModePerm) - if err != nil { - return errors.Wrap(err, "parsing destination failed") - } destination = destination + path.Base(u.Path) + fallthrough default: err = os.MkdirAll(filepath.Dir(destination), os.ModePerm) if err != nil { From 47f5dab31b0cc81dd13eac03cac65788559478d1 Mon Sep 17 00:00:00 2001 From: joyqat Date: Wed, 20 Feb 2019 15:54:31 +0800 Subject: [PATCH 4/4] clearify error info --- plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.go b/plugin.go index 06400a9..354ab7f 100644 --- a/plugin.go +++ b/plugin.go @@ -52,7 +52,7 @@ func (p Plugin) Exec() error { default: err = os.MkdirAll(filepath.Dir(destination), os.ModePerm) if err != nil { - return errors.Wrap(err, "parsing destination failed") + return errors.Wrap(err, "creating directory failed") } }