From 416317df6373b279067ef28fdab0c09467f345d5 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Wed, 22 Feb 2023 20:28:10 +0100 Subject: [PATCH] little enhancements, supports dirs --- README.md | 2 ++ upctl/cmd/root.go | 4 ++++ upctl/lib/client.go | 36 +++++++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 626abe1..a9c63d3 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,5 @@ Simple standalone file upload server with api and cli - add auth options (access key, users, roles, oauth2) - add metrics - add upctl command to remove a file +- upd: add short uuid to files, in case multiple files with the same + name are being uploaded diff --git a/upctl/cmd/root.go b/upctl/cmd/root.go index 7b6b8cc..f3e0d6e 100644 --- a/upctl/cmd/root.go +++ b/upctl/cmd/root.go @@ -68,6 +68,10 @@ func Execute() { return completion(cmd, ShowCompletion) } + if len(args) == 0 { + return errors.New("No files specified to upload!") + } + // errors at this stage do not cause the usage to be shown cmd.SilenceUsage = true diff --git a/upctl/lib/client.go b/upctl/lib/client.go index ec3c7e7..c05bb9e 100644 --- a/upctl/lib/client.go +++ b/upctl/lib/client.go @@ -19,11 +19,12 @@ package lib import ( "encoding/json" - "errors" + //"errors" "fmt" "github.com/imroc/req/v3" "github.com/tlinden/up/upctl/cfg" - //"path/filepath" + "os" + "path/filepath" "time" ) @@ -34,10 +35,6 @@ type Response struct { } func Runclient(c *cfg.Config, args []string) error { - if len(args) == 0 { - return errors.New("No files specified to upload.") - } - client := req.C() if c.Debug { client.DevMode() @@ -49,7 +46,31 @@ func Runclient(c *cfg.Config, args []string) error { rq := client.R() for _, file := range args { - rq.SetFile("upload[]", file) + info, err := os.Stat(file) + + if os.IsNotExist(err) { + return err + } + + if info.IsDir() { + err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() { + rq.SetFile("upload[]", path) + } + return nil + }) + + if err != nil { + return err + } + } else { + rq.SetFile("upload[]", file) + } + } if c.Retries > 0 { @@ -73,6 +94,7 @@ func Runclient(c *cfg.Config, args []string) error { }). SetUploadCallbackWithInterval(func(info req.UploadInfo) { fmt.Printf("\r%q uploaded %.2f%%", info.FileName, float64(info.UploadedSize)/float64(info.FileSize)*100.0) + fmt.Println() }, 10*time.Millisecond). Post(url)