Skip to content

Commit

Permalink
add upload error log
Browse files Browse the repository at this point in the history
  • Loading branch information
huangnauh committed Apr 19, 2024
1 parent c0e9bc7 commit 9731c6b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
21 changes: 21 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -343,6 +344,15 @@ func NewPutCommand() cli.Command {
if c.Int("w") > 10 || c.Int("w") < 1 {
PrintErrorAndExit("max concurrent threads must between (1 - 10)")
}
errLog := c.String("err-log")
if errLog != "" {
f, err := os.OpenFile(errLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
PrintErrorAndExit("open error log file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
session.Put(
localPath,
upPath,
Expand All @@ -354,6 +364,7 @@ func NewPutCommand() cli.Command {
Flags: []cli.Flag{
cli.IntFlag{Name: "w", Usage: "max concurrent threads", Value: 5},
cli.BoolFlag{Name: "all", Usage: "upload all files including hidden files"},
cli.StringFlag{Name: "err-log", Usage: "upload file error log to file"},
},
}
}
Expand All @@ -372,6 +383,15 @@ func NewUploadCommand() cli.Command {
if isWindowsGOOS() {
filenames = globFiles(filenames)
}
errLog := c.String("err-log")
if errLog != "" {
f, err := os.OpenFile(errLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
PrintErrorAndExit("open error log file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
session.Upload(
filenames,
c.String("remote"),
Expand All @@ -384,6 +404,7 @@ func NewUploadCommand() cli.Command {
cli.BoolFlag{Name: "all", Usage: "upload all files including hidden files"},
cli.IntFlag{Name: "w", Usage: "max concurrent threads", Value: 5},
cli.StringFlag{Name: "remote", Usage: "remote path", Value: "./"},
cli.StringFlag{Name: "err-log", Usage: "upload file error log to file"},
},
}
}
Expand Down
8 changes: 7 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,18 @@ func (sess *Session) putDir(localPath, upPath string, workers int, withIgnore bo
for info := range localFiles {
rel, _ := filepath.Rel(localAbsPath, info.fpath)
desPath := path.Join(upPath, filepath.ToSlash(rel))
if fInfo, err := os.Stat(info.fpath); err == nil && fInfo.IsDir() {
fInfo, err := os.Stat(info.fpath)
if err == nil && fInfo.IsDir() {
err = sess.updriver.Mkdir(desPath)
} else {
err = sess.putFileWithProgress(info.fpath, desPath, info.fInfo)
}
if err != nil {
log.Printf("put %s to %s error: %s", info.fpath, desPath, err)
if upyun.IsTooManyRequests(err) {
time.Sleep(time.Second)
continue
}
return
}
}
Expand Down

0 comments on commit 9731c6b

Please sign in to comment.