-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
100 lines (97 loc) · 2.25 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
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"github.com/danmarg/outtake/lib"
"github.com/danmarg/outtake/lib/gmail"
"github.com/urfave/cli/v2"
"os"
"time"
)
const (
progressUpdateFreqSecs = 2.0
)
func main() {
app := &cli.App{
Name: "outtake",
Usage: "Export Gmail to Maildir...efficiently!",
Version: "0.0.1",
Authors: []*cli.Author{&cli.Author{
Name: "Daniel Margolis", Email: "[email protected]"}},
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "directory",
Usage: "Maildir to output to.",
},
&cli.BoolFlag{
Name: "full",
Usage: "Force a full sync",
},
&cli.StringFlag{
Name: "to-impersonate",
Usage: "The domain user that must be impersonated.",
},
&cli.StringFlag{
Name: "service-account-json-file",
Usage: "The JWT service account JSON file to use for authentication.",
},
&cli.StringFlag{
Name: "label",
Usage: "Label to sync",
},
&cli.IntFlag{
Name: "buffer",
Usage: "Download buffer size",
Value: 128,
},
&cli.IntFlag{
Name: "parallel",
Usage: "Max parallel downloads",
Value: 8,
},
}
app.Action = func(ctx *cli.Context) error {
d := ctx.String("directory")
if d == "" {
return fmt.Errorf("Missing --directory flag")
}
if s, err := os.Stat(d); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(d, 0766); err != nil {
return err
}
} else if err != nil {
return err
} else if !s.IsDir() {
return fmt.Errorf("Error: %v exists and is not a directory\n", d)
}
g, err := gmail.NewGmail(d, ctx.String("label"), ctx.String("service-account-json-file"), ctx.String("to-impersonate"))
if err != nil {
return err
}
gmail.MessageBufferSize = ctx.Int("buffer")
gmail.ConcurrentDownloads = ctx.Int("parallel")
if err != nil {
return err
}
progress := make(chan lib.Progress)
go func() {
l := time.Time{}
for p := range progress {
if time.Since(l).Seconds() > progressUpdateFreqSecs {
l = time.Now()
fmt.Printf("\r%d / %d %.2f%% ", p.Current, p.Total, float32(p.Current)/float32(p.Total)*100)
}
}
fmt.Println()
}()
if err := g.Sync(ctx.Bool("full"), progress); err != nil {
fmt.Println(err)
os.Exit(-1)
}
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}