From 9016c12c263e3ba8d34edf5d303b63d6b782bec7 Mon Sep 17 00:00:00 2001 From: tickstep Date: Sat, 13 Aug 2022 19:38:10 +0800 Subject: [PATCH] add log flag for sync command --- docker/sync/Dockerfile | 1 + docker/sync/app.sh | 2 +- docker/sync/docker-compose.yml | 3 ++- internal/command/sync.go | 17 ++++++++++++++--- internal/syncdrive/file_action_task.go | 12 ++++++++++++ internal/syncdrive/sync_constants.go | 4 ++++ 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/docker/sync/Dockerfile b/docker/sync/Dockerfile index cd3aaf7..eb2e624 100644 --- a/docker/sync/Dockerfile +++ b/docker/sync/Dockerfile @@ -59,6 +59,7 @@ ENV ALIYUNPAN_LOCAL_DIR=/home/app/data ENV ALIYUNPAN_PAN_DIR=/sync_drive ENV ALIYUNPAN_SYNC_MODE=upload ENV ALIYUNPAN_SYNC_PRIORITY=time +ENV ALIYUNPAN_SYNC_LOG=false # 运行 ENTRYPOINT ./app.sh \ No newline at end of file diff --git a/docker/sync/app.sh b/docker/sync/app.sh index 91145bc..26d2261 100755 --- a/docker/sync/app.sh +++ b/docker/sync/app.sh @@ -40,4 +40,4 @@ else fi ./aliyunpan config set -transfer_url_type ${ALIYUNPAN_TRANSFER_URL_TYPE} -./aliyunpan sync start -dp ${ALIYUNPAN_DOWNLOAD_PARALLEL} -up ${ALIYUNPAN_UPLOAD_PARALLEL} -dbs ${ALIYUNPAN_DOWNLOAD_BLOCK_SIZE} -ubs ${ALIYUNPAN_UPLOAD_BLOCK_SIZE} +./aliyunpan sync start -dp ${ALIYUNPAN_DOWNLOAD_PARALLEL} -up ${ALIYUNPAN_UPLOAD_PARALLEL} -dbs ${ALIYUNPAN_DOWNLOAD_BLOCK_SIZE} -ubs ${ALIYUNPAN_UPLOAD_BLOCK_SIZE} -log ${ALIYUNPAN_SYNC_LOG} diff --git a/docker/sync/docker-compose.yml b/docker/sync/docker-compose.yml index 3aa10f5..95f9092 100644 --- a/docker/sync/docker-compose.yml +++ b/docker/sync/docker-compose.yml @@ -31,5 +31,6 @@ services: - ALIYUNPAN_SYNC_MODE=upload # 优先级,只对双向同步备份模式有效。选项支持三种: time-时间优先,local-本地优先,pan-网盘优先 - ALIYUNPAN_SYNC_PRIORITY=time - + # 是否显示文件备份过程日志,true-显示,false-不显示 + - ALIYUNPAN_SYNC_LOG=true diff --git a/internal/command/sync.go b/internal/command/sync.go index 9ff1896..2630474 100644 --- a/internal/command/sync.go +++ b/internal/command/sync.go @@ -94,8 +94,8 @@ priority - 优先级,只对双向同步备份模式有效。选项支持三种 aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "download" 4. 使用命令行配置启动同步备份服务,将云盘目录 /sync_drive/我的文档 和本地目录 D:\tickstep\Documents\设计文档 的文件进行双向同步 - 同时配置同步优先选项为本地文件优先 - aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "sync" -pri "local" + 同时配置同步优先选项为本地文件优先,并显示同步过程的日志 + aliyunpan sync start -ldir "D:\tickstep\Documents\设计文档" -pdir "/sync_drive/我的文档" -mode "sync" -pri "local" -log true 5. 使用命令行配置启动同步备份服务,将本地目录 D:\tickstep\Documents\设计文档 中的文件备份到云盘目录 /sync_drive/我的文档 同时配置下载并发为2,上传并发为1,下载分片大小为256KB,上传分片大小为1MB @@ -115,6 +115,12 @@ priority - 优先级,只对双向同步备份模式有效。选项支持三种 } activeUser := GetActiveUser() + if c.String("log") == "true" { + syncdrive.LogPrompt = true + } else { + syncdrive.LogPrompt = false + } + dp := c.Int("dp") if dp == 0 { dp = config.Config.MaxDownloadParallel @@ -242,6 +248,11 @@ priority - 优先级,只对双向同步备份模式有效。选项支持三种 Usage: "upload block size,上传分片大小,单位KB。推荐值:1024 ~ 10240", Value: 10240, }, + cli.StringFlag{ + Name: "log", + Usage: "是否显示文件备份过程日志,true-显示,false-不显示", + Value: "false", + }, }, }, }, @@ -319,7 +330,7 @@ func RunSync(defaultTask *syncdrive.SyncTask, fileDownloadParallel, fileUploadPa } else { // in cmd mode c := "" - fmt.Print("本命令不会退出,如需要结束同步备份进程请输入y,然后按Enter键进行停止:") + fmt.Println("本命令不会退出,如需要结束同步备份进程请输入y,然后按Enter键进行停止。") for strings.ToLower(c) != "y" { fmt.Scan(&c) } diff --git a/internal/syncdrive/file_action_task.go b/internal/syncdrive/file_action_task.go index 0bf7e9f..1f3b09c 100644 --- a/internal/syncdrive/file_action_task.go +++ b/internal/syncdrive/file_action_task.go @@ -41,6 +41,12 @@ type ( } ) +func (f *FileActionTask) prompt(msg string) { + if LogPrompt { + fmt.Println("[" + utils.NowTimeStr() + "] " + msg) + } +} + func (f *FileActionTask) HashCode() string { postfix := "" if f.syncItem.Action == SyncFileActionDownload { @@ -54,6 +60,7 @@ func (f *FileActionTask) HashCode() string { func (f *FileActionTask) DoAction(ctx context.Context) error { logger.Verboseln("file action task:", utils.ObjectToJsonStr(f.syncItem, false)) if f.syncItem.Action == SyncFileActionUpload { + f.prompt("上传文件:" + f.syncItem.getLocalFileFullPath()) if e := f.uploadFile(ctx); e != nil { // TODO: retry / cleanup downloading file return e @@ -79,6 +86,7 @@ func (f *FileActionTask) DoAction(ctx context.Context) error { } if f.syncItem.Action == SyncFileActionDownload { + f.prompt("下载文件:" + f.syncItem.getPanFileFullPath()) if e := f.downloadFile(ctx); e != nil { // TODO: retry / cleanup downloading file return e @@ -121,6 +129,7 @@ func (f *FileActionTask) DoAction(ctx context.Context) error { } if f.syncItem.Action == SyncFileActionDeleteLocal { + f.prompt("删除本地文件:" + f.syncItem.getLocalFileFullPath()) if e := f.deleteLocalFile(ctx); e != nil { // TODO: retry return e @@ -132,6 +141,7 @@ func (f *FileActionTask) DoAction(ctx context.Context) error { } if f.syncItem.Action == SyncFileActionDeletePan { + f.prompt("删除云盘文件:" + f.syncItem.getPanFileFullPath()) if e := f.deletePanFile(ctx); e != nil { // TODO: retry return e @@ -143,6 +153,7 @@ func (f *FileActionTask) DoAction(ctx context.Context) error { } if f.syncItem.Action == SyncFileActionCreateLocalFolder { + f.prompt("创建本地文件夹:" + f.syncItem.getLocalFileFullPath()) if e := f.createLocalFolder(ctx); e != nil { // TODO: retry return e @@ -165,6 +176,7 @@ func (f *FileActionTask) DoAction(ctx context.Context) error { } if f.syncItem.Action == SyncFileActionCreatePanFolder { + f.prompt("创建云盘文件夹:" + f.syncItem.getPanFileFullPath()) if e := f.createPanFolder(ctx); e != nil { // TODO: retry return e diff --git a/internal/syncdrive/sync_constants.go b/internal/syncdrive/sync_constants.go index 8b85290..0f1a917 100644 --- a/internal/syncdrive/sync_constants.go +++ b/internal/syncdrive/sync_constants.go @@ -25,3 +25,7 @@ const ( // TimeSecondsOf60Minute 60分钟秒数 TimeSecondsOf60Minute int64 = 60 * TimeSecondsOfOneMinute ) + +var ( + LogPrompt = false +)