/
cmds.go
288 lines (245 loc) · 7.54 KB
/
cmds.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
package cmds
import (
"context"
"github.com/go-go-golems/glazed/pkg/cmds/layers"
"github.com/go-go-golems/glazed/pkg/cmds/layout"
"github.com/go-go-golems/glazed/pkg/cmds/parameters"
"github.com/go-go-golems/glazed/pkg/middlewares"
"gopkg.in/yaml.v3"
"io"
)
// CommandDescription contains the necessary information for registering
// a command with cobra. Because a command gets registered in a verb tree,
// a full list of Parents all the way to the root needs to be provided.
type CommandDescription struct {
Name string `yaml:"name"`
Short string `yaml:"short"`
Long string `yaml:"long,omitempty"`
// TODO(manuel, 2023-12-21) Does this need to be a list of pointers? Could it just be a list of struct?
Layout []*layout.Section `yaml:"layout,omitempty"`
Layers *layers.ParameterLayers `yaml:"layers,omitempty"`
AdditionalData map[string]interface{} `yaml:"additionalData,omitempty"`
Parents []string `yaml:",omitempty"`
// Source indicates where the command was loaded from, to make debugging easier.
Source string `yaml:",omitempty"`
}
// Steal the builder API from https://github.com/bbkane/warg
type CommandDescriptionOption func(*CommandDescription)
func WithName(s string) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Name = s
}
}
func WithShort(s string) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Short = s
}
}
func WithLong(s string) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Long = s
}
}
func WithLayersList(ls ...layers.ParameterLayer) CommandDescriptionOption {
return func(c *CommandDescription) {
for _, l := range ls {
c.Layers.Set(l.GetSlug(), l)
}
}
}
func WithLayers(ls *layers.ParameterLayers) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Layers.Merge(ls)
}
}
// WithFlags is a convenience function to add arguments to the default layer, useful
// to make the transition from explicit flags and arguments to a default layer a bit easier.
func WithFlags(
flags ...*parameters.ParameterDefinition,
) CommandDescriptionOption {
return func(c *CommandDescription) {
layer, ok := c.GetDefaultLayer()
var err error
if !ok {
layer, err = layers.NewParameterLayer(layers.DefaultSlug, "Flags")
if err != nil {
panic(err)
}
c.Layers.Set(layer.GetSlug(), layer)
err = c.Layers.MoveToFront(layer.GetSlug())
if err != nil {
panic(err)
}
}
layer.AddFlags(flags...)
}
}
// WithArguments is a convenience function to add arguments to the default layer, useful
// to make the transition from explicit flags and arguments to a default layer a bit easier.
func WithArguments(
arguments ...*parameters.ParameterDefinition,
) CommandDescriptionOption {
return func(c *CommandDescription) {
layer, ok := c.GetDefaultLayer()
var err error
if !ok {
layer, err = layers.NewParameterLayer(layers.DefaultSlug, "Arguments")
if err != nil {
panic(err)
}
c.Layers.Set(layer.GetSlug(), layer)
err = c.Layers.MoveToFront(layer.GetSlug())
if err != nil {
panic(err)
}
}
for _, arg := range arguments {
arg.IsArgument = true
}
layer.AddFlags(arguments...)
}
}
func WithLayout(l *layout.Layout) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Layout = l.Sections
}
}
func WithReplaceLayers(layers_ ...layers.ParameterLayer) CommandDescriptionOption {
return func(c *CommandDescription) {
for _, l := range layers_ {
c.Layers.Set(l.GetSlug(), l)
}
}
}
func WithParents(p ...string) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Parents = p
}
}
func WithStripParentsPrefix(prefixes []string) CommandDescriptionOption {
return func(c *CommandDescription) {
toRemove := 0
for i, p := range c.Parents {
if i < len(prefixes) && p == prefixes[i] {
toRemove = i + 1
}
}
c.Parents = c.Parents[toRemove:]
}
}
func WithSource(s string) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Source = s
}
}
func WithPrependSource(s string) CommandDescriptionOption {
return func(c *CommandDescription) {
c.Source = s + c.Source
}
}
func NewCommandDescription(name string, options ...CommandDescriptionOption) *CommandDescription {
ret := &CommandDescription{
Name: name,
Layers: layers.NewParameterLayers(),
}
for _, o := range options {
o(ret)
}
return ret
}
func (cd *CommandDescription) GetDefaultLayer() (layers.ParameterLayer, bool) {
return cd.GetLayer(layers.DefaultSlug)
}
func (cd *CommandDescription) GetDefaultFlags() *parameters.ParameterDefinitions {
l, ok := cd.GetDefaultLayer()
if !ok {
return parameters.NewParameterDefinitions()
}
return l.GetParameterDefinitions().GetFlags()
}
func (cd *CommandDescription) GetDefaultArguments() *parameters.ParameterDefinitions {
l, ok := cd.GetDefaultLayer()
if !ok {
return parameters.NewParameterDefinitions()
}
return l.GetParameterDefinitions().GetArguments()
}
func (cd *CommandDescription) GetLayer(name string) (layers.ParameterLayer, bool) {
return cd.Layers.Get(name)
}
func (cd *CommandDescription) Clone(cloneLayers bool, options ...CommandDescriptionOption) *CommandDescription {
// clone flags
layers_ := layers.NewParameterLayers()
if cloneLayers {
layers_ = cd.Layers.Clone()
}
// copy parents
parents := make([]string, len(cd.Parents))
copy(parents, cd.Parents)
ret := &CommandDescription{
Name: cd.Name,
Short: cd.Short,
Long: cd.Long,
Layers: layers_,
Parents: parents,
Source: cd.Source,
}
for _, o := range options {
o(ret)
}
return ret
}
func (cd *CommandDescription) ToYAML(w io.Writer) error {
enc := yaml.NewEncoder(w)
defer func(enc *yaml.Encoder) {
_ = enc.Close()
}(enc)
return enc.Encode(cd)
}
func (cd *CommandDescription) Description() *CommandDescription {
return cd
}
type Command interface {
Description() *CommandDescription
ToYAML(w io.Writer) error
}
type CommandWithMetadata interface {
Command
Metadata(ctx context.Context, parsedLayers *layers.ParsedLayers) (map[string]interface{}, error)
}
// NOTE(manuel, 2023-03-17) Future types of commands that we could need
// - async emitting command (just strings, for example)
// - async emitting structured log
// - async emitting of glaze rows (useful in general, and could be done with a special TableOutputFormatter, really)
// - no output (just do it yourself)
// - typed generic output structure (with error)
type BareCommand interface {
Command
Run(ctx context.Context, parsedLayers *layers.ParsedLayers) error
}
type WriterCommand interface {
Command
RunIntoWriter(ctx context.Context, parsedLayers *layers.ParsedLayers, w io.Writer) error
}
type GlazeCommand interface {
Command
// RunIntoGlazeProcessor is called to actually execute the command.
//
// NOTE(manuel, 2023-02-27) We can probably simplify this to only take parsed layers
//
// The ps and GlazeProcessor calls could be replaced by a GlazeCommand specific layer,
// which would allow the GlazeCommand to parse into a specific struct. The GlazeProcessor
// is just something created by the passed in GlazeLayer anyway.
//
// When we are just left with building a convenience wrapper for Glaze based commands,
// instead of forcing it into the upstream interface.
//
// https://github.com/go-go-golems/glazed/issues/217
// https://github.com/go-go-golems/glazed/issues/216
// See https://github.com/go-go-golems/glazed/issues/173
RunIntoGlazeProcessor(ctx context.Context, parsedLayers *layers.ParsedLayers, gp middlewares.Processor) error
}
type ExitWithoutGlazeError struct{}
func (e *ExitWithoutGlazeError) Error() string {
return "Exit without glaze"
}