@@ -59,74 +59,73 @@ var (
59
59
60
60
# Get the documentation of resources in different format
61
61
kubectl explain deployment --output=plaintext-openapiv2` ))
62
+ )
62
63
64
+ const (
63
65
plaintextTemplateName = "plaintext"
64
66
plaintextOpenAPIV2TemplateName = "plaintext-openapiv2"
65
67
)
66
68
69
+ // ExplainFlags directly reflect the information that CLI is gathering via flags.
70
+ // They will be converted to Options, which reflect the runtime requirements for
71
+ // the command.
67
72
type ExplainFlags struct {
68
- APIVersion string
69
- // Name of the template to use with the openapiv3 template renderer. If
70
- // `EnableOpenAPIV3` is disabled, this does nothing
73
+ APIVersion string
71
74
OutputFormat string
72
75
Recursive bool
73
- genericclioptions.IOStreams
74
- }
75
76
76
- // AddFlags registers flags for a cli
77
- func (flags * ExplainFlags ) AddFlags (cmd * cobra.Command ) {
78
- cmd .Flags ().BoolVar (& flags .Recursive , "recursive" , flags .Recursive , "Print the fields of fields (Currently only 1 level deep)" )
79
- cmd .Flags ().StringVar (& flags .APIVersion , "api-version" , flags .APIVersion , "Get different explanations for particular API version (API group/version)" )
80
-
81
- // Only enable --output as a valid flag if the feature is enabled
82
- cmd .Flags ().StringVar (& flags .OutputFormat , "output" , plaintextTemplateName , "Format in which to render the schema (plaintext, plaintext-openapiv2)" )
77
+ genericiooptions.IOStreams
83
78
}
84
79
85
80
// NewExplainFlags returns a default ExplainFlags
86
- func NewExplainFlags (streams genericclioptions .IOStreams ) * ExplainFlags {
81
+ func NewExplainFlags (streams genericiooptions .IOStreams ) * ExplainFlags {
87
82
return & ExplainFlags {
88
83
OutputFormat : plaintextTemplateName ,
89
84
IOStreams : streams ,
90
85
}
91
86
}
92
87
88
+ // AddFlags registers flags for a cli
89
+ func (flags * ExplainFlags ) AddFlags (cmd * cobra.Command ) {
90
+ cmd .Flags ().BoolVar (& flags .Recursive , "recursive" , flags .Recursive , "Print the fields of fields (Currently only 1 level deep)" )
91
+ cmd .Flags ().StringVar (& flags .APIVersion , "api-version" , flags .APIVersion , "Get different explanations for particular API version (API group/version)" )
92
+ cmd .Flags ().StringVar (& flags .OutputFormat , "output" , plaintextTemplateName , "Format in which to render the schema (plaintext, plaintext-openapiv2)" )
93
+ }
94
+
93
95
// ToOptions converts from CLI inputs to runtime input
94
96
func (flags * ExplainFlags ) ToOptions (f cmdutil.Factory , parent string , args []string ) (* ExplainOptions , error ) {
95
97
mapper , err := f .ToRESTMapper ()
96
98
if err != nil {
97
99
return nil , err
98
100
}
99
101
100
- schema , err := f .OpenAPISchema ()
101
- if err != nil {
102
- return nil , err
103
- }
104
-
105
102
// Only openapi v3 needs the discovery client.
106
103
openAPIV3Client , err := f .OpenAPIV3Client ()
107
104
if err != nil {
108
105
return nil , err
109
106
}
110
107
111
108
o := & ExplainOptions {
112
- CmdParent : parent ,
113
- Mapper : mapper ,
114
- Schema : schema ,
115
- args : args ,
116
- IOStreams : flags .IOStreams ,
117
- Recursive : flags .Recursive ,
118
- APIVersion : flags .APIVersion ,
119
- OutputFormat : plaintextTemplateName ,
109
+ IOStreams : flags .IOStreams ,
110
+
111
+ Recursive : flags .Recursive ,
112
+ APIVersion : flags .APIVersion ,
113
+ OutputFormat : flags .OutputFormat ,
114
+
115
+ CmdParent : parent ,
116
+ args : args ,
117
+
118
+ Mapper : mapper ,
119
+ openAPIGetter : f ,
120
+
120
121
OpenAPIV3Client : openAPIV3Client ,
121
122
}
122
123
123
124
return o , nil
124
125
}
125
126
126
127
// NewCmdExplain returns a cobra command for swagger docs
127
- func NewCmdExplain (parent string , f cmdutil.Factory , streams genericclioptions.IOStreams ) * cobra.Command {
128
- // o := NewExplainOptions(parent, streams)
129
-
128
+ func NewCmdExplain (parent string , f cmdutil.Factory , streams genericiooptions.IOStreams ) * cobra.Command {
130
129
flags := NewExplainFlags (streams )
131
130
132
131
cmd := & cobra.Command {
@@ -148,6 +147,24 @@ func NewCmdExplain(parent string, f cmdutil.Factory, streams genericclioptions.I
148
147
return cmd
149
148
}
150
149
150
+ type ExplainOptions struct {
151
+ genericiooptions.IOStreams
152
+
153
+ Recursive bool
154
+ APIVersion string
155
+ // Name of the template to use with the openapiv3 template renderer.
156
+ OutputFormat string
157
+
158
+ CmdParent string
159
+ args []string
160
+
161
+ Mapper meta.RESTMapper
162
+ openAPIGetter openapi.OpenAPIResourcesGetter
163
+
164
+ // Client capable of fetching openapi documents from the user's cluster
165
+ OpenAPIV3Client openapiclient.Client
166
+ }
167
+
151
168
func (o * ExplainOptions ) Validate () error {
152
169
if len (o .args ) == 0 {
153
170
return fmt .Errorf ("You must specify the type of resource to explain. %s\n " , cmdutil .SuggestAPIResources (o .CmdParent ))
@@ -245,23 +262,3 @@ func (o *ExplainOptions) renderOpenAPIV2(
245
262
246
263
return explain .PrintModelDescription (fieldsPath , o .Out , schema , gvk , o .Recursive )
247
264
}
248
-
249
- type ExplainOptions struct {
250
- genericclioptions.IOStreams
251
-
252
- CmdParent string
253
- APIVersion string
254
- Recursive bool
255
-
256
- args []string
257
-
258
- Mapper meta.RESTMapper
259
- Schema openapi.Resources
260
-
261
- // Name of the template to use with the openapiv3 template renderer. If
262
- // `EnableOpenAPIV3` is disabled, this does nothing
263
- OutputFormat string
264
-
265
- // Client capable of fetching openapi documents from the user's cluster
266
- OpenAPIV3Client openapiclient.Client
267
- }
0 commit comments