From 790d0f46440b695a34668f83e0642e76bc85d2f9 Mon Sep 17 00:00:00 2001 From: Christian Hernandez Date: Wed, 18 Jan 2023 16:15:15 -0800 Subject: [PATCH 1/2] Added the ability to change the output to YAML or JSON Signed-off-by: Christian Hernandez changing to output of json or yaml Signed-off-by: Christian Hernandez updated to use output of yaml or json Signed-off-by: Christian Hernandez added the GVK to the output Signed-off-by: Christian Hernandez --- pkg/cli/whoami/whoami.go | 41 +++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/cli/whoami/whoami.go b/pkg/cli/whoami/whoami.go index 3c03a83e20..3919650825 100644 --- a/pkg/cli/whoami/whoami.go +++ b/pkg/cli/whoami/whoami.go @@ -2,6 +2,7 @@ package whoami import ( "context" + "encoding/json" "fmt" "github.com/spf13/cobra" @@ -17,6 +18,7 @@ import ( userv1 "github.com/openshift/api/user/v1" userv1typedclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1" + "sigs.k8s.io/yaml" ) const ( @@ -47,6 +49,8 @@ type WhoAmIOptions struct { ShowContext bool ShowServer bool ShowConsoleUrl bool + ShowGroups bool + Output string genericclioptions.IOStreams } @@ -76,6 +80,7 @@ func NewCmdWhoAmI(f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobr cmd.Flags().BoolVarP(&o.ShowContext, "show-context", "c", o.ShowContext, "Print the current user context name") cmd.Flags().BoolVar(&o.ShowServer, "show-server", o.ShowServer, "If true, print the current server's REST API URL") cmd.Flags().BoolVar(&o.ShowConsoleUrl, "show-console", o.ShowConsoleUrl, "If true, print the current server's web console URL") + cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.") return cmd } @@ -136,6 +141,12 @@ func (o *WhoAmIOptions) getWebConsoleUrl() (string, error) { } func (o *WhoAmIOptions) Run() error { + var err error + o.UserInterface, err = userv1typedclient.NewForConfig(o.ClientConfig) + if err != nil { + return err + } + switch { case o.ShowToken: fmt.Fprintf(o.Out, "%s\n", o.ClientConfig.BearerToken) @@ -153,14 +164,34 @@ func (o *WhoAmIOptions) Run() error { } fmt.Fprintf(o.Out, "%s\n", consoleUrl) return nil - } + case o.Output == "yaml": + u, err := o.UserInterface.Users().Get(context.TODO(), "~", metav1.GetOptions{}) + u.SetGroupVersionKind(userv1.GroupVersion.WithKind("User")) + if err != nil { + return err + } - var err error - o.UserInterface, err = userv1typedclient.NewForConfig(o.ClientConfig) - if err != nil { - return err + y, err := yaml.Marshal(u) + if err != nil { + return err + } + fmt.Fprintf(o.Out, "%s\n", string(y)) + return nil + case o.Output == "json": + u, err := o.UserInterface.Users().Get(context.TODO(), "~", metav1.GetOptions{}) + u.SetGroupVersionKind(userv1.GroupVersion.WithKind("User")) + if err != nil { + return err + } + j, err := json.MarshalIndent(u, "", " ") + if err != nil { + return err + } + fmt.Fprintf(o.Out, "%s\n", string(j)) + return nil } _, err = o.WhoAmI() + return err } From 71ba17f816ff51b705b013fced4821d687a0b6dd Mon Sep 17 00:00:00 2001 From: Christian Hernandez Date: Mon, 20 Oct 2025 17:25:33 -0700 Subject: [PATCH 2/2] resolved comments Signed-off-by: Christian Hernandez --- pkg/cli/whoami/whoami.go | 88 ++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/pkg/cli/whoami/whoami.go b/pkg/cli/whoami/whoami.go index e7776b9dea..28144284fc 100644 --- a/pkg/cli/whoami/whoami.go +++ b/pkg/cli/whoami/whoami.go @@ -2,7 +2,6 @@ package whoami import ( "context" - "encoding/json" "fmt" "github.com/spf13/cobra" @@ -10,7 +9,8 @@ import ( v1 "k8s.io/api/authentication/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/cli-runtime/pkg/genericiooptions" + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/cli-runtime/pkg/printers" "k8s.io/client-go/kubernetes" authenticationv1client "k8s.io/client-go/kubernetes/typed/authentication/v1" "k8s.io/client-go/rest" @@ -21,7 +21,6 @@ import ( userv1 "github.com/openshift/api/user/v1" userv1typedclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1" - "sigs.k8s.io/yaml" ) const ( @@ -49,23 +48,24 @@ type WhoAmIOptions struct { KubeClient kubernetes.Interface RawConfig api.Config - ShowToken bool - ShowContext bool - ShowServer bool - ShowConsoleUrl bool - ShowGroups bool - Output string + ShowToken bool + ShowContext bool + ShowServer bool + ShowConsoleUrl bool + PrintFlags *genericclioptions.PrintFlags + resourcePrinterFunc printers.ResourcePrinterFunc - genericiooptions.IOStreams + genericclioptions.IOStreams } -func NewWhoAmIOptions(streams genericiooptions.IOStreams) *WhoAmIOptions { +func NewWhoAmIOptions(streams genericclioptions.IOStreams) *WhoAmIOptions { return &WhoAmIOptions{ - IOStreams: streams, + PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(""), + IOStreams: streams, } } -func NewCmdWhoAmI(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { +func NewCmdWhoAmI(f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { o := NewWhoAmIOptions(streams) cmd := &cobra.Command{ @@ -84,7 +84,7 @@ func NewCmdWhoAmI(f kcmdutil.Factory, streams genericiooptions.IOStreams) *cobra cmd.Flags().BoolVarP(&o.ShowContext, "show-context", "c", o.ShowContext, "Print the current user context name") cmd.Flags().BoolVar(&o.ShowServer, "show-server", o.ShowServer, "If true, print the current server's REST API URL") cmd.Flags().BoolVar(&o.ShowConsoleUrl, "show-console", o.ShowConsoleUrl, "If true, print the current server's web console URL") - cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.") + o.PrintFlags.AddFlags(cmd) return cmd } @@ -127,10 +127,28 @@ func (o *WhoAmIOptions) Complete(f kcmdutil.Factory) error { o.KubeClient = kubeClient o.RawConfig, err = f.ToRawKubeConfigLoader().RawConfig() - return err + if err != nil { + return err + } + + // Setup printer function + if o.PrintFlags.OutputFlagSpecified() { + printer, err := o.PrintFlags.ToPrinter() + if err != nil { + return err + } + o.resourcePrinterFunc = printer.PrintObj + } + + return nil } func (o *WhoAmIOptions) Validate() error { + // Check if output flag is used with other show flags + if o.PrintFlags.OutputFlagSpecified() && (o.ShowToken || o.ShowContext || o.ShowServer || o.ShowConsoleUrl) { + return fmt.Errorf("--output cannot be used with --show-token, --show-context, --show-server, or --show-console") + } + if o.ShowToken && len(o.ClientConfig.BearerToken) == 0 { return fmt.Errorf("no token is currently in use for this session") } @@ -165,6 +183,11 @@ func (o *WhoAmIOptions) Run() error { return err } + o.AuthV1Client, err = authenticationv1client.NewForConfig(o.ClientConfig) + if err != nil { + return err + } + switch { case o.ShowToken: fmt.Fprintf(o.Out, "%s\n", o.ClientConfig.BearerToken) @@ -182,39 +205,18 @@ func (o *WhoAmIOptions) Run() error { } fmt.Fprintf(o.Out, "%s\n", consoleUrl) return nil - case o.Output == "yaml": - u, err := o.UserInterface.Users().Get(context.TODO(), "~", metav1.GetOptions{}) - u.SetGroupVersionKind(userv1.GroupVersion.WithKind("User")) + case o.resourcePrinterFunc != nil: + // If output format is specified, get the user info and print it in the requested format + me, err := o.WhoAmI() if err != nil { return err } - - y, err := yaml.Marshal(u) - if err != nil { - return err - } - fmt.Fprintf(o.Out, "%s\n", string(y)) - return nil - case o.Output == "json": - u, err := o.UserInterface.Users().Get(context.TODO(), "~", metav1.GetOptions{}) - u.SetGroupVersionKind(userv1.GroupVersion.WithKind("User")) - if err != nil { - return err - } - j, err := json.MarshalIndent(u, "", " ") - if err != nil { - return err - } - fmt.Fprintf(o.Out, "%s\n", string(j)) - return nil - } - - o.AuthV1Client, err = authenticationv1client.NewForConfig(o.ClientConfig) - if err != nil { - return err + // Set GroupVersionKind so it's properly displayed + me.SetGroupVersionKind(userv1.GroupVersion.WithKind("User")) + return o.resourcePrinterFunc(me, o.Out) } + // Default behavior: just print the username _, err = o.WhoAmI() - return err }