forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
whoami.go
109 lines (90 loc) · 2.87 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package cmd
import (
"fmt"
"io"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
userapi "github.com/openshift/origin/pkg/user/apis/user"
userclientinternal "github.com/openshift/origin/pkg/user/generated/internalclientset"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
)
const WhoAmIRecommendedCommandName = "whoami"
var whoamiLong = templates.LongDesc(`
Show information about the current session
The default options for this command will return the currently authenticated user name
or an empty string. Other flags support returning the currently used token or the
user context.`)
type WhoAmIOptions struct {
UserInterface userclient.UserResourceInterface
Out io.Writer
}
func NewCmdWhoAmI(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
o := &WhoAmIOptions{}
cmd := &cobra.Command{
Use: name,
Short: "Return information about the current session",
Long: whoamiLong,
Run: func(cmd *cobra.Command, args []string) {
err := RunWhoAmI(f, out, cmd, args, o)
kcmdutil.CheckErr(err)
},
}
cmd.Flags().BoolP("show-token", "t", false, "Print the token the current session is using. This will return an error if you are using a different form of authentication.")
cmd.Flags().BoolP("show-context", "c", false, "Print the current user context name")
cmd.Flags().Bool("show-server", false, "If true, print the current server's REST API URL")
return cmd
}
func (o WhoAmIOptions) WhoAmI() (*userapi.User, error) {
me, err := o.UserInterface.Get("~", metav1.GetOptions{})
if err == nil {
fmt.Fprintf(o.Out, "%s\n", me.Name)
}
return me, err
}
func RunWhoAmI(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string, o *WhoAmIOptions) error {
if kcmdutil.GetFlagBool(cmd, "show-token") {
cfg, err := f.ClientConfig()
if err != nil {
return err
}
if len(cfg.BearerToken) == 0 {
return fmt.Errorf("no token is currently in use for this session")
}
fmt.Fprintf(out, "%s\n", cfg.BearerToken)
return nil
}
if kcmdutil.GetFlagBool(cmd, "show-context") {
cfg, err := f.RawConfig()
if err != nil {
return err
}
if len(cfg.CurrentContext) == 0 {
return fmt.Errorf("no context has been set")
}
fmt.Fprintf(out, "%s\n", cfg.CurrentContext)
return nil
}
if kcmdutil.GetFlagBool(cmd, "show-server") {
cfg, err := f.ClientConfig()
if err != nil {
return err
}
fmt.Fprintf(out, "%s\n", cfg.Host)
return nil
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
client, err := userclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
o.UserInterface = client.User().Users()
o.Out = out
_, err = o.WhoAmI()
return err
}