-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
100 lines (75 loc) · 2.71 KB
/
option.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
//
//
//
package main
import (
"fmt"
"os"
"strings"
flags "github.com/jessevdk/go-flags"
)
type Options struct {
SrcDir string `short:"s" long:"source-dir" description:"source directory"`
DstDir string `short:"d" long:"destination-dir" description:"destination directory"`
Route []string `short:"r" long:"route" description:"src_host:dst_host" default:"localhost:localhost"`
Parallel int `short:"p" long:"parallel" default:"1" description:"number of rsync processes per route"`
//
MkdirCommand string `short:"m" long:"mkdir-command" description:"mkdir command" default:"mkdir"`
MkdirOptions []string `short:"n" long:"mkdir-option" description:"mkdir options" default:"-p"`
SshCommand string `short:"a" long:"ssh-command" description:"ssh command" default:"ssh"`
SshOptions []string `short:"b" long:"ssh-option" description:"ssh options" default:"-qtt"`
// SshOption []string `short:"b" long:"ssh-option" description:"ssh options" default:"{\"-l\", \"mana\"}"`
RsyncCommand string `short:"x" long:"rsync-command" description:"rsync command" default:"rsync"`
RsyncOptions []string `short:"y" long:"rsync-option" description:"rsync options" default:"-dglopstADHX" default:"--delete"`
//
LogFile string `short:"o" long:"log-file" description:"write logs to file"`
LogLevel int `short:"O" long:"log-level" description:"[-1|0|1|2|3|4|5|6|7] log level" default:"5"`
Verbose bool `short:"v" long:"verbose" description:"same as --log-level=6"`
Quiet bool `short:"q" long:"quiet" description:"same as --log-level=-1"`
//
// IgnoreError bool `long:"ignore-error" description:"continue as much as possible"`
ExitAtWarn bool `short:"w" long:"exit-at-warn" description:"treat continuable warning as error"`
//
Version bool `long:"version" description:"print version"`
//
StdIn bool `no-flag:"true" default:"false"`
StdOut bool `no-flag:"true" default:"false"`
}
var oP Options
var version string
//
func parseFlags() {
parser := flags.NewParser(&oP, flags.Default)
parser.Name = "csync"
parser.Usage = `[OPTIONS...] -s src_dir -d dst_dir`
_, err := parser.Parse()
if err != nil {
// fmt.Fprint(os.Stderr, "csync: %s\n", err)
os.Exit(1)
}
if oP.Version {
fmt.Fprintf(os.Stderr, "csync: %s\n", version)
os.Exit(0)
}
//
if oP.Verbose {
oP.LogLevel = 6
}
if oP.Quiet {
oP.LogLevel = -1
}
if len(oP.SrcDir) == 0 {
fmt.Fprintf(os.Stderr, "csync: source directory not sprcified\n")
os.Exit(1)
}
if len(oP.DstDir) == 0 {
fmt.Fprintf(os.Stderr, "csync: destination directory not sprcified\n")
os.Exit(1)
}
for _, v := range oP.Route {
if !strings.Contains(v, ":") {
fmt.Fprintf(os.Stderr, "csync: '%s' does not contain separater mark ':'\n", v)
os.Exit(1)
}
}
}