forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whoami.go
52 lines (39 loc) · 1.11 KB
/
whoami.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package tokens
import (
"fmt"
"io"
"github.com/spf13/cobra"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
osclient "github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
userapi "github.com/openshift/origin/pkg/user/api"
)
const WhoAmIRecommendedCommandName = "whoami"
type WhoAmIOptions struct {
UserInterface osclient.UserInterface
Out io.Writer
}
func NewCmdWhoAmI(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
o := &WhoAmIOptions{}
cmd := &cobra.Command{
Use: name,
Short: "displays the username of the currently authenticated user",
Long: `displays the username of the currently authenticated user`,
Run: func(cmd *cobra.Command, args []string) {
client, _, err := f.Clients()
kcmdutil.CheckErr(err)
o.UserInterface = client.Users()
o.Out = out
_, err = o.WhoAmI()
kcmdutil.CheckErr(err)
},
}
return cmd
}
func (o WhoAmIOptions) WhoAmI() (*userapi.User, error) {
me, err := o.UserInterface.Get("~")
if err == nil {
fmt.Fprintf(o.Out, "%s\n", me.Name)
}
return me, err
}