func (o *ViewServiceaccountKubeconfigOptions) Validate() error {
	if len(o.args) != 1 {
		return fmt.Errorf("exactly one SERVICEACCOUT is required, got %d", len(o.args))
	}
	return nil
}
Validate ensures that all required arguments and flag values are provided
package cmd
import (
	"context"
	"flag"
	"fmt"
	"strings"
	"github.com/spf13/cobra"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/genericclioptions"
	"k8s.io/cli-runtime/pkg/printers"
	"k8s.io/client-go/kubernetes"
	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
	"k8s.io/client-go/tools/clientcmd/api/latest"
	"k8s.io/kubectl/pkg/util/templates"
	_ "k8s.io/client-go/plugin/pkg/client/auth"
)
var (
	viewServiceaccountKubeconfigLong = templates.LongDesc(`
Show a kubeconfig setting to access the apiserver with a specified serviceaccount.
The kubeconfig setting will reference the serviceaccount token and use the
current server and namespace
`)
	viewServiceaccountKubeconfigExample = templates.Examples(`
# Show a kubeconfig setting of serviceaccount/default
kubectl view-serviceaccount-kubeconfig default
# Show a kubeconfig setting of serviceaccount/bot in namespace/kube-system
kubectl view-serviceaccount-kubeconfig bot -n kube-system
# Show a kubeconfig setting of serviceaccount/default in JSON format
kubectl view-serviceaccount-kubeconfig default -o json
`)
)
type ViewServiceaccountKubeconfigOptions struct {
	configFlags *genericclioptions.ConfigFlags
	printFlags  *genericclioptions.PrintFlags
	printObj    printers.ResourcePrinterFunc
	args []string
	genericclioptions.IOStreams
}
func NewViewServiceaccountKubeconfigOptions(streams genericclioptions.IOStreams) *ViewServiceaccountKubeconfigOptions {
	return &ViewServiceaccountKubeconfigOptions{
		configFlags: genericclioptions.NewConfigFlags(true),
		printFlags: (&genericclioptions.PrintFlags{
			JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
		}).WithDefaultOutput("yaml"),
		IOStreams: streams,
	}
}
func NewCmdViewServiceaccountKubeconfig(streams genericclioptions.IOStreams) *cobra.Command {
	o := NewViewServiceaccountKubeconfigOptions(streams)
	cmd := &cobra.Command{
		Use:     "kubectl view-serviceaccount-kubeconfig SERVICEACCOUNT [options]",
		Short:   "Show a kubeconfig setting to access the apiserver with a specified serviceaccount.",
		Long:    viewServiceaccountKubeconfigLong,
		Example: viewServiceaccountKubeconfigExample,
		RunE: func(c *cobra.Command, args []string) error {
			if err := o.Complete(c, args); err != nil {
				return err
			}
			if err := o.Validate(); err != nil {
				return err
			}
			c.SilenceUsage = true
			if err := o.Run(); err != nil {
				return err
			}
			return nil
		},
	}
	cmd.Flags().StringVarP(o.printFlags.OutputFormat, "output", "o", *o.printFlags.OutputFormat, fmt.Sprintf("Output format. One of : %s.", strings.Join(o.printFlags.AllowedFormats(), "|")))
	o.configFlags.AddFlags(cmd.Flags())
	cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
	_ = flag.CommandLine.Parse([]string{})
	return cmd
}
func (o *ViewServiceaccountKubeconfigOptions) Complete(cmd *cobra.Command, args []string) error {
	o.args = args
	printer, err := o.printFlags.ToPrinter()
	if err != nil {
		return err
	}
	o.printObj = printer.PrintObj
	return nil
}