/
common_helpers.go
419 lines (375 loc) · 10.1 KB
/
common_helpers.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package cmd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"text/template"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/manifoldco/promptui"
"github.com/spf13/pflag"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/helpers"
"github.com/treeverse/lakefs/pkg/uri"
"golang.org/x/term"
)
var isTerminal = true
const (
PathDelimiter = "/"
)
const (
LakectlInteractive = "LAKECTL_INTERACTIVE"
DeathMessage = "{{.Error|red}}\nError executing command.\n"
DeathMessageWithFields = "{{.Message|red}}\n{{.Status}}\n"
WarnMessage = "{{.Warning|yellow}}\n\n"
)
const (
internalPageSize = 1000 // when retrieving all records, use this page size under the hood
defaultAmountArgumentValue = 100 // when no amount is specified, use this value for the argument
defaultPollInterval = 3 * time.Second // default interval while pulling tasks status
minimumPollInterval = time.Second // minimum interval while pulling tasks status
defaultPollTimeout = time.Hour // default expiry for pull status with no update
)
const resourceListTemplate = `{{.Table | table -}}
{{.Pagination | paginate }}
`
var ErrTaskNotCompleted = errors.New("task not completed")
//nolint:gochecknoinits
func init() {
// disable colors if we're not attached to interactive TTY.
// when an environment variable is set, we use it to control interactive mode
// otherwise we will try to detect based on the standard output
interactiveVal := os.Getenv(LakectlInteractive)
if interactiveVal != "" {
if interactive, err := strconv.ParseBool(interactiveVal); err == nil && !interactive {
DisableColors()
}
} else if !term.IsTerminal(int(os.Stdout.Fd())) {
DisableColors()
}
}
func DisableColors() {
text.DisableColors()
isTerminal = false
}
type Table struct {
Headers []interface{}
Rows [][]interface{}
}
type Pagination struct {
Amount int
HasNext bool
After string
}
func WriteTo(tpl string, data interface{}, w io.Writer) {
templ := template.New("output")
templ.Funcs(template.FuncMap{
"red": func(arg interface{}) string {
return text.FgHiRed.Sprint(arg)
},
"yellow": func(arg interface{}) string {
return text.FgHiYellow.Sprint(arg)
},
"green": func(arg interface{}) string {
return text.FgHiGreen.Sprint(arg)
},
"blue": func(arg interface{}) string {
return text.FgHiBlue.Sprint(arg)
},
"bold": func(arg interface{}) string {
return text.Bold.Sprint(arg)
},
"date": func(ts int64) string {
return time.Unix(ts, 0).String()
},
"ljust": func(length int, s string) string {
return text.AlignLeft.Apply(s, length)
},
"json": func(v interface{}) string {
encoded, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(fmt.Sprintf("failed to encode JSON: %s", err.Error()))
}
return string(encoded)
},
"paginate": func(pag *Pagination) string {
if pag != nil && pag.Amount > 0 && pag.HasNext && isTerminal {
params := text.FgHiYellow.Sprintf("--amount %d --after \"%s\"", pag.Amount, pag.After)
return fmt.Sprintf("for more results run with %s\n", params)
}
return ""
},
"lower": strings.ToLower,
"human_bytes": func(b int64) string {
var unit int64 = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := unit, 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
},
"join": func(sep string, args []string) string {
return strings.Join(args, sep)
},
"table": func(tab *Table) string {
if isTerminal {
buf := new(bytes.Buffer)
t := table.NewWriter()
t.SetOutputMirror(buf)
t.AppendHeader(tab.Headers)
for _, row := range tab.Rows {
t.AppendRow(row)
}
t.Render()
return buf.String()
}
var b strings.Builder
for _, row := range tab.Rows {
for ic, cell := range row {
b.WriteString(fmt.Sprint(cell))
if ic < len(row)-1 {
b.WriteString("\t")
}
}
b.WriteString("\n")
}
return b.String()
},
})
t := template.Must(templ.Parse(tpl))
err := t.Execute(w, data)
if err != nil {
panic(err)
}
}
func Write(tpl string, data interface{}) {
WriteTo(tpl, data, os.Stdout)
}
func WriteIfVerbose(tpl string, data interface{}) {
if verboseMode {
WriteTo(tpl, data, os.Stdout)
}
}
func Warning(message string) {
WriteTo(WarnMessage, struct{ Warning string }{Warning: "Warning: " + message}, os.Stderr)
}
func Die(errMsg string, code int) {
WriteTo(DeathMessage, struct{ Error string }{Error: errMsg}, os.Stderr)
os.Exit(code)
}
func DieFmt(msg string, args ...interface{}) {
errMsg := fmt.Sprintf(msg, args...)
Die(errMsg, 1)
}
type APIError interface {
GetPayload() *apigen.Error
}
func DieErr(err error) {
type ErrData struct {
Error string
}
var (
apiError APIError
userVisibleError helpers.UserVisibleAPIError
)
apiError, _ = err.(APIError)
switch {
case errors.As(err, &userVisibleError):
WriteTo(DeathMessageWithFields, userVisibleError.APIFields, os.Stderr)
case apiError != nil:
WriteTo(DeathMessage, ErrData{Error: apiError.GetPayload().Message}, os.Stderr)
default:
WriteTo(DeathMessage, ErrData{Error: err.Error()}, os.Stderr)
}
os.Exit(1)
}
func RetrieveError(response interface{}, err error) error {
if err != nil {
return err
}
return helpers.ResponseAsError(response)
}
func dieOnResponseError(response interface{}, err error) {
retrievedErr := RetrieveError(response, err)
if retrievedErr != nil {
DieErr(retrievedErr)
}
}
func DieOnErrorOrUnexpectedStatusCode(response interface{}, err error, expectedStatusCode int) {
dieOnResponseError(response, err)
var statusCode int
if httpResponse, ok := response.(*http.Response); ok {
statusCode = httpResponse.StatusCode
} else {
r := reflect.Indirect(reflect.ValueOf(response))
f := r.FieldByName("HTTPResponse")
httpResponse, _ := f.Interface().(*http.Response)
if httpResponse != nil {
statusCode = httpResponse.StatusCode
}
}
if statusCode == 0 {
Die("could not get status code from response", 1)
}
if statusCode != expectedStatusCode {
DieFmt("got unexpected status code: %d, expected: %d", statusCode, expectedStatusCode)
}
}
func DieOnHTTPError(httpResponse *http.Response) {
err := helpers.HTTPResponseAsError(httpResponse)
if err != nil {
DieErr(err)
}
}
func PrintTable(rows [][]interface{}, headers []interface{}, paginator *apigen.Pagination, amount int) {
ctx := struct {
Table *Table
Pagination *Pagination
}{
Table: &Table{
Headers: headers,
Rows: rows,
},
}
if paginator.HasMore {
ctx.Pagination = &Pagination{
Amount: amount,
HasNext: true,
After: paginator.NextOffset,
}
}
Write(resourceListTemplate, ctx)
}
func MustParseRepoURI(name, s string) *uri.URI {
u, err := uri.ParseWithBaseURI(s, baseURI)
if err != nil {
DieFmt("%s %s", name, err)
}
if err = u.ValidateRepository(); err != nil {
DieFmt("%s %s", name, err)
}
return u
}
func MustParseRefURI(name, s string) *uri.URI {
u, err := uri.ParseWithBaseURI(s, baseURI)
if err != nil {
DieFmt("%s %s", name, err)
}
if err = u.ValidateRef(); err != nil {
DieFmt("%s %s", name, err)
}
return u
}
func MustParseBranchURI(name, s string) *uri.URI {
u, err := uri.ParseWithBaseURI(s, baseURI)
if err != nil {
DieFmt("%s %s", name, err)
}
if err = u.ValidateBranch(); err != nil {
DieFmt("%s %s", name, err)
}
return u
}
func MustParsePathURI(name, s string) *uri.URI {
u, err := uri.ParseWithBaseURI(s, baseURI)
if err != nil {
DieFmt("%s %s", name, err)
}
if err = u.ValidateFullyQualified(); err != nil {
DieFmt("%s %s", name, err)
}
return u
}
const (
AutoConfirmFlagName = "yes"
AutoConfigFlagShortName = "y"
AutoConfirmFlagHelp = "Automatically say yes to all confirmations"
StdinFileName = "-"
)
func AssignAutoConfirmFlag(flags *pflag.FlagSet) {
flags.BoolP(AutoConfirmFlagName, AutoConfigFlagShortName, false, AutoConfirmFlagHelp)
}
func Confirm(flags *pflag.FlagSet, question string) (bool, error) {
yes, err := flags.GetBool(AutoConfirmFlagName)
if err == nil && yes {
// got auto confirm flag
return true, nil
}
prm := promptui.Prompt{
Label: question,
IsConfirm: true,
}
_, err = prm.Run()
if err != nil {
return false, err
}
return true, nil
}
// nopCloser wraps a ReadSeekCloser to ignore calls to Close(). It is io.NopCloser (or
// ioutils.NopCloser) for Seeks.
type nopCloser struct {
io.ReadSeekCloser
}
func (nc *nopCloser) Close() error {
return nil
}
// deleteOnClose wraps a File to be a ReadSeekCloser that deletes itself when closed.
type deleteOnClose struct {
*os.File
}
func (d *deleteOnClose) Close() error {
if err := os.Remove(d.Name()); err != nil {
_ = d.File.Close() // "Only" file descriptor leak if close fails (but data might stay).
return fmt.Errorf("delete on close: %w", err)
}
return d.File.Close()
}
// OpenByPath returns a reader from the given path.
// If the path is "-", it consumes Stdin and
// opens a readable copy that is either deleted (POSIX) or will delete itself on close
// (non-POSIX, notably WINs).
func OpenByPath(path string) (io.ReadSeekCloser, error) {
if path != StdinFileName {
return os.Open(path)
}
// check if stdin is seekable
_, err := os.Stdin.Seek(0, io.SeekCurrent)
if err == nil {
return &nopCloser{ReadSeekCloser: os.Stdin}, nil
}
temp, err := os.CreateTemp("", "lakectl-stdin")
if err != nil {
return nil, fmt.Errorf("create temporary file to buffer stdin: %w", err)
}
if _, err = io.Copy(temp, os.Stdin); err != nil {
return nil, fmt.Errorf("copy stdin to temporary file: %w", err)
}
if _, err = temp.Seek(0, io.SeekStart); err != nil {
return nil, fmt.Errorf("rewind temporary copied file: %w", err)
}
// Try to delete the file. This will fail on Windows, we shall try to
// delete on close anyway.
if os.Remove(temp.Name()) != nil {
return &deleteOnClose{File: temp}, nil
}
return temp, nil
}
// Must return the call value or die with error if err is not nil
func Must[T any](v T, err error) T {
if err != nil {
DieErr(err)
}
return v
}