-
Notifications
You must be signed in to change notification settings - Fork 786
/
get_token.go
80 lines (69 loc) · 1.79 KB
/
get_token.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
package cmd
import (
"github.com/jenkins-x/jx/pkg/auth"
"github.com/spf13/cobra"
)
// GetTokenOptions the command line options
type GetTokenOptions struct {
GetOptions
Kind string
Name string
}
// NewCmdGetToken creates the command
func NewCmdGetToken(commonOpts *CommonOptions) *cobra.Command {
options := &GetTokenOptions{
GetOptions: GetOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "token",
Short: "Display the tokens for different kinds of services",
Aliases: []string{"api-token"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.AddCommand(NewCmdGetTokenAddon(commonOpts))
return cmd
}
func (o *GetTokenOptions) addFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Kind, "kind", "k", "", "Filters the services by the kind")
cmd.Flags().StringVarP(&o.Name, "name", "n", "", "Filters the services by the name")
}
// Run implements this command
func (o *GetTokenOptions) Run() error {
return o.Cmd.Help()
}
func (o *GetTokenOptions) displayUsersWithTokens(authConfigSvc auth.ConfigService) error {
config := authConfigSvc.Config()
filterKind := o.Kind
filterName := o.Name
table := o.createTable()
table.AddRow("KIND", "NAME", "URL", "USERNAME", "TOKEN?")
for _, s := range config.Servers {
kind := s.Kind
name := s.Name
if (filterKind == "" || filterKind == kind) && (filterName == "" || filterName == name) {
user := ""
pwd := ""
if len(s.Users) == 0 {
table.AddRow(kind, name, kind, s.URL, user, pwd)
} else {
for _, u := range s.Users {
user = u.Username
pwd = ""
if u.ApiToken != "" {
pwd = "yes"
}
}
table.AddRow(kind, name, s.URL, user, pwd)
}
}
}
table.Render()
return nil
}