forked from weavenet/gosync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (70 loc) · 2.04 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/brettweavnet/gosync/gosync"
"github.com/brettweavnet/gosync/version"
log "github.com/cihub/seelog"
"github.com/codegangsta/cli"
"github.com/mitchellh/goamz/aws"
)
func main() {
app := cli.NewApp()
app.Name = "gosync"
app.Usage = "gosync OPTIONS SOURCE TARGET"
app.Version = version.Version()
app.Flags = []cli.Flag{
cli.IntFlag{"concurrent, c", 20, "number of concurrent transfers", ""},
cli.StringFlag{"log-level, l", "info", "log level", ""},
cli.StringFlag{"aws-secret-access-key", "", "AWS Secret Access Key", ""},
cli.StringFlag{"aws-access-key-id", "", "AWS Access Key Id", ""},
cli.StringFlag{"aws-security-token", "", "AWS Security Token", ""},
}
const concurrent = 20
app.Action = func(c *cli.Context) {
defer log.Flush()
setLogLevel(c.String("log-level"))
err := validateArgs(c)
exitOnError(err)
key := c.String("aws-access-key-id")
secret := c.String("aws-secret-access-key")
token := c.String("aws-security-token")
auth, err := aws.GetAuth(key, secret)
exitOnError(err)
if token != "" {
auth.Token = token
}
source := c.Args()[0]
log.Infof("Setting source to '%s'.", source)
target := c.Args()[1]
log.Infof("Setting target to '%s'.", target)
syncPair := gosync.NewSyncPair(auth, source, target)
syncPair.Concurrent = c.Int("concurrent")
log.Infof("Setting concurrent transfers to '%d'.", syncPair.Concurrent)
err = syncPair.Sync()
exitOnError(err)
log.Infof("Syncing completed successfully.")
}
app.Run(os.Args)
}
func validateArgs(c *cli.Context) error {
if len(c.Args()) != 2 {
return fmt.Errorf("Source and target required.")
}
return nil
}
func exitOnError(e error) {
if e != nil {
log.Errorf("Received error '%s'", e.Error())
log.Flush()
os.Exit(1)
}
}
func setLogLevel(level string) {
if level != "error" && level != "warn" {
log.Infof("Setting log level '%s'.", level)
}
logConfig := fmt.Sprintf("<seelog minlevel='%s'>", level)
logger, _ := log.LoggerFromConfigAsBytes([]byte(logConfig))
log.ReplaceLogger(logger)
}