-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
162 lines (126 loc) · 3.9 KB
/
get.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package commands
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
lib "github.com/uhppoted/uhppoted-lib/os"
)
var GetCmd = Get{
command: command{
workdir: DEFAULT_WORKDIR,
credentials: DEFAULT_CREDENTIALS,
tokens: "",
url: "",
debug: false,
},
area: "",
file: time.Now().Format("2006-01-02T150405.tsv"),
}
type Get struct {
command
area string
file string
withPIN bool
}
func (cmd *Get) Name() string {
return "get"
}
func (cmd *Get) Description() string {
return "Retrieves an access control list from a Google Sheets worksheet and stores it to a local file"
}
func (cmd *Get) Usage() string {
return "--credentials <file> --url <url> --file <file>"
}
func (cmd *Get) Help() {
fmt.Println()
fmt.Printf(" Usage: %s [--debug] get [options] --url <URL> --range <range> --file <file>\n", APP)
fmt.Println()
fmt.Println(" Downloads a Google Sheets worksheet to a TSV file")
fmt.Println()
helpOptions(cmd.FlagSet())
fmt.Println()
fmt.Println(" Examples:")
fmt.Println(` uhppote-app-sheets --debug get --credentials "credentials.json" \`)
fmt.Println(` --url "https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms" \`)
fmt.Println(` --range "ACL!A2:E" \`)
fmt.Println(` --file "example.tsv"`)
fmt.Println()
}
func (cmd *Get) FlagSet() *flag.FlagSet {
flagset := cmd.flagset("get")
flagset.StringVar(&cmd.area, "range", cmd.area, "Spreadsheet range e.g. 'ACL!A2:E'")
flagset.StringVar(&cmd.file, "file", cmd.file, "TSV file name. Defaults to 'ACL - <yyyy-mm-dd HHmmss>.tsv'")
flagset.BoolVar(&cmd.withPIN, "with-pin", cmd.withPIN, "Includes the card keypad PIN codes in the retrieved ACL file")
return flagset
}
func (cmd *Get) Execute(args ...interface{}) error {
options := args[0].(*Options)
cmd.debug = options.Debug
// ... check parameters
if strings.TrimSpace(cmd.credentials) == "" {
return fmt.Errorf("--credentials is a required option")
}
if strings.TrimSpace(cmd.url) == "" {
return fmt.Errorf("--url is a required option")
}
if strings.TrimSpace(cmd.area) == "" {
return fmt.Errorf("--range is a required option")
}
match := regexp.MustCompile(`^https://docs.google.com/spreadsheets/d/(.*?)(?:/.*)?$`).FindStringSubmatch(cmd.url)
if len(match) < 2 {
return fmt.Errorf("invalid spreadsheet URL - expected something like 'https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'")
}
spreadsheet := match[1]
area := cmd.area
if cmd.debug {
debugf("Spreadsheet - ID:%s range:%s", spreadsheet, area)
}
// ... authorise
tokens := cmd.tokens
if tokens == "" {
tokens = filepath.Join(cmd.workdir, ".google")
}
client, err := authorize(cmd.credentials, SHEETS, tokens)
if err != nil {
return fmt.Errorf("authentication/authorization error (%v)", err)
}
google, err := sheets.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to create new Sheets client (%v)", err)
}
response, err := google.Spreadsheets.Values.Get(spreadsheet, area).Do()
if err != nil {
return fmt.Errorf("unable to retrieve data from sheet (%v)", err)
}
if len(response.Values) == 0 {
return fmt.Errorf("no data in spreadsheet/range")
}
tmp, err := os.CreateTemp(os.TempDir(), "ACL")
if err != nil {
return err
}
defer func() {
tmp.Close()
os.Remove(tmp.Name())
}()
if err := sheetToTSV(tmp, response, cmd.withPIN); err != nil {
return fmt.Errorf("error creating TSV file (%v)", err)
}
tmp.Close()
dir := filepath.Dir(cmd.file)
if err := os.MkdirAll(dir, 0770); err != nil {
return err
}
if err := lib.Rename(tmp.Name(), cmd.file); err != nil {
return err
}
infof("Retrieved ACL to file %s", cmd.file)
return nil
}