Compare commits

...

3 Commits

Author SHA1 Message Date
bee321c4e8 修复推送远端时会在本地创建文件夹的问题 2024-10-09 17:05:26 +08:00
梁 致源
528cbaa735 更新 'README.md' 2021-07-12 10:27:37 +00:00
liangzy
9f969bb80f refresh
refresh
2020-04-14 03:12:27 +00:00
3 changed files with 56 additions and 4 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@
*.iml
out
gen
*_test.go

View File

@ -1,2 +1,40 @@
# grsync
```go
package main
import (
"fmt"
"grsync"
"time"
)
func main() {
task := grsync.NewTask(
"username@server.com:/source/folder",
"/home/user/destination",
grsync.RsyncOptions{},
)
go func() {
for {
state := task.State()
fmt.Printf(
"progress: %.2f / rem. %d / tot. %d / sp. %s \n",
state.Progress,
state.Remain,
state.Total,
state.Speed,
)
time.Sleep(time.Second)
}
}()
if err := task.Run(); err != nil {
panic(err)
}
fmt.Println("well done")
fmt.Println(task.Log())
}
```

View File

@ -21,17 +21,17 @@ type Rsync struct {
type RsyncOptions struct {
// Verbose increase verbosity
Verbose bool
// Quet suppress non-error messages
// Quiet suppress non-error messages
Quiet bool
// Checksum skip based on checksum, not mod-time & size
Checksum bool
// Archve is archive mode; equals -rlptgoD (no -H,-A,-X)
// Archive is archive mode; equals -rlptgoD (no -H,-A,-X)
Archive bool
// Recurse into directories
Recursive bool
// Relative option to use relative path names
Relative bool
// NoImliedDirs don't send implied dirs with --relative
// NoImpliedDirs don't send implied dirs with --relative
NoImpliedDirs bool
// Update skip files that are newer on the receiver
Update bool
@ -196,7 +196,8 @@ func (r Rsync) StderrPipe() (io.ReadCloser, error) {
// Run start rsync task
func (r Rsync) Run() error {
if !isExist(r.Destination) {
// 目标地址为 远程地址 则不进行文件夹创建
if !isRsyncPath(r.Destination) && !isExist(r.Destination) {
if err := createDir(r.Destination); err != nil {
return err
}
@ -552,3 +553,15 @@ func isExist(p string) bool {
stat, err := os.Stat(p)
return os.IsExist(err) && stat.IsDir()
}
// isRsyncPath 判断一个给定的字符串或路径是否格式是否符合rsync协议的要求
func isRsyncPath(path string) bool {
if strings.HasPrefix(path, "rsync://") {
return true
}
parts := strings.SplitN(path, ":", 2)
if len(parts) == 2 && strings.Contains(parts[0], "@") {
return true
}
return false
}