aboutsummaryrefslogtreecommitdiffstats
path: root/drive.go
blob: 33d64b375b0284ee366ea7c89a90cb75b0f2c379 (plain)
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
package main

import (
    "fmt"
    "os"
    "github.com/voxelbrain/goptions"
    "./gdrive"
    "./util"
    "./cli"
)

const (
    VersionNumber = "1.0.0"
)

type Options struct {
    Advanced bool `goptions:"-a, --advanced, description='Advanced Mode -- lets you specify your own oauth client id and secret on setup'"`
    AppPath string `goptions:"-c, --config, description='Set application path where config and token is stored. Defaults to ~/.gdrive'"`
    Version bool `goptions:"-v, --version, description='Print version'"`
    goptions.Help `goptions:"-h, --help, description='Show this help'"`

    goptions.Verbs

    List struct {
        MaxResults int `goptions:"-m, --max, description='Max results'"`
        TitleFilter string `goptions:"-t, --title, mutexgroup='query', description='Title filter'"`
        Query string `goptions:"-q, --query, mutexgroup='query', description='Query (see https://developers.google.com/drive/search-parameters)'"`
        SharedStatus bool `goptions:"-s, --shared, description='Show shared status (Note: this will generate 1 http req per file)'"`
    } `goptions:"list"`

    Info struct {
        FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
    } `goptions:"info"`

    Upload struct {
        File *os.File `goptions:"-f, --file, mutexgroup='input', obligatory, rdonly, description='File to upload'"`
        Stdin bool `goptions:"-s, --stdin, mutexgroup='input', obligatory, description='Use stdin as file content'"`
        Title string `goptions:"-t, --title, description='Title to give uploaded file. Defaults to filename'"`
        Share bool `goptions:"--share, description='Share uploaded file'"`
    } `goptions:"upload"`

    Download struct {
        FileId string `goptions:"-i, --id, mutexgroup='download', obligatory, description='File Id'"`
        Stdout bool `goptions:"-s, --stdout, description='Write file content to stdout'"`
        Pop bool `goptions:"--pop, mutexgroup='download', description='Download latest file, and remove it from google drive'"`
    } `goptions:"download"`

    Delete struct {
        FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
    } `goptions:"delete"`

    Share struct {
        FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
    } `goptions:"share"`

    Unshare struct {
        FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
    } `goptions:"unshare"`

    Url struct {
        FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
        Preview bool `goptions:"-p, --preview, mutexgroup='urltype', description='Generate preview url (default)'"`
        Download bool `goptions:"-d, --download, mutexgroup='urltype', description='Generate download url'"`
    } `goptions:"url"`
}

func main() {
    opts := &Options{}
    goptions.ParseAndFail(opts)

    // Print version number and exit if the version flag is set
    if opts.Version {
        fmt.Printf("gdrive v%s\n", VersionNumber)
        return
    }

    // Get authorized drive client
    drive, err := gdrive.New(opts.AppPath, opts.Advanced)
    if err != nil {
        fmt.Printf("An error occurred creating Drive client: %v\n", err)
        return
    }

    switch opts.Verbs {
        case "list":
            args := opts.List
            cli.List(drive, args.Query, args.TitleFilter, args.MaxResults, args.SharedStatus)

        case "info":
            cli.Info(drive, opts.Info.FileId)

        case "upload":
            args := opts.Upload
            if args.Stdin {
                cli.Upload(drive, os.Stdin, args.Title, args.Share)
            } else {
                cli.Upload(drive, args.File, args.Title, args.Share)
            }

        case "download":
            args := opts.Download
            if args.Pop {
                cli.DownloadLatest(drive, args.Stdout)
            } else {
                cli.Download(drive, args.FileId, args.Stdout, false)
            }

        case "delete":
            cli.Delete(drive, opts.Delete.FileId)

        case "share":
            cli.Share(drive, opts.Share.FileId)

        case "unshare":
            cli.Unshare(drive, opts.Unshare.FileId)

        case "url":
            if opts.Url.Download {
                fmt.Println(util.DownloadUrl(opts.Url.FileId))
            } else {
                fmt.Println(util.PreviewUrl(opts.Url.FileId))
            }

        default:
            goptions.PrintHelp()
    }
}