/
generic.go
100 lines (74 loc) · 1.78 KB
/
generic.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
package main
import (
"context"
"fmt"
"github.com/gopatchy/metadata"
"github.com/tasksolo/gosolo"
)
func create[T any](ctx context.Context, c *gosolo.Client, name string, args []string) error {
create := new(T)
err := setFields(create, args)
if err != nil {
return err
}
created, err := gosolo.CreateName[T](ctx, c, name, create)
if err != nil {
return err
}
printDetails(created)
return nil
}
func get[T any](ctx context.Context, c *gosolo.Client, name string, args []string) error {
if len(args) != 1 {
return fmt.Errorf("args: <id>")
}
shortID := args[0]
obj, err := gosolo.FindName[T](ctx, c, name, shortID)
if err != nil {
return err
}
printDetails(obj)
return nil
}
func listWrapper[T any](name string, optsCB func() *gosolo.ListOpts[T], printCB func(*T)) func(context.Context, *gosolo.Client, []string) error {
return func(ctx context.Context, c *gosolo.Client, args []string) error {
if len(args) != 0 {
return fmt.Errorf("args: none")
}
opts := &gosolo.ListOpts[T]{}
if optsCB != nil {
opts = optsCB()
}
objs, err := gosolo.ListName[T](ctx, c, name, opts)
if err != nil {
return err
}
for _, obj := range objs {
printCB(obj)
}
return nil
}
}
func update[T any](ctx context.Context, c *gosolo.Client, name string, args []string) error {
if len(args) < 2 {
return fmt.Errorf("args: <id> <field>=<value> ...") //nolint:revive,stylecheck
}
shortID := args[0]
fields := args[1:]
obj, err := gosolo.FindName[T](ctx, c, name, shortID)
if err != nil {
return err
}
md := metadata.GetMetadata(obj)
update := new(T)
err = setFields(update, fields)
if err != nil {
return err
}
updated, err := gosolo.UpdateName[T](ctx, c, name, md.ID, update, nil)
if err != nil {
return err
}
printDetails(updated)
return nil
}