Repository navigation
Expand file tree
/
Copy pathflags.go
More file actions
233 lines (204 loc) · 7.66 KB
/
Copy pathflags.go
File metadata and controls
233 lines (204 loc) · 7.66 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package flags
import (
"fmt"
"strings"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/spf13/cobra"
"github.com/stoewer/go-strcase"
"k8s.io/kube-openapi/pkg/validation/spec"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
utilcomp "k8s.io/kubectl/pkg/util/completion"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
)
const (
CobraInt = "int"
CobraSting = "string"
CobraBool = "bool"
CobraFloat64 = "float64"
CobraStringArray = "stringArray"
CobraIntSlice = "intSlice"
CobraFloat64Slice = "float64Slice"
CobraBoolSlice = "boolSlice"
)
const (
typeString = "string"
typeNumber = "number"
typeInteger = "integer"
typeBoolean = "boolean"
typeObject = "object"
typeArray = "array"
)
// AddClusterDefinitionFlag adds a flag "cluster-definition" for the cmd and stores the value of the flag
// in string p
func AddClusterDefinitionFlag(f cmdutil.Factory, cmd *cobra.Command, p *string) {
cmd.Flags().StringVar(p, "cluster-definition", *p, "Specify cluster definition, run \"kbcli clusterdefinition list\" to show all available cluster definition")
util.CheckErr(cmd.RegisterFlagCompletionFunc("cluster-definition",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return utilcomp.CompGetResource(f, util.GVRToString(types.ClusterDefGVR()), toComplete), cobra.ShellCompDirectiveNoFileComp
}))
}
// BuildFlagsBySchema builds a flag.FlagSet by the given schema, convert the schema key
// to flag name, and convert the schema type to flag type.
func BuildFlagsBySchema(cmd *cobra.Command, schema *spec.Schema) error {
if schema == nil {
return nil
}
for name, prop := range schema.Properties {
if err := buildOneFlag(cmd, name, &prop, false); err != nil {
return err
}
}
for _, name := range schema.Required {
flagName := strcase.KebabCase(name)
// fixme: array/object type flag will be "--servers.name", but in schema.Required will check the "--servers" flag, do not mark the array type as required in schema.json
if schema.Properties[name].Type[0] == typeObject || schema.Properties[name].Type[0] == typeArray {
continue
}
if err := cmd.MarkFlagRequired(flagName); err != nil {
return err
}
}
return nil
}
// castOrZero is a helper function to cast a value to a specific type.
// If the cast fails, the zero value will be returned.
func castOrZero[T any](v any) T {
cv, _ := v.(T)
return cv
}
func buildOneFlag(cmd *cobra.Command, k string, s *spec.Schema, isArray bool) error {
name := strcase.KebabCase(k)
tpe := typeString
if len(s.Type) > 0 {
tpe = s.Type[0]
}
if isArray {
switch tpe {
case typeString:
cmd.Flags().StringArray(name, []string{castOrZero[string](s.Default)}, buildFlagDescription(s))
case typeInteger:
cmd.Flags().IntSlice(name, []int{castOrZero[int](s.Default)}, buildFlagDescription(s))
case typeNumber:
cmd.Flags().Float64Slice(name, []float64{castOrZero[float64](s.Default)}, buildFlagDescription(s))
case typeBoolean:
cmd.Flags().BoolSlice(name, []bool{castOrZero[bool](s.Default)}, buildFlagDescription(s))
case typeObject:
for subName, prop := range s.Properties {
if err := buildOneFlag(cmd, fmt.Sprintf("%s.%s", name, subName), &prop, true); err != nil {
return err
}
}
case typeArray:
return fmt.Errorf("unsupported build flags for object with array nested within an array")
default:
return fmt.Errorf("unsupported json schema type %s", s.Type)
}
registerFlagCompFunc(cmd, name, s)
} else {
switch tpe {
case typeString:
cmd.Flags().String(name, castOrZero[string](s.Default), buildFlagDescription(s))
case typeInteger:
cmd.Flags().Int(name, int(castOrZero[float64](s.Default)), buildFlagDescription(s))
case typeNumber:
cmd.Flags().Float64(name, castOrZero[float64](s.Default), buildFlagDescription(s))
case typeBoolean:
cmd.Flags().Bool(name, castOrZero[bool](s.Default), buildFlagDescription(s))
case typeObject:
for subName, prop := range s.Properties {
if err := buildOneFlag(cmd, fmt.Sprintf("%s.%s", name, subName), &prop, false); err != nil {
return err
}
}
case typeArray:
if err := buildOneFlag(cmd, name, s.Items.Schema, true); err != nil {
return err
}
default:
return fmt.Errorf("unsupported json schema type %s", s.Type)
}
registerFlagCompFunc(cmd, name, s)
}
// flag not support array type
return nil
}
func buildFlagDescription(s *spec.Schema) string {
desc := strings.Builder{}
desc.WriteString(s.Description)
var legalVals []string
for _, e := range s.Enum {
legalVals = append(legalVals, fmt.Sprintf("%v", e))
}
if len(legalVals) > 0 {
desc.WriteString(fmt.Sprintf(" Legal values [%s].", strings.Join(legalVals, ", ")))
}
var valueRange []string
if s.Minimum != nil {
valueRange = append(valueRange, fmt.Sprintf("%v", *s.Minimum))
}
if s.Maximum != nil {
valueRange = append(valueRange, fmt.Sprintf("%v", *s.Maximum))
}
if len(valueRange) > 0 {
desc.WriteString(fmt.Sprintf(" Value range [%s].", strings.Join(valueRange, ", ")))
}
return desc.String()
}
func registerFlagCompFunc(cmd *cobra.Command, name string, s *spec.Schema) {
// register the enum entry for autocompletion
if len(s.Enum) > 0 {
var entries []string
for _, e := range s.Enum {
entries = append(entries, fmt.Sprintf("%s\t", e))
}
_ = cmd.RegisterFlagCompletionFunc(name,
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return entries, cobra.ShellCompDirectiveNoFileComp
})
return
}
}
// AddComponentFlag add flag "component" for cobra.Command and support auto complete for it
func AddComponentFlag(f cmdutil.Factory, cmd *cobra.Command, p *string, usage string) {
cmd.Flags().StringVar(p, "component", "", usage)
util.CheckErr(autoCompleteClusterComponent(cmd, f, "component"))
}
// AddComponentsFlag add flag "components" for cobra.Command and support auto complete for it
func AddComponentsFlag(f cmdutil.Factory, cmd *cobra.Command, p *[]string, usage string) {
cmd.Flags().StringSliceVar(p, "components", nil, usage)
util.CheckErr(autoCompleteClusterComponent(cmd, f, "components"))
}
func autoCompleteClusterComponent(cmd *cobra.Command, f cmdutil.Factory, flag string) error {
autoComplete := func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var components []string
if len(args) == 0 {
return components, cobra.ShellCompDirectiveNoFileComp
}
namespace, _, _ := f.ToRawKubeConfigLoader().Namespace()
dynamic, _ := f.DynamicClient()
cluster := &appsv1alpha1.Cluster{}
_ = util.GetK8SClientObject(dynamic, cluster, types.ClusterGVR(), namespace, util.GetClusterNameFromArgsOrFlag(cmd, args))
for _, comp := range cluster.Spec.ComponentSpecs {
if strings.HasPrefix(comp.Name, toComplete) {
components = append(components, comp.Name)
}
}
return components, cobra.ShellCompDirectiveNoFileComp
}
return cmd.RegisterFlagCompletionFunc(flag, autoComplete)
}