-
Notifications
You must be signed in to change notification settings - Fork 1
/
drivefs.go
109 lines (101 loc) · 3.1 KB
/
drivefs.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
101
102
103
104
105
106
107
108
109
/*
Copyright (c) 2014 Eliot Hedeman
Copyright (C) 2014 S2 Games
All rights reserved.
*/
package main
import (
"bazil.org/fuse/fs/fstestutil"
"code.google.com/p/goauth2/oauth"
drive "code.google.com/p/google-api-go-client/drive/v2"
"flag"
"github.com/S2Games/drivefs/lib"
"log"
"os"
"os/signal"
"syscall"
)
// Collect command line arguments for OAUTH and mounting options
var (
// OAUTH options
clientId = flag.String("id", "266884823682-qfd1cvgpe2vucokeuhv0qr0bihn87ge4.apps.googleusercontent.com", "Client ID")
clientSecret = flag.String("secret", "6SGgwuVL8KMyFsigaPn2MGBK", "Client Secret")
scope = flag.String("scope", "https://www.googleapis.com/auth/drive", "OAuth scope")
redirectURL = flag.String("redirect_url", "oob", "Redirect URL")
authURL = flag.String("auth_url", "https://accounts.google.com/o/oauth2/auth", "Authentication URL")
tokenURL = flag.String("token_url", "https://accounts.google.com/o/oauth2/token", "Token URL")
requestURL = flag.String("request_url", "https://www.googleapis.com/oauth2/v1/userinfo", "API request")
code = flag.String("code", "", "Authorization Code")
cachefile = flag.String("cache", "cache.json", "Token cache file")
getCode = flag.Bool("getCode", false, "if getCode is supplied, a url will be returned to get your auth code at.")
// Filesystem options
mountpoint = flag.String("mount", "", "Mount point for drivefs")
refresh = flag.Int("refresh", 10, "Rate at which to refresh if local file system has not changed.")
// Debugging options
m fstestutil.Mount
)
// Exists checks if a file or directory exists on disk
func Exists(fileName string) bool {
if _, err := os.Stat(fileName); os.IsNotExist(err) {
return false
} else {
return true
}
}
func main() {
flag.Parse()
// if the cache file does not exists, create it
if !Exists(*cachefile) {
f, err := os.Create(*cachefile)
if err == nil {
f.Close()
} else {
log.Fatal("CacheFile: ", err)
}
}
// Set up a configuration.
config := &oauth.Config{
ClientId: *clientId,
ClientSecret: *clientSecret,
RedirectURL: *redirectURL,
Scope: drive.DriveScope,
AuthURL: *authURL,
TokenURL: *tokenURL,
TokenCache: oauth.CacheFile(*cachefile),
}
if *getCode {
lib.GetCodeURL(config)
}
// if the mountpoint given does not exist, make it
if *mountpoint == "" {
log.Fatal("Must provide mountpoint via argument -mount")
}
if !Exists(*mountpoint) {
log.Println(*mountpoint)
err := os.Mkdir(*mountpoint, 0777)
if err != nil {
log.Fatal("Mountpoint: ", err)
}
}
// fail if the server can't auth
server, err := lib.NewServer(config, *code)
if err != nil {
log.Fatal(err)
}
// Attempt to mount the filesystem, fail if mountpoint is not given
err = server.Mount(*mountpoint)
if err != nil {
log.Fatalln(err)
}
// Start the server
go server.Serve(*refresh)
// wait for a termination before exit
killChan := make(chan os.Signal)
signal.Notify(killChan, os.Interrupt)
signal.Notify(killChan, syscall.SIGTERM)
for sig := range killChan {
log.Println("drivefs: stopping due to ", sig)
break
}
server.Unmount(*mountpoint, 3)
}