-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_user_list.go
49 lines (46 loc) · 967 Bytes
/
cmd_user_list.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
package main
import (
"fmt"
"github.com/folded-ear/datawell/model"
"log"
"strconv"
)
var (
userListCmd = &Command{
Name: "user-list",
Usage: "",
Summary: "list all users in the system",
Run: userListRun,
}
)
func userListRun(cmd *Command, args ...string) {
users := []model.User{}
db, err := model.Gorm()
if err != nil {
log.Fatal(err)
}
err = db.Order("name").Order("username").Find(&users).Error
if err != nil {
log.Fatal(err)
}
idLen, nameLen, unLen := 2, 4, 8
for _, u := range users {
l := len(strconv.FormatInt(u.ID, 10))
if l > idLen {
idLen = l
}
l = len(u.Name)
if l > nameLen {
nameLen = l
}
l = len(u.Username)
if l > unLen {
unLen = l
}
}
fmt.Printf("| %*s | %-*s | %-*s |\n", idLen, "id", unLen, "username", nameLen, "name")
for _, u := range users {
fmt.Printf("| %*d | %-*s | %-*s |\n", idLen, u.ID, unLen, u.Username, nameLen, u.Name)
}
fmt.Printf("%d user(s)\n", len(users))
}