From a7b5361054fd62e8d214401df4b80dc9bcdd12f3 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Thu, 24 Dec 2020 11:49:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gocos.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/gocos.go b/gocos.go index 214c0af..0395434 100644 --- a/gocos.go +++ b/gocos.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "github.com/tencentyun/cos-go-sdk-v5" "io/fs" "log" @@ -13,7 +14,6 @@ import ( ) const ( - CodeErr = 1 SecretID = "secret_id" SecretKey = "secret_key" BucketURL = "bucket_url" @@ -23,6 +23,11 @@ const ( PathSeparator = "/" ) +func ErrExit(err error) { + log.Println(err.Error()) + os.Exit(1) +} + func GetConfig(key string) string { key = "PLUGIN_" + strings.ToUpper(key) return os.Getenv(key) @@ -59,8 +64,7 @@ func main() { stripPrefix = GetConfig(StripPrefix) ) if !VarIsEmpty(secretID, secretKey, bucketURL, source, target) { - os.Exit(CodeErr) - return + ErrExit(errors.New("input error")) } sourceFiles := make([]string, 0, 0) if err = filepath.WalkDir(source, func(path string, d fs.DirEntry, err error) error { @@ -69,8 +73,7 @@ func main() { } return nil }); err != nil { - os.Exit(CodeErr) - return + ErrExit(err) } sourceLen := len(sourceFiles) if sourceLen == 0 { @@ -81,8 +84,7 @@ func main() { } u, err := url.Parse(bucketURL) if err != nil { - os.Exit(CodeErr) - return + ErrExit(err) } cosClient := cos.NewClient( &cos.BaseURL{ @@ -105,8 +107,7 @@ func main() { ACLHeaderOptions: &cos.ACLHeaderOptions{}, ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{}, }); err != nil { - os.Exit(CodeErr) - return + ErrExit(err) } log.Printf("source:%s target:%s\r\n", local, remote) }