Skip to content

Commit

Permalink
changing to output of json or yaml
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hernandez <[email protected]>
  • Loading branch information
christianh814 committed Jan 20, 2023
1 parent 4f67069 commit 83ede6c
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 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 @@ -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 (
Expand Down Expand Up @@ -48,6 +50,7 @@ type WhoAmIOptions struct {
ShowServer bool
ShowConsoleUrl bool
ShowGroups bool
Output string

genericclioptions.IOStreams
}
Expand Down Expand Up @@ -77,7 +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().BoolVarP(&o.ShowGroups, "show-groups", "g", o.ShowGroups, "Print the groups the current user belongs to.")
cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")

return cmd
}
Expand Down Expand Up @@ -155,6 +158,29 @@ func (o *WhoAmIOptions) Run() error {
}
fmt.Fprintf(o.Out, "%s\n", consoleUrl)
return nil
case o.Output == "yaml":
u, 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.WhoAmI()
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
}

var err error
Expand All @@ -163,14 +189,7 @@ func (o *WhoAmIOptions) Run() error {
return err
}

u, err := o.WhoAmI()

if o.ShowGroups && len(u.Groups) > 0 {
fmt.Fprintf(o.Out, "%s\n", "Groups:")
for _, group := range u.Groups {
fmt.Fprintf(o.Out, "- %s\n", group)
}
}
_, err = o.WhoAmI()

return err
}

0 comments on commit 83ede6c

Please sign in to comment.