-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
228 lines (191 loc) · 8.69 KB
/
options.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package qbcli
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/QuickBase/quickbase-cli/qbclient"
"github.com/cpliakas/cliutil"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
en_translations "github.com/go-playground/validator/v10/translations/en"
"github.com/spf13/viper"
)
// QueryOption implements Option for string options that contain queries.
type QueryOption struct {
tag map[string]string
}
// NewQueryOption is a cliutil.OptionTypeFunc that returns a *cliutil.QueryOption.
func NewQueryOption(tag map[string]string) cliutil.OptionType { return &QueryOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *QueryOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *QueryOption) Read(cfg *viper.Viper, field reflect.Value) error {
s := ParseQuery(cfg.GetString(opt.tag["option"]))
field.SetString(s)
return nil
}
// SortOption implements Option for string options that contain queries.
type SortOption struct {
tag map[string]string
}
// NewSortOption is a cliutil.OptionTypeFunc that returns a *cliutil.SortOption.
func NewSortOption(tag map[string]string) cliutil.OptionType { return &SortOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *SortOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *SortOption) Read(cfg *viper.Viper, field reflect.Value) error {
s := cfg.GetString(opt.tag["option"])
if s == "" {
return nil
}
v, err := ParseSortBy(s)
if err != nil {
return err
}
field.Set(reflect.ValueOf(v))
return nil
}
// GroupOption implements Option for string options that contain queries.
type GroupOption struct {
tag map[string]string
}
// NewGroupOption is a cliutil.OptionTypeFunc that returns a *cliutil.GroupOption.
func NewGroupOption(tag map[string]string) cliutil.OptionType { return &GroupOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *GroupOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *GroupOption) Read(cfg *viper.Viper, field reflect.Value) error {
s := cfg.GetString(opt.tag["option"])
if s == "" {
return nil
}
v, err := ParseGroupBy(s)
if err != nil {
return err
}
field.Set(reflect.ValueOf(v))
return nil
}
// RecordOption implements Option for string options that contain record datas.
type RecordOption struct {
tag map[string]string
}
// NewRecordOption is a cliutil.OptionTypeFunc that returns a *cliutil.RecordOption.
func NewRecordOption(tag map[string]string) cliutil.OptionType { return &RecordOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *RecordOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *RecordOption) Read(cfg *viper.Viper, field reflect.Value) error {
// Get the cached table schema.
// TODO Do we need to remove the hard-coding to "to"?
m, err := GetCachedTableSchema(cfg.GetString("to"))
if err != nil {
return err
}
// Parse the key/value pairs into *qbclient.Value objects, and build the
// record being inserted into the table.
record := make(map[int]*qbclient.InsertRecordsInputData)
data := cliutil.ParseKeyValue(cfg.GetString(opt.tag["option"]))
for k, v := range data {
// Make sure k is an integer.
fid, err := strconv.Atoi(k)
if err != nil {
return fmt.Errorf("key %s: %w", k, err)
}
// Get the field type from the table's field type map.
field, ok := m[fid]
if !ok {
return fmt.Errorf("field %v not defined in table", fid)
}
// Create a *qbclient.Value from the string value and field type.
val, err := qbclient.NewValueFromString(v, field.Type)
if err != nil {
return fmt.Errorf("value invalid for field %v: %w", fid, err)
}
// Add the value to the record .
record[fid] = &qbclient.InsertRecordsInputData{Value: val}
}
// Set the field's value.
r := []map[int]*qbclient.InsertRecordsInputData{record}
field.Set(reflect.ValueOf(r))
return nil
}
// GetOptions gets options based on the input and validates them.
func GetOptions(ctx context.Context, logger *cliutil.LeveledLogger, input interface{}, cfg *viper.Viper) {
err := cliutil.GetOptions(input, cfg)
logger.FatalIfError(ctx, "error getting options", err)
validate := validator.New()
english := en.New()
uni := ut.New(english, english)
trans, _ := uni.GetTranslator("en")
_ = en_translations.RegisterDefaultTranslations(validate, trans)
// Custom translation for the "required" validator.
validate.RegisterTranslation("required", trans, func(ut ut.Translator) error {
return ut.Add("required", "{0} option is required", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
// TODO We should be defensive, even if the error conditions shouldn't happen.
field, _ := reflect.ValueOf(input).Elem().Type().FieldByName(fe.Field())
tag := cliutil.ParseKeyValue(field.Tag.Get("cliutil"))
t, _ := ut.T("required", tag["option"])
return t
})
// Other validators we need to translate:
//
// - required_if (See Field.Label)
// - min (See DeleteFieldsInput.FieldID)
msgs := []string{}
verr := validate.Struct(input)
if verr != nil {
verrs := verr.(validator.ValidationErrors)
for _, ve := range verrs {
msgs = append(msgs, ve.Translate(trans))
}
}
if len(msgs) > 0 {
HandleError(ctx, logger, "input not valid", errors.New(strings.Join(msgs, ", ")))
}
}
func init() {
cliutil.RegisterOptionTypeFunc("query", NewQueryOption)
cliutil.RegisterOptionTypeFunc("record", NewRecordOption)
cliutil.RegisterOptionTypeFunc("sort", NewSortOption)
cliutil.RegisterOptionTypeFunc("group", NewGroupOption)
cliutil.SetOptionMetadata("app-id", map[string]string{"usage": "the app's unique identifier, e.g., bqgruir3g"})
cliutil.SetOptionMetadata("batch-size", map[string]string{"usage": "the number of rows processed in each batch"})
cliutil.SetOptionMetadata("child-table-id", map[string]string{"usage": "the child table's unique identifier, e.g., bqgruir7z"})
cliutil.SetOptionMetadata("data", map[string]string{"usage": "the record data in key=value format, e.g., '6=\"Another Record\" 7=3'"})
cliutil.SetOptionMetadata("delay", map[string]string{"usage": "delay between batches in milliseconds"})
cliutil.SetOptionMetadata("field-id", map[string]string{"usage": "the fields's unique identifier, e.g., 6"})
cliutil.SetOptionMetadata("fields-to-return", map[string]string{"usage": "the list/range of fields to return, e.g., 6,7,10:15"})
cliutil.SetOptionMetadata("from", map[string]string{"usage": "the table's unique identifier, e.g., bqgruir7z"})
cliutil.SetOptionMetadata("group-by", map[string]string{"usage": "group records by fields, e.g., '6 DESC,7 ASC,8 equal-values'"})
cliutil.SetOptionMetadata("lookup-field-ids", map[string]string{"usage": "the list/range of fids for lookup fields to create, e.g., 6,7,10:15"})
cliutil.SetOptionMetadata("map", map[string]string{"usage": "map csv header labels to destination table field labels, e.g., \"'Old Label 1'='New Label 1' 'Old Label 2'='New Label 2'\""})
cliutil.SetOptionMetadata("parent-table-id", map[string]string{"usage": "the parent table's unique identifier, e.g., bqgruir6f"})
cliutil.SetOptionMetadata("relationship-id", map[string]string{"usage": "the relationship's unique identifier, e.g., 10"})
cliutil.SetOptionMetadata("report-id", map[string]string{"usage": "the report's unique identifier, e.g., 1"})
cliutil.SetOptionMetadata("select", map[string]string{"usage": "the list/range of fields to return, e.g., 6,7,10:15"})
cliutil.SetOptionMetadata("skip", map[string]string{"usage": "the number of records to skip"})
cliutil.SetOptionMetadata("sort-by", map[string]string{"usage": "sort records by fields, e.g., '6 DESC,8 ASC'"})
cliutil.SetOptionMetadata("table-id", map[string]string{"usage": "the table's unique identifier, e.g., bqgruir7z"})
cliutil.SetOptionMetadata("to", map[string]string{"usage": "the table's unique identifier, e.g., bqgruir7z"})
cliutil.SetOptionMetadata("top", map[string]string{"usage": "the maximum number of records to display"})
cliutil.SetOptionMetadata("use-app-time", map[string]string{"usage": "run the query against a date time field with the app's local time instead of UTC"})
cliutil.SetOptionMetadata("where", map[string]string{"usage": "the filter, using the Quickbase query language or simplified syntax e.g., {3.EX.2}, 3=2"})
}