/
common.go
185 lines (166 loc) · 4.2 KB
/
common.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
package cmd
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"math"
"os"
"regexp"
"strconv"
"strings"
"github.com/richarda23/rspace-client-go/rspace"
//"errors"
)
const (
DISPLAY_TIMESTAMP_WIDTH = 16
)
func exitWithStdErrMsg(message string) {
messageStdErr(message)
os.Exit(1)
}
func exitWithErr(err error) {
exitWithStdErrMsg(err.Error())
}
func messageStdErr(message string) {
fmt.Fprintln(os.Stderr, message)
}
func message(writer io.Writer, message string) {
fmt.Fprintln(writer, message)
}
func configurePagination() rspace.RecordListingConfig {
cfg := rspace.NewRecordListingConfig()
if len(sortOrderArg) > 0 && validateArrayContains(validSortOrders, []string{sortOrderArg}) {
cfg.SortOrder = sortOrderArg
}
if len(orderByArg) > 0 && validateArrayContains(validRecordOrders, []string{orderByArg}) {
cfg.OrderBy = orderByArg
}
// name sort is asc by default
if orderByArg == "name" && len(sortOrderArg) == 0 {
cfg.SortOrder = "asc"
}
if pageSizeArg > 0 {
cfg.PageSize = pageSizeArg
}
return cfg
}
func idsFromGlobalIds(globalIds []string) []int {
ids := make([]int, 0)
for _, v := range globalIds {
id, err := idFromGlobalId(v)
if err != nil || id == 0 {
messageStdErr(fmt.Sprintf("%s is not a valid identifier, skipping", v))
} else {
ids = append(ids, id)
}
}
return ids
}
// idFromGlobalId matches either globalId string or a numeric id, returning
// the numeric id or an Error if input string cannot be parsed
func idFromGlobalId(globalId string) (int, error) {
v, _ := strconv.Atoi(globalId)
if v > 0 {
return v, nil
}
match, err := regexp.MatchString("^[A-Z]{2}\\d+", globalId)
if match {
globalId = globalId[2:] //handle globalId
v, _ := strconv.Atoi(globalId)
return v, nil
} else {
return 0, err
}
}
func prettyMarshal(anything interface{}) string {
bytes, _ := json.MarshalIndent(anything, "", "\t")
return string(bytes)
}
type columnDef struct {
Title string
Width int
}
type TableResult struct {
Headers []columnDef
Content [][]string
}
type ResultListFormatter interface {
ToJson() string
ToTable() *TableResult
ToQuiet() []identifiable
}
func printTable(ctx *Context, table *TableResult) {
printTableHeaders(ctx, table.Headers)
printContent(ctx, table)
}
func printCsv(ctx *Context, table *TableResult) {
writer := csv.NewWriter(ctx.Writer)
writer.Write(columnDefsToString(table.Headers))
if err := writer.WriteAll(table.Content); err != nil {
exitWithErr(err)
}
}
func columnDefsToString(headers []columnDef) []string {
rowToPrint := make([]string, 0)
for _, header := range headers {
rowToPrint = append(rowToPrint, header.Title)
}
return rowToPrint
}
func printContent(ctx *Context, table *TableResult) {
for _, row := range table.Content {
rowToPrint := make([]string, 0)
for i, cell := range row {
toPrint := abbreviate(cell, table.Headers[i].Width)
toPrint = fmt.Sprintf("%-[1]*s", table.Headers[i].Width, toPrint)
rowToPrint = append(rowToPrint, toPrint)
}
ctx.write(strings.Join(rowToPrint, "\t"))
}
}
func printTableHeaders(ctx *Context, headers []columnDef) {
headersToPrint := make([]string, 0)
for _, header := range headers {
toPrint := abbreviate(header.Title, header.Width)
toPrint = fmt.Sprintf("%-[1]*s", header.Width, toPrint)
headersToPrint = append(headersToPrint, toPrint)
}
ctx.write(strings.Join(headersToPrint, "\t"))
}
func abbreviate(toAbbreviate string, maxLen int) string {
if maxLen > 3 && len(toAbbreviate) > maxLen {
toAbbreviate = toAbbreviate[0:(maxLen-2)] + ".."
}
return toAbbreviate
}
type identifiable struct {
Id string
}
//
func printIds(ctx *Context, source []identifiable) {
for _, item := range source {
ctx.write(item.Id)
}
}
// gets the length of the longest name in the result list
func getMaxNameLength(results []rspace.BasicInfo) int {
var maxPossible float64 = 25
var currLongest float64 = 0
for _, res := range results {
if nameLen := float64(len(res.GetName())); nameLen > currLongest {
currLongest = math.Min(maxPossible, nameLen)
}
}
return int(currLongest)
}
func globalIdListToIntList(slice []string) []int {
results := make([]int, 0)
for _, v := range slice {
id, _ := idFromGlobalId(v)
if id != 0 {
results = append(results, id)
}
}
return results
}