-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
198 lines (175 loc) · 4.5 KB
/
utils.go
File metadata and controls
198 lines (175 loc) · 4.5 KB
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
package commands
import (
"bytes"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"time"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
"github.com/olekukonko/tablewriter"
yaml "gopkg.in/yaml.v3"
)
var (
defaultLimit = -1
defaultLines = 64
)
func progress(wOut io.Writer) chan bool {
frames := []string{"...", "o..", ".o.", "..o"}
backspaces := strings.Repeat("\b", 3)
tick := time.NewTicker(400 * time.Millisecond)
quit := make(chan bool)
go func() {
for {
for _, frame := range frames {
fmt.Fprint(wOut, frame)
select {
case <-quit:
fmt.Fprint(wOut, backspaces)
close(quit)
return
case <-tick.C:
fmt.Fprint(wOut, backspaces)
}
}
}
}()
return quit
}
func drinkOfChoice() string {
drink := os.Getenv("DRYCC_DRINK_OF_CHOICE")
if drink == "" {
drink = "coffee"
}
return drink
}
func limitCount(objs, total int) string {
if objs == total {
return "\n"
}
return fmt.Sprintf(" (%d of %d)\n", objs, total)
}
// checkAPICompatibility handles specific behavior for certain errors,
// such as printing an warning for the API mismatch error
func (d *DryccCmd) checkAPICompatibility(c *drycc.Client, err error) error {
if err == drycc.ErrAPIMismatch {
if !d.Warned {
d.PrintErrf(`! WARNING: Client and server API versions do not match. Please consider upgrading.
! Client version: %s
! Server version: %s
`, drycc.APIVersion, c.ControllerAPIVersion)
d.Warned = true
}
// API mismatch isn't fatal, so after warning continue on.
return nil
}
return err
}
// getDefaultFormatTable return default format ascii table
func (d *DryccCmd) getDefaultFormatTable(headers []string) *tablewriter.Table {
table := tablewriter.NewWriter(d.WOut)
table.SetHeader(headers)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding(fmt.Sprintf("%4s", " "))
table.SetNoWhiteSpace(true)
return table
}
// format time string to local time
func (d *DryccCmd) formatTime(timeStr string) string {
t, err := time.Parse(time.RFC3339, timeStr)
if err != nil {
return timeStr
}
if d.Location != nil {
return t.In(d.Location).Format(time.RFC3339)
}
return t.In(time.UTC).Format(time.RFC3339)
}
// wrapString wraps s into a paragraph of lines of length lim, with minimal raggedness.
func (d *DryccCmd) wrapString(s string) string {
sa, _ := tablewriter.WrapString(s, defaultLines)
return strings.Join(sa, "\n")
}
// fixateString fix s width.
func (d *DryccCmd) fixateString(s string, width int) string {
switch {
case len(s) > width:
trimSize := len(s) - width + 3
if trimSize < len(s) {
s = "..." + s[trimSize:]
}
case len(s) < width:
s += strings.Repeat(" ", width-len(s))
}
return s
}
// indentString indent s into a paragraph of lines of length lim, with minimal raggedness.
func (d *DryccCmd) indentString(s string, indent int) string {
var lines []string
for _, line := range strings.Split(s, "\n") {
line = strings.TrimRight(line, "\r")
padding := indent + len(line)
lines = append(lines, fmt.Sprintf("% "+strconv.Itoa(padding)+"s", line))
}
return strings.Join(lines, "\n")
}
// toYamlString convert object to yaml string
func (d *DryccCmd) toYamlString(v any, indent int) string {
buf := bytes.Buffer{}
encode := yaml.NewEncoder(&buf)
encode.SetIndent(indent)
encode.Encode(v)
return buf.String()
}
func sortKeys(data map[string]any) *[]string {
keys := make([]string, 0, len(data))
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
return &keys
}
func sortConfigValues(data []api.ConfigValue) []api.ConfigValue {
sort.Slice(data, func(i, j int) bool {
return strings.ToLower(data[i].Name) < strings.ToLower(data[j].Name)
})
return data
}
func sortPtypes(ptypes []string) []string {
sort.Slice(ptypes, func(i, j int) bool {
if ptypes[i] == "web" {
return true
}
if ptypes[j] == "web" {
return false
}
return ptypes[i] < ptypes[j]
})
return ptypes
}
func safeGetString(data string) string {
if data == "" {
return "<none>"
}
return data
}
// ResponseLimit converts a limit value to the format expected by the API.
// If limit is 0, it returns -1 to indicate no limit.
func ResponseLimit(limit int) (int, error) {
if limit == 0 {
return -1, nil
}
return limit, nil
}