-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.go
45 lines (37 loc) · 981 Bytes
/
drive.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
package drive
import (
"log"
"net/http"
"github.com/pkg/errors"
"golang.org/x/net/context"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
// Service google drive API wrapper.
type Service struct {
drive *drive.Service
}
// NewService returns new service instance.
func NewService(client *http.Client) (*Service, error) {
srv, err := drive.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return &Service{}, errors.Wrap(err, "unable to retrieve Drive client")
}
return &Service{
drive: srv,
}, nil
}
// CreateDir creates a directory at google drive.
func (s *Service) CreateDir(name string, parentID string) (*drive.File, error) {
d := &drive.File{
Name: name,
MimeType: "application/vnd.google-apps.folder",
Parents: []string{parentID},
}
file, err := s.drive.Files.Create(d).Do()
if err != nil {
log.Println("Could not create dir: " + err.Error())
return nil, err
}
return file, nil
}