Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions pkg/cli/whoami/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package whoami

import (
"context"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -20,6 +21,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 (
Expand Down Expand Up @@ -51,6 +53,8 @@ type WhoAmIOptions struct {
ShowContext bool
ShowServer bool
ShowConsoleUrl bool
ShowGroups bool
Output string

genericiooptions.IOStreams
}
Expand Down Expand Up @@ -80,6 +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'.")

return cmd
}
Expand Down Expand Up @@ -154,6 +159,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)
Expand All @@ -171,12 +182,31 @@ 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
}

o.AuthV1Client, err = authenticationv1client.NewForConfig(o.ClientConfig)
Expand All @@ -185,5 +215,6 @@ func (o *WhoAmIOptions) Run() error {
}

_, err = o.WhoAmI()

return err
}