This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
forked from weavenet/gosync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (73 loc) · 2.31 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
89
package main
import (
"fmt"
"os"
"github.com/rotundasoftware/gosync/gosync"
"github.com/rotundasoftware/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{Name: "concurrent, c", Value: 20, Usage: "number of concurrent transfers"},
cli.StringFlag{Name: "log-level, l", Value: "info", Usage: "log level"},
cli.StringFlag{Name: "aws-access-key-id", Value: "", Usage: "AWS Access Key Id"},
cli.StringFlag{Name: "aws-access-key-secret", Value: "", Usage: "AWS Access Key Secret"},
cli.StringFlag{Name: "aws-security-token", Value: "", Usage: "AWS Security Token"},
cli.StringFlag{Name: "aws-region", Value: "", Usage: "AWS Region"},
cli.StringFlag{Name: "aws-acl", Value: "private", Usage: "AWS ACL"},
}
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-access-key-secret")
token := c.String("aws-security-token")
region := c.String("aws-region")
acl := c.String("aws-acl")
concurrent := c.Int("concurrent")
auth, err := aws.GetAuth(key, secret)
exitOnError(err)
if token != "" {
auth.Token = token
}
source := c.Args()[0]
target := c.Args()[1]
log.Infof("Setting source to '%s'.", source)
log.Infof("Setting target to '%s'.", target)
log.Infof("Setting concurrent transfers to '%d'.", concurrent)
syncPair := gosync.NewSyncPair(auth, source, target, region, acl, 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)
}