forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdrive.go
53 lines (44 loc) · 1.25 KB
/
gdrive.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
package gdrive
import (
"code.google.com/p/google-api-go-client/drive/v2"
"github.com/prasmussen/gdrive/auth"
"github.com/prasmussen/gdrive/config"
"github.com/prasmussen/gdrive/util"
"net/http"
"path/filepath"
)
// File paths and names
var (
AppPath = filepath.Join(util.Homedir(), ".gdrive")
ConfigFname = "config.json"
TokenFname = "token.json"
//ConfigPath = filepath.Join(ConfigDir, "config.json")
//TokenPath = filepath.Join(ConfigDir, "token.json")
)
type Drive struct {
*drive.Service
client *http.Client
}
// Returns the raw http client which has the oauth transport
func (self *Drive) Client() *http.Client {
return self.client
}
func New(customAppPath string, advancedMode bool, promptUser bool) (*Drive, error) {
if customAppPath != "" {
AppPath = customAppPath
}
// Build paths to config files
configPath := filepath.Join(AppPath, ConfigFname)
tokenPath := filepath.Join(AppPath, TokenFname)
config := config.Load(configPath, advancedMode)
client, err := auth.GetOauth2Client(config.ClientId, config.ClientSecret, tokenPath, promptUser)
if err != nil {
return nil, err
}
drive, err := drive.New(client)
if err != nil {
return nil, err
}
// Return a new authorized Drive client.
return &Drive{drive, client}, nil
}