-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (71 loc) · 1.7 KB
/
main.go
File metadata and controls
87 lines (71 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"flag"
"fmt"
dsync "github.com/bondhan/sync/modules"
"github.com/bondhan/sync/modules/errors"
"os"
"os/signal"
"syscall"
)
func checkErr(err error) {
if err != nil {
fmt.Println("Err:", err)
os.Exit(1)
}
}
// isDir will check if path is directory, if
// not will return false and error
func isDir(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, err
}
defer func(f *os.File) {
err = f.Close()
if err != nil {
fmt.Println(err)
}
}(file)
fileInfo, err := file.Stat()
if err != nil {
return false, err
}
if !fileInfo.IsDir() {
return false, dsyncerr.ErrNotDirectory
}
return true, nil
}
func main() {
var src, dest string
var isVerbose, createEmptyFolder bool
flag.StringVar(&src, "s", "", "source folder")
flag.StringVar(&dest, "d", "", "destination folder")
flag.BoolVar(&isVerbose, "v", false, "verbose")
flag.BoolVar(&createEmptyFolder, "e", false, "create empty folder")
flag.Parse()
if dest == "" || src == "" {
fmt.Println("Usage: sync [-ds], where:")
flag.PrintDefaults()
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err := isDir(src)
checkErr(err)
_, err = isDir(dest)
checkErr(err)
ds, err := dsync.New(ctx, src, dest, dsync.WithVerbose(isVerbose), dsync.WithCreateEmptyFolder(createEmptyFolder))
checkErr(err)
// Setting up a channel to capture system signals
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
go func() {
<-stop
cancel()
}()
err = ds.DoSync(ctx)
checkErr(err)
fmt.Println("Total files processed:", ds.GetTotal())
}