Skip to content

Commit 790d0f4

Browse files
Christian Hernandezchristianh814
authored andcommitted
Added the ability to change the output to YAML or JSON
Signed-off-by: Christian Hernandez <[email protected]> changing to output of json or yaml Signed-off-by: Christian Hernandez <[email protected]> updated to use output of yaml or json Signed-off-by: Christian Hernandez <[email protected]> added the GVK to the output Signed-off-by: Christian Hernandez <[email protected]>
1 parent 9c5b183 commit 790d0f4

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

pkg/cli/whoami/whoami.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package whoami
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
"github.com/spf13/cobra"
@@ -17,6 +18,7 @@ import (
1718

1819
userv1 "github.com/openshift/api/user/v1"
1920
userv1typedclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
21+
"sigs.k8s.io/yaml"
2022
)
2123

2224
const (
@@ -47,6 +49,8 @@ type WhoAmIOptions struct {
4749
ShowContext bool
4850
ShowServer bool
4951
ShowConsoleUrl bool
52+
ShowGroups bool
53+
Output string
5054

5155
genericclioptions.IOStreams
5256
}
@@ -76,6 +80,7 @@ func NewCmdWhoAmI(f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobr
7680
cmd.Flags().BoolVarP(&o.ShowContext, "show-context", "c", o.ShowContext, "Print the current user context name")
7781
cmd.Flags().BoolVar(&o.ShowServer, "show-server", o.ShowServer, "If true, print the current server's REST API URL")
7882
cmd.Flags().BoolVar(&o.ShowConsoleUrl, "show-console", o.ShowConsoleUrl, "If true, print the current server's web console URL")
83+
cmd.Flags().StringVarP(&o.Output, "output", "o", o.Output, "One of 'yaml' or 'json'.")
7984

8085
return cmd
8186
}
@@ -136,6 +141,12 @@ func (o *WhoAmIOptions) getWebConsoleUrl() (string, error) {
136141
}
137142

138143
func (o *WhoAmIOptions) Run() error {
144+
var err error
145+
o.UserInterface, err = userv1typedclient.NewForConfig(o.ClientConfig)
146+
if err != nil {
147+
return err
148+
}
149+
139150
switch {
140151
case o.ShowToken:
141152
fmt.Fprintf(o.Out, "%s\n", o.ClientConfig.BearerToken)
@@ -153,14 +164,34 @@ func (o *WhoAmIOptions) Run() error {
153164
}
154165
fmt.Fprintf(o.Out, "%s\n", consoleUrl)
155166
return nil
156-
}
167+
case o.Output == "yaml":
168+
u, err := o.UserInterface.Users().Get(context.TODO(), "~", metav1.GetOptions{})
169+
u.SetGroupVersionKind(userv1.GroupVersion.WithKind("User"))
170+
if err != nil {
171+
return err
172+
}
157173

158-
var err error
159-
o.UserInterface, err = userv1typedclient.NewForConfig(o.ClientConfig)
160-
if err != nil {
161-
return err
174+
y, err := yaml.Marshal(u)
175+
if err != nil {
176+
return err
177+
}
178+
fmt.Fprintf(o.Out, "%s\n", string(y))
179+
return nil
180+
case o.Output == "json":
181+
u, err := o.UserInterface.Users().Get(context.TODO(), "~", metav1.GetOptions{})
182+
u.SetGroupVersionKind(userv1.GroupVersion.WithKind("User"))
183+
if err != nil {
184+
return err
185+
}
186+
j, err := json.MarshalIndent(u, "", " ")
187+
if err != nil {
188+
return err
189+
}
190+
fmt.Fprintf(o.Out, "%s\n", string(j))
191+
return nil
162192
}
163193

164194
_, err = o.WhoAmI()
195+
165196
return err
166197
}

0 commit comments

Comments
 (0)