Skip to content

Commit 3030b1d

Browse files
committed
Finish extracting ExplainFlags structure
Signed-off-by: Maciej Szulik <[email protected]>
1 parent 91afef6 commit 3030b1d

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/explain/explain.go

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -59,74 +59,73 @@ var (
5959
6060
# Get the documentation of resources in different format
6161
kubectl explain deployment --output=plaintext-openapiv2`))
62+
)
6263

64+
const (
6365
plaintextTemplateName = "plaintext"
6466
plaintextOpenAPIV2TemplateName = "plaintext-openapiv2"
6567
)
6668

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.
6772
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
7174
OutputFormat string
7275
Recursive bool
73-
genericclioptions.IOStreams
74-
}
7576

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
8378
}
8479

8580
// NewExplainFlags returns a default ExplainFlags
86-
func NewExplainFlags(streams genericclioptions.IOStreams) *ExplainFlags {
81+
func NewExplainFlags(streams genericiooptions.IOStreams) *ExplainFlags {
8782
return &ExplainFlags{
8883
OutputFormat: plaintextTemplateName,
8984
IOStreams: streams,
9085
}
9186
}
9287

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+
9395
// ToOptions converts from CLI inputs to runtime input
9496
func (flags *ExplainFlags) ToOptions(f cmdutil.Factory, parent string, args []string) (*ExplainOptions, error) {
9597
mapper, err := f.ToRESTMapper()
9698
if err != nil {
9799
return nil, err
98100
}
99101

100-
schema, err := f.OpenAPISchema()
101-
if err != nil {
102-
return nil, err
103-
}
104-
105102
// Only openapi v3 needs the discovery client.
106103
openAPIV3Client, err := f.OpenAPIV3Client()
107104
if err != nil {
108105
return nil, err
109106
}
110107

111108
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+
120121
OpenAPIV3Client: openAPIV3Client,
121122
}
122123

123124
return o, nil
124125
}
125126

126127
// 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 {
130129
flags := NewExplainFlags(streams)
131130

132131
cmd := &cobra.Command{
@@ -148,6 +147,24 @@ func NewCmdExplain(parent string, f cmdutil.Factory, streams genericclioptions.I
148147
return cmd
149148
}
150149

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+
151168
func (o *ExplainOptions) Validate() error {
152169
if len(o.args) == 0 {
153170
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(
245262

246263
return explain.PrintModelDescription(fieldsPath, o.Out, schema, gvk, o.Recursive)
247264
}
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-
}

staging/src/k8s.io/kubectl/pkg/cmd/explain/explain_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestExplainInvalidArgs(t *testing.T) {
5757
tf := cmdtesting.NewTestFactory()
5858
defer tf.Cleanup()
5959

60-
flags := explain.NewExplainFlags(genericclioptions.NewTestIOStreamsDiscard())
60+
flags := explain.NewExplainFlags(genericiooptions.NewTestIOStreamsDiscard())
6161

6262
opts, err := flags.ToOptions(tf, "kubectl", []string{})
6363
if err != nil {
@@ -84,7 +84,7 @@ func TestExplainNotExistResource(t *testing.T) {
8484
tf := cmdtesting.NewTestFactory()
8585
defer tf.Cleanup()
8686

87-
flags := explain.NewExplainFlags(genericclioptions.NewTestIOStreamsDiscard())
87+
flags := explain.NewExplainFlags(genericiooptions.NewTestIOStreamsDiscard())
8888

8989
opts, err := flags.ToOptions(tf, "kubectl", []string{"foo"})
9090
if err != nil {

0 commit comments

Comments
 (0)