/
main.go
187 lines (163 loc) · 4.15 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"errors"
"os"
"github.com/hink/SquadSheets/pkg/models"
"google.golang.org/api/sheets/v4"
"github.com/apex/log/handlers/text"
"github.com/hink/SquadSheets/internal/pkg/config"
"github.com/apex/log"
"github.com/urfave/cli"
)
// CLIOpts command line options
type CLIOpts struct {
ConfigPath string
LogPath string
Verbose bool
SquadConfigDir string
ASCWhitelist bool
}
var cfg *config.Config
func main() {
app := initApp()
app.Action = func(c *cli.Context) error {
// Parse CLI Args
opts, err := validateArgs(c)
if err != nil {
log.Fatal(err.Error())
}
// configure logging
logLevel := log.InfoLevel
if opts.Verbose {
logLevel = log.DebugLevel
}
log.SetLevel(logLevel)
if opts.LogPath != "" {
logFile, err := os.OpenFile(opts.LogPath, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err.Error())
}
defer logFile.Close()
log.SetHandler(text.New(logFile))
} else {
log.SetHandler(text.New(os.Stderr))
}
// Parse Configuration File
log.WithField("path", opts.ConfigPath).Info("loading configuration")
cfg, err = config.Load(opts.ConfigPath)
if err != nil {
log.Fatal(err.Error())
}
// parse sheets
roles := make(map[string]models.AdminRole)
admins := []models.User{}
whitelist := []models.User{}
for _, s := range cfg.Sheets {
// Initialize Sheets client
log.Info("Initializing Google Sheets client")
sheetsSrv, err := getSheetsService(s.SecretJSONPath)
if err != nil {
log.Fatal(err.Error())
}
// Get sheet properties
sheetProps := make(map[string]*sheets.SheetProperties)
log.WithField("sheetid", s.SheetID).Info("getting Google sheet properties")
props, err := getSheetProperties(sheetsSrv, s.SheetID)
if err != nil {
log.WithFields(log.Fields{
"sheetid": s.SheetID,
"error": err.Error(),
}).Fatal("failed to get Google sheet properties")
}
for _, v := range props {
// roles
if v.Properties.Title == s.SheetAdminRoles {
sheetProps[s.SheetAdminRoles] = v.Properties
continue
}
// whitelist
for _, s := range s.SheetsWhitelist {
if v.Properties.Title == s {
sheetProps[s] = v.Properties
continue
}
}
// Admins
for _, s := range s.SheetsAdmin {
if v.Properties.Title == s {
sheetProps[s] = v.Properties
continue
}
}
}
// Double check for props
// roles
if sheetProps[s.SheetAdminRoles] == nil {
log.WithField("sheet", s.SheetAdminRoles).Fatal("could not find sheet")
}
// Admins
for _, sx := range s.SheetsAdmin {
if sheetProps[sx] == nil {
log.WithField("sheet", sx).Fatal("could not find sheet")
}
}
// whitelist
for _, sx := range s.SheetsWhitelist {
if sheetProps[sx] == nil {
log.WithField("sheet", sx).Fatal("could not find sheet")
}
}
log.Info("Retrieving data and writing admins file")
r, a, w, err := getFileLines(sheetsSrv, s)
for k, v := range r {
roles[k] = v
}
admins = append(admins, a...)
whitelist = append(whitelist, w...)
}
return WriteAdminsFile(roles, admins, whitelist, opts.SquadConfigDir, opts.ASCWhitelist)
}
app.Run(os.Args)
}
func initApp() *cli.App {
app := cli.NewApp()
app.Name = "squadsheets"
app.Usage = "syncs google sheets with admin data to squad admin files"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "configuration file",
Value: "squadsheets.cfg",
},
cli.StringFlag{
Name: "configdir, d",
Usage: "Squad configuration directory",
},
cli.BoolFlag{
Name: "verbose",
Usage: "verbose logging",
},
cli.StringFlag{
Name: "log, l",
Usage: "log file",
},
cli.BoolFlag{
Name: "whitelist, w",
Usage: "ASC Whitelist",
},
}
return app
}
func validateArgs(c *cli.Context) (*CLIOpts, error) {
configDir := c.String("configdir")
if configDir == "" {
return nil, errors.New("no squad configuration directory provided")
}
return &CLIOpts{
ConfigPath: c.String("config"),
SquadConfigDir: c.String("configdir"),
LogPath: c.String("log"),
Verbose: c.Bool("verbose"),
ASCWhitelist: c.Bool("whitelist"),
}, nil
}