Skip to content

Commit

Permalink
Finish extracting ExplainFlags structure
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Szulik <soltysh@gmail.com>

Kubernetes-commit: 3030b1dc6a445929c20dc911196746c2d8af2bac
  • Loading branch information
soltysh authored and k8s-publishing-bot committed Jan 23, 2025
1 parent bcafb59 commit 1ab40ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 51 deletions.
95 changes: 46 additions & 49 deletions pkg/cmd/explain/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,74 +59,73 @@ var (
# Get the documentation of resources in different format
kubectl explain deployment --output=plaintext-openapiv2`))
)

const (
plaintextTemplateName = "plaintext"
plaintextOpenAPIV2TemplateName = "plaintext-openapiv2"
)

// ExplainFlags directly reflect the information that CLI is gathering via flags.
// They will be converted to Options, which reflect the runtime requirements for
// the command.
type ExplainFlags struct {
APIVersion string
// Name of the template to use with the openapiv3 template renderer. If
// `EnableOpenAPIV3` is disabled, this does nothing
APIVersion string
OutputFormat string
Recursive bool
genericclioptions.IOStreams
}

// AddFlags registers flags for a cli
func (flags *ExplainFlags) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&flags.Recursive, "recursive", flags.Recursive, "Print the fields of fields (Currently only 1 level deep)")
cmd.Flags().StringVar(&flags.APIVersion, "api-version", flags.APIVersion, "Get different explanations for particular API version (API group/version)")

// Only enable --output as a valid flag if the feature is enabled
cmd.Flags().StringVar(&flags.OutputFormat, "output", plaintextTemplateName, "Format in which to render the schema (plaintext, plaintext-openapiv2)")
genericiooptions.IOStreams
}

// NewExplainFlags returns a default ExplainFlags
func NewExplainFlags(streams genericclioptions.IOStreams) *ExplainFlags {
func NewExplainFlags(streams genericiooptions.IOStreams) *ExplainFlags {
return &ExplainFlags{
OutputFormat: plaintextTemplateName,
IOStreams: streams,
}
}

// AddFlags registers flags for a cli
func (flags *ExplainFlags) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&flags.Recursive, "recursive", flags.Recursive, "Print the fields of fields (Currently only 1 level deep)")
cmd.Flags().StringVar(&flags.APIVersion, "api-version", flags.APIVersion, "Get different explanations for particular API version (API group/version)")
cmd.Flags().StringVar(&flags.OutputFormat, "output", plaintextTemplateName, "Format in which to render the schema (plaintext, plaintext-openapiv2)")
}

// ToOptions converts from CLI inputs to runtime input
func (flags *ExplainFlags) ToOptions(f cmdutil.Factory, parent string, args []string) (*ExplainOptions, error) {
mapper, err := f.ToRESTMapper()
if err != nil {
return nil, err
}

schema, err := f.OpenAPISchema()
if err != nil {
return nil, err
}

// Only openapi v3 needs the discovery client.
openAPIV3Client, err := f.OpenAPIV3Client()
if err != nil {
return nil, err
}

o := &ExplainOptions{
CmdParent: parent,
Mapper: mapper,
Schema: schema,
args: args,
IOStreams: flags.IOStreams,
Recursive: flags.Recursive,
APIVersion: flags.APIVersion,
OutputFormat: plaintextTemplateName,
IOStreams: flags.IOStreams,

Recursive: flags.Recursive,
APIVersion: flags.APIVersion,
OutputFormat: flags.OutputFormat,

CmdParent: parent,
args: args,

Mapper: mapper,
openAPIGetter: f,

OpenAPIV3Client: openAPIV3Client,
}

return o, nil
}

// NewCmdExplain returns a cobra command for swagger docs
func NewCmdExplain(parent string, f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
// o := NewExplainOptions(parent, streams)

func NewCmdExplain(parent string, f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
flags := NewExplainFlags(streams)

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

type ExplainOptions struct {
genericiooptions.IOStreams

Recursive bool
APIVersion string
// Name of the template to use with the openapiv3 template renderer.
OutputFormat string

CmdParent string
args []string

Mapper meta.RESTMapper
openAPIGetter openapi.OpenAPIResourcesGetter

// Client capable of fetching openapi documents from the user's cluster
OpenAPIV3Client openapiclient.Client
}

func (o *ExplainOptions) Validate() error {
if len(o.args) == 0 {
return fmt.Errorf("You must specify the type of resource to explain. %s\n", cmdutil.SuggestAPIResources(o.CmdParent))
Expand Down Expand Up @@ -245,23 +262,3 @@ func (o *ExplainOptions) renderOpenAPIV2(

return explain.PrintModelDescription(fieldsPath, o.Out, schema, gvk, o.Recursive)
}

type ExplainOptions struct {
genericclioptions.IOStreams

CmdParent string
APIVersion string
Recursive bool

args []string

Mapper meta.RESTMapper
Schema openapi.Resources

// Name of the template to use with the openapiv3 template renderer. If
// `EnableOpenAPIV3` is disabled, this does nothing
OutputFormat string

// Client capable of fetching openapi documents from the user's cluster
OpenAPIV3Client openapiclient.Client
}
4 changes: 2 additions & 2 deletions pkg/cmd/explain/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestExplainInvalidArgs(t *testing.T) {
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()

flags := explain.NewExplainFlags(genericclioptions.NewTestIOStreamsDiscard())
flags := explain.NewExplainFlags(genericiooptions.NewTestIOStreamsDiscard())

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

flags := explain.NewExplainFlags(genericclioptions.NewTestIOStreamsDiscard())
flags := explain.NewExplainFlags(genericiooptions.NewTestIOStreamsDiscard())

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

0 comments on commit 1ab40ed

Please sign in to comment.