| 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
 | package main
import (
	"fmt"
	"os"
	"strings"
    "./cli"
	"./client"
	"./drive"
)
const ClientId     = "367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com"
const ClientSecret = "1qsNodXNaWq1mQuBjUjmvhoO"
const TokenFilename = "token_v2.json"
func listHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).List(drive.ListFilesArgs{
        Out: os.Stdout,
        MaxFiles: args.Int64("maxFiles"),
        NameWidth: args.Int64("nameWidth"),
        Query: args.String("query"),
        SkipHeader: args.Bool("skipHeader"),
        SizeInBytes: args.Bool("sizeInBytes"),
    })
    checkErr(err)
}
func downloadHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).Download(drive.DownloadFileArgs{
        Out: os.Stdout,
        Id: args.String("id"),
        Force: args.Bool("force"),
        Stdout: args.Bool("stdout"),
        NoProgress: args.Bool("noprogress"),
    })
    checkErr(err)
}
func uploadHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).Upload(drive.UploadFileArgs{
        Out: os.Stdout,
        Path: args.String("path"),
        Name: args.String("name"),
        Parent: args.String("parent"),
        Mime: args.String("mime"),
        Recursive: args.Bool("recursive"),
        Stdin: args.Bool("stdin"),
        Share: args.Bool("share"),
    })
    checkErr(err)
}
func infoHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).Info(drive.FileInfoArgs{
        Out: os.Stdout,
        Id: args.String("id"),
        SizeInBytes: args.Bool("sizeInBytes"),
    })
    checkErr(err)
}
func mkdirHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).Mkdir(drive.MkdirArgs{
        Out: os.Stdout,
        Name: args.String("name"),
        Parent: args.String("parent"),
        Share: args.Bool("share"),
    })
    checkErr(err)
}
func shareHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).Share(drive.ShareArgs{
        Out: os.Stdout,
        FileId: args.String("id"),
        Role: args.String("role"),
        Type: args.String("type"),
        Email: args.String("email"),
        Discoverable: args.Bool("discoverable"),
        Revoke: args.Bool("revoke"),
    })
    checkErr(err)
}
func urlHandler(ctx cli.Context) {
    args := ctx.Args()
    newDrive(args).Url(drive.UrlArgs{
        Out: os.Stdout,
        FileId: args.String("id"),
        DownloadUrl: args.Bool("download"),
    })
}
func deleteHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).Delete(drive.DeleteArgs{
        Out: os.Stdout,
        Id: args.String("id"),
    })
    checkErr(err)
}
func aboutHandler(ctx cli.Context) {
    args := ctx.Args()
    err := newDrive(args).About(drive.AboutArgs{
        Out: os.Stdout,
        SizeInBytes: args.Bool("sizeInBytes"),
        ImportFormats: args.Bool("importFormats"),
        ExportFormats: args.Bool("exportFormats"),
    })
    checkErr(err)
}
func printVersion(ctx cli.Context) {
    fmt.Printf("%s v%s\n", Name, Version)
}
func printHelp(ctx cli.Context) {
    fmt.Printf("%s usage:\n\n", Name)
    for _, h := range ctx.Handlers() {
        fmt.Printf("%s %s  (%s)\n", Name, h.Pattern, h.Description)
    }
}
func printCommandHelp(ctx cli.Context) {
    handlers := ctx.FilterHandlers(ctx.Args().String("subcommand"))
    if len(handlers) == 0 {
        ExitF("Subcommand not found")
    }
    if len(handlers) > 1 {
        ExitF("More than one matching subcommand, be more specific")
    }
    handler := handlers[0]
    fmt.Printf("%s %s  (%s)\n", Name, handler.Pattern, handler.Description)
    for name, flags := range handler.Flags {
        fmt.Printf("\n%s:\n", name)
        for _, flag := range flags {
            fmt.Printf("  %s  (%s)\n", strings.Join(flag.GetPatterns(), ", "), flag.GetDescription())
        }
    }
}
func newDrive(args cli.Arguments) *drive.Drive {
    configDir := args.String("configDir")
    tokenPath := ConfigFilePath(configDir, TokenFilename)
    oauth, err := client.NewOauthClient(ClientId, ClientSecret, tokenPath, authCodePrompt)
    if err != nil {
        ExitF("Failed getting oauth client: %s", err.Error())
    }
    client, err := client.NewClient(oauth)
    if err != nil {
        ExitF("Failed getting drive: %s", err.Error())
    }
    return drive.NewDrive(client)
}
func authCodePrompt(url string) func() string {
    return func() string {
        fmt.Println("Authentication needed")
        fmt.Println("Go to the following url in your browser:")
        fmt.Printf("%s\n\n", url)
        fmt.Print("Enter verification code: ")
        var code string
        if _, err := fmt.Scan(&code); err != nil {
            fmt.Printf("Failed reading code: %s", err.Error())
        }
        return code
    }
}
 |