/
model.go
212 lines (189 loc) · 4.99 KB
/
model.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
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"bytes"
"context"
"fmt"
"io"
"path"
"strings"
"text/tabwriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.ligato.io/vpp-agent/v3/cmd/agentctl/api/types"
agentcli "go.ligato.io/vpp-agent/v3/cmd/agentctl/cli"
)
func NewModelCommand(cli agentcli.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "model",
Short: "Manage known models",
}
cmd.AddCommand(
newModelListCommand(cli),
newModelInspectCommand(cli),
)
return cmd
}
func newModelsCommand(cli agentcli.Cli) *cobra.Command {
cmd := newModelListCommand(cli)
cmd.Use = "models"
cmd.Aliases = nil
cmd.Hidden = true
return cmd
}
func newModelListCommand(cli agentcli.Cli) *cobra.Command {
var opts ModelListOptions
cmd := &cobra.Command{
Use: "list PATTERN",
Aliases: []string{"ls", "l"},
Short: "List models mathing pattern(s)",
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
opts.Refs = args
if strings.ToLower(opts.Class) == "all" {
opts.Class = ""
}
return runModelList(cli, opts)
},
}
flags := cmd.Flags()
flags.StringVar(&opts.Class, "class", "config", "Filter by model class")
flags.StringVarP(&opts.Format, "format", "f", "", "Format output")
return cmd
}
type ModelListOptions struct {
Class string
Refs []string
Format string
}
func runModelList(cli agentcli.Cli, opts ModelListOptions) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
allModels, err := cli.Client().ModelList(ctx, types.ModelListOptions{
Class: opts.Class,
})
if err != nil {
return err
}
models := filterModelsByRefs(allModels, opts.Refs)
format := opts.Format
if len(format) == 0 {
printModelTable(cli.Out(), models)
} else {
if err := formatAsTemplate(cli.Out(), format, models); err != nil {
return err
}
}
return nil
}
func printModelTable(out io.Writer, models []types.Model) {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "MODEL NAME\tCLASS\tPROTO MESSAGE\tKEY PREFIX\t\n")
for _, model := range models {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n",
model.Name, model.Class, model.ProtoName, model.KeyPrefix)
}
if err := w.Flush(); err != nil {
panic(err)
}
fmt.Fprint(out, buf.String())
}
func filterModelsByPrefix(models []types.Model, prefixes []string) ([]types.Model, error) {
if len(prefixes) == 0 {
return models, nil
}
var filtered []types.Model
for _, pref := range prefixes {
var model types.Model
for _, m := range models {
if !strings.HasPrefix(m.Name, pref) {
continue
}
if model.Name != "" {
return nil, fmt.Errorf("multiple models found with provided prefix: %s", pref)
}
model = m
}
if model.Name == "" {
return nil, fmt.Errorf("no model found for provided prefix: %s", pref)
}
filtered = append(filtered, model)
}
return filtered, nil
}
func filterModelsByRefs(models []types.Model, refs []string) []types.Model {
var filtered []types.Model
for _, model := range models {
if !matchAnyRef(model, refs) {
continue
}
filtered = append(filtered, model)
}
return filtered
}
func matchAnyRef(model types.Model, refs []string) bool {
if len(refs) == 0 {
return true
}
for _, ref := range refs {
if ok, _ := path.Match(ref, model.Name); ok {
return true
}
}
return false
}
func newModelInspectCommand(cli agentcli.Cli) *cobra.Command {
var (
opts ModelInspectOptions
)
cmd := &cobra.Command{
Use: "inspect MODEL [MODEL...]",
Aliases: []string{"i"},
Short: "Display detailed information on one or more models",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Names = args
return runModelInspect(cli, opts)
},
}
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format for the output")
return cmd
}
type ModelInspectOptions struct {
Names []string
Format string
}
func runModelInspect(cli agentcli.Cli, opts ModelInspectOptions) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
allModels, err := cli.Client().ModelList(ctx, types.ModelListOptions{})
if err != nil {
return err
}
models, err := filterModelsByPrefix(allModels, opts.Names)
if err != nil {
return err
}
logrus.Debugf("models: %+v", models)
format := opts.Format
if len(format) == 0 {
format = "json"
}
if err := formatAsTemplate(cli.Out(), format, models); err != nil {
return err
}
return nil
}