-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeneric.go
More file actions
218 lines (194 loc) · 7.12 KB
/
generic.go
File metadata and controls
218 lines (194 loc) · 7.12 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package genericcli
import (
"io"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/multisort"
"github.com/spf13/afero"
)
// GenericCLI can be used to gain generic CLI functionality.
//
// C is the create request for an entity.
// U is the update request for an entity.
// R is the response object of an entity.
type GenericCLI[C any, U any, R any] struct {
// internally we map everything to multi arg cli to reduce code redundance
multiCLI *MultiArgGenericCLI[C, U, R]
}
// CRUD must be implemented in order to get generic CLI functionality.
//
// C is the create request for an entity.
// U is the update request for an entity.
// R is the response object of an entity.
type CRUD[C any, U any, R any] interface {
// Get returns the entity with the given id.
Get(id string) (R, error)
// List returns a slice of entities.
List() ([]R, error)
// Create tries to create the entity with the given request and returns the created entity.
Create(rq C) (R, error)
// Update tries to update the entity with the given request and returns the updated entity.
Update(rq U) (R, error)
// Delete tries to delete the entity with the given id and returns the deleted entity.
Delete(id string) (R, error)
// Convert converts an entity's response object to best possible create and update requests and additionally returns the entities ID.
// This is required for capabilities like creation/update/deletion from a file of response objects.
Convert(r R) (string, C, U, error)
}
// NewGenericCLI returns a new generic cli.
//
// C is the create request for an entity.
// U is the update request for an entity.
// R is the response object of an entity.
func NewGenericCLI[C any, U any, R any](crud CRUD[C, U, R]) *GenericCLI[C, U, R] {
return &GenericCLI[C, U, R]{
multiCLI: NewGenericMultiArgCLI(multiArgMapper[C, U, R]{singleArg: crud}),
}
}
func (a *GenericCLI[C, U, R]) WithFS(fs afero.Fs) *GenericCLI[C, U, R] {
a.multiCLI.WithFS(fs)
return a
}
func (a *GenericCLI[C, U, R]) WithSorter(sorter *multisort.Sorter[R]) *GenericCLI[C, U, R] {
a.multiCLI.WithSorter(sorter)
return a
}
// WithBulkPrint prints results in a bulk at the end on multi-entity operations, the results are a list.
// default is printing results intermediately during the bulk operation, which causes single entities to be printed in sequence.
func (a *GenericCLI[C, U, R]) WithBulkPrint() *GenericCLI[C, U, R] {
a.multiCLI.WithBulkPrint()
return a
}
// WithBulkSecurityPrompt prints interactive prompts before a multi-entity operation if there is a tty.
func (a *GenericCLI[C, U, R]) WithBulkSecurityPrompt(in io.Reader, out io.Writer) *GenericCLI[C, U, R] {
a.multiCLI.WithBulkSecurityPrompt(in, out)
return a
}
// WithBulkTimestamps prints out the duration of an operation to stdout during a bulk operation.
func (a *GenericCLI[C, U, R]) WithTimestamps() *GenericCLI[C, U, R] {
a.multiCLI.WithTimestamps()
return a
}
// Interface returns the interface that was used to create this generic cli.
func (a *GenericCLI[C, U, R]) Interface() MultiArgCRUD[C, U, R] {
return a.multiCLI.Interface()
}
// Sorter returns the sorter of this generic cli.
func (a *GenericCLI[C, U, R]) Sorter() *multisort.Sorter[R] {
return a.multiCLI.Sorter()
}
func (a *GenericCLI[C, U, R]) List(sortKeys ...multisort.Key) ([]R, error) {
return a.multiCLI.List(sortKeys...)
}
func (a *GenericCLI[C, U, R]) ListAndPrint(p printers.Printer, sortKeys ...multisort.Key) error {
return a.multiCLI.ListAndPrint(p, sortKeys...)
}
func (a *GenericCLI[C, U, R]) Describe(id string) (R, error) {
return a.multiCLI.Describe(id)
}
func (a *GenericCLI[C, U, R]) DescribeAndPrint(id string, p printers.Printer) error {
return a.multiCLI.DescribeAndPrint(p, id)
}
func (a *GenericCLI[C, U, R]) Delete(id string) (R, error) {
return a.multiCLI.Delete(id)
}
func (a *GenericCLI[C, U, R]) DeleteAndPrint(id string, p printers.Printer) error {
return a.multiCLI.DeleteAndPrint(p, id)
}
func (a *GenericCLI[C, U, R]) Create(rq C) (R, error) {
return a.multiCLI.Create(rq)
}
func (a *GenericCLI[C, U, R]) CreateAndPrint(rq C, p printers.Printer) error {
return a.multiCLI.CreateAndPrint(rq, p)
}
func (a *GenericCLI[C, U, R]) Update(rq U) (R, error) {
return a.multiCLI.Update(rq)
}
func (a *GenericCLI[C, U, R]) UpdateAndPrint(rq U, p printers.Printer) error {
return a.multiCLI.UpdateAndPrint(rq, p)
}
func (a *GenericCLI[C, U, R]) Edit(args []string) (R, error) {
return a.multiCLI.Edit(1, args)
}
func (a *GenericCLI[C, U, R]) EditAndPrint(args []string, p printers.Printer) error {
return a.multiCLI.EditAndPrint(1, args, p)
}
func (a *GenericCLI[C, U, R]) CreateFromFile(from string) (BulkResults[R], error) {
return a.multiCLI.CreateFromFile(from)
}
func (a *GenericCLI[C, U, R]) CreateFromFileAndPrint(from string, p printers.Printer) error {
return a.multiCLI.CreateFromFileAndPrint(from, p)
}
func (a *GenericCLI[C, U, R]) UpdateFromFile(from string) (BulkResults[R], error) {
return a.multiCLI.UpdateFromFile(from)
}
func (a *GenericCLI[C, U, R]) UpdateFromFileAndPrint(from string, p printers.Printer) error {
return a.multiCLI.UpdateFromFileAndPrint(from, p)
}
func (a *GenericCLI[C, U, R]) ApplyFromFile(from string) (BulkResults[R], error) {
return a.multiCLI.ApplyFromFile(from)
}
func (a *GenericCLI[C, U, R]) ApplyFromFileAndPrint(from string, p printers.Printer) error {
return a.multiCLI.ApplyFromFileAndPrint(from, p)
}
func (a *GenericCLI[C, U, R]) DeleteFromFile(from string) (BulkResults[R], error) {
return a.multiCLI.DeleteFromFile(from)
}
func (a *GenericCLI[C, U, R]) DeleteFromFileAndPrint(from string, p printers.Printer) error {
return a.multiCLI.DeleteFromFileAndPrint(from, p)
}
type multiArgMapper[C any, U any, R any] struct {
singleArg CRUD[C, U, R]
}
func (v multiArgMapper[C, U, R]) Get(ids ...string) (R, error) {
id, err := GetExactlyOneArg(ids)
if err != nil {
var zero R
return zero, err
}
return v.singleArg.Get(id)
}
func (v multiArgMapper[C, U, R]) List() ([]R, error) {
return v.singleArg.List()
}
func (v multiArgMapper[C, U, R]) Create(rq C) (R, error) {
return v.singleArg.Create(rq)
}
func (v multiArgMapper[C, U, R]) Update(rq U) (R, error) {
return v.singleArg.Update(rq)
}
func (v multiArgMapper[C, U, R]) Delete(ids ...string) (R, error) {
id, err := GetExactlyOneArg(ids)
if err != nil {
var zero R
return zero, err
}
return v.singleArg.Delete(id)
}
func (v multiArgMapper[C, U, R]) Convert(r R) ([]string, C, U, error) {
id, cr, ur, err := v.singleArg.Convert(r)
return []string{id}, cr, ur, err
}
// following only used for mock generation (has to be in non-test file), do not use:
type (
testClient interface {
Get(id string) (*testResponse, error)
List() ([]*testResponse, error)
Create(rq *testCreate) (*testResponse, error)
Update(rq *testUpdate) (*testResponse, error)
Delete(id string) (*testResponse, error)
Convert(r *testResponse) ([]string, *testCreate, *testUpdate, error)
}
testCRUD struct{ client testClient }
testCreate struct {
ID string `json:"id"`
Name string `json:"name"`
}
testUpdate struct {
ID string `json:"id"`
Name string `json:"name"`
}
testResponse struct {
ID string `json:"id"`
Name string `json:"name"`
}
)