-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
114 lines (91 loc) · 2.07 KB
/
google.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
110
111
112
113
114
package commands
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"time"
"google.golang.org/api/drive/v3"
"google.golang.org/api/sheets/v4"
)
type revision struct {
FileID string `json:"file-id"`
ID string `json:"id"`
Modified time.Time `json:"modified"`
}
func (r *revision) load(file string) error {
bytes, err := os.ReadFile(file)
if err != nil {
return err
}
object := revision{}
if err := json.Unmarshal(bytes, &object); err != nil {
return err
}
if r != nil {
r.FileID = object.FileID
r.ID = object.ID
r.Modified = object.Modified
}
return nil
}
func (r *revision) store(file string) error {
dir := filepath.Dir(file)
if err := os.MkdirAll(dir, 0770); err != nil {
return err
}
if bytes, err := json.Marshal(r); err != nil {
return err
} else if err := os.WriteFile(file, bytes, 0660); err != nil {
return err
}
return nil
}
func (r *revision) sameAs(v *revision) bool {
return reflect.DeepEqual(r, v)
}
func getRevision(gdrive *drive.Service, fileId string) (*revision, error) {
page := ""
latest := revision{
FileID: fileId,
ID: "",
Modified: time.Time{},
}
for {
call := drive.NewRevisionsService(gdrive).List(fileId)
if page != "" {
call.PageToken(page)
}
revisions, err := call.Do()
if err != nil {
return nil, err
}
for _, r := range revisions.Revisions {
datetime, err := time.Parse("2006-01-02T15:04:05.999Z", r.ModifiedTime)
if err != nil {
return nil, err
}
if latest.Modified.Before(datetime) {
latest.ID = r.Id
latest.Modified = datetime
}
}
if page = revisions.NextPageToken; page == "" {
break
}
}
if latest.Modified.IsZero() {
return nil, fmt.Errorf("unable to identify latest revision for file ID %s", fileId)
}
return &latest, nil
}
func clear(google *sheets.Service, spreadsheet *sheets.Spreadsheet, ranges []string) error {
rq := sheets.BatchClearValuesRequest{
Ranges: ranges,
}
if _, err := google.Spreadsheets.Values.BatchClear(spreadsheet.SpreadsheetId, &rq).Do(); err != nil {
return err
}
return nil
}