-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (78 loc) · 2.23 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/korylprince/drive-archive/drive"
)
func run(auth, user, root, out string, downloadOrphans bool) error {
svc, err := drive.NewService(auth, user, time.Second, 8)
if err != nil {
return fmt.Errorf("could not create service: %w", err)
}
if root == "" {
root, err = svc.Root()
if err != nil {
return fmt.Errorf("could not get root id: %w", err)
}
}
files, err := svc.List()
if err != nil {
return fmt.Errorf("could not list files: %w", err)
}
fmt.Println("found", len(files), "total files")
rootTree, orphans := drive.NewTree(root, files)
if err = svc.DownloadTree(rootTree, out, 0); err != nil {
return fmt.Errorf("could not finish downloading \"My Drive\" files: %w", err)
}
if downloadOrphans {
if err = svc.DownloadTree(orphans, out, 0); err != nil {
return fmt.Errorf("could not finish downloading Shared files: %w", err)
}
}
fmt.Println("done!")
return nil
}
func main() {
flAuthJSON := flag.String("authfile", "", "path to service account json file")
flUser := flag.String("user", "", "email of user to download Google Drive files for")
flRoot := flag.String("root", "", "the id of the folder to download. Leave empty to download entire Drive")
flOrphans := flag.Bool("orphans", false, "download orphaned files. These are usually Shared Files")
flOut := flag.String("out", "", "path to output files to. Will be created if it doesn't already exist")
flHelp := flag.Bool("help", false, "display this help information")
flag.Parse()
if *flHelp {
flag.Usage()
os.Exit(0)
}
if *flAuthJSON == "" {
flag.Usage()
fmt.Println("\n-authfile must be set")
os.Exit(-1)
}
if *flUser == "" {
flag.Usage()
fmt.Println("\n-user must be set")
os.Exit(-1)
}
if *flOut == "" {
flag.Usage()
fmt.Println("\n-out must be set")
os.Exit(-1)
}
if *flRoot != "" && *flOrphans {
flag.Usage()
fmt.Println("\n-orphans cannot be used when -root is set")
os.Exit(-1)
}
if err := os.MkdirAll(*flOut, 0755); err != nil {
fmt.Println("could not create output directory:", err)
os.Exit(-1)
}
err := run(*flAuthJSON, *flUser, *flRoot, *flOut, *flOrphans)
if err != nil {
fmt.Println("could not download files:", err)
os.Exit(-1)
}
}