This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
/
userList.go
68 lines (59 loc) · 1.6 KB
/
userList.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
package cmd
import (
api "github.com/32leaves/ruruku/pkg/api/v1"
"github.com/32leaves/ruruku/pkg/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type userListOutput struct {
Name string `json:"name"`
Email string `json:"email"`
Permissions []types.Permission `json:"permissions"`
}
// userListCmd represents the sessionClose command
var userListCmd = &cobra.Command{
Use: "list",
Short: "Lists all users in the system (requires user.list permission)",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := GetConfigFromViper()
if err != nil {
log.Fatalf("Error while loading the configuration: %v", err)
}
conn, err := cfg.Connect()
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := api.NewUserServiceClient(conn)
ctx, cancel := cfg.GetContext(true)
defer cancel()
resp, err := client.List(ctx, &api.ListUsersRequest{})
if err != nil {
log.WithError(err).Fatal()
}
out := make([]userListOutput, len(resp.User))
for idx, u := range resp.User {
perms := make([]types.Permission, len(u.Permission))
for jdx, p := range u.Permission {
perms[jdx] = p.Convert()
}
out[idx] = userListOutput{
Name: u.Name,
Email: u.Email,
Permissions: perms,
}
}
tpl := `NAME EMAIL PERMISSION
{{- range . }}
{{ .Name }} {{ .Email }} {{ .Permissions -}}
{{ end }}
`
ctnt := remoteCmdValues.GetOutputFormat(out, tpl, `{..name}`)
if err := ctnt.Print(); err != nil {
log.WithError(err).Fatal()
}
},
}
func init() {
userCmd.AddCommand(userListCmd)
}