Skip to content

Commit 821d8e3

Browse files
author
Yohann Delafollye
committed
Improve users management through the CLI (#6001)
- add user subcommand - add list, create, delete, change-password microcommands - delete create-user and change-password subcommands
1 parent 7ffc242 commit 821d8e3

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

cmd/admin.go

+84-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,38 @@ var (
2929
Name: "admin",
3030
Usage: "Command line interface to perform common administrative operations",
3131
Subcommands: []cli.Command{
32-
subcmdCreateUser,
33-
subcmdChangePassword,
32+
subcmdUser,
3433
subcmdRepoSyncReleases,
3534
subcmdRegenerate,
3635
subcmdAuth,
3736
},
3837
}
3938

40-
subcmdCreateUser = cli.Command{
41-
Name: "create-user",
39+
subcmdUser = cli.Command{
40+
Name: "user",
41+
Usage: "Modify users",
42+
Subcommands: []cli.Command{
43+
microcmdUserCreate,
44+
microcmdUserList,
45+
microcmdUserChangePassword,
46+
microcmdUserDelete,
47+
},
48+
}
49+
50+
microcmdUserList = cli.Command{
51+
Name: "list",
52+
Usage: "List users",
53+
Action: runListUsers,
54+
Flags: []cli.Flag{
55+
cli.BoolFlag{
56+
Name: "admin",
57+
Usage: "List only admin users",
58+
},
59+
},
60+
}
61+
62+
microcmdUserCreate = cli.Command{
63+
Name: "create",
4264
Usage: "Create a new user in database",
4365
Action: runCreateUser,
4466
Flags: []cli.Flag{
@@ -82,7 +104,7 @@ var (
82104
},
83105
}
84106

85-
subcmdChangePassword = cli.Command{
107+
microcmdUserChangePassword = cli.Command{
86108
Name: "change-password",
87109
Usage: "Change a user's password",
88110
Action: runChangePassword,
@@ -100,6 +122,13 @@ var (
100122
},
101123
}
102124

125+
microcmdUserDelete = cli.Command{
126+
Name: "delete",
127+
Usage: "Delete specific user",
128+
Flags: []cli.Flag{idFlag},
129+
Action: runDeleteUser,
130+
}
131+
103132
subcmdRepoSyncReleases = cli.Command{
104133
Name: "repo-sync-releases",
105134
Usage: "Synchronize repository releases with tags",
@@ -343,6 +372,56 @@ func runCreateUser(c *cli.Context) error {
343372
return nil
344373
}
345374

375+
func runListUsers(c *cli.Context) error {
376+
if err := initDB(); err != nil {
377+
return err
378+
}
379+
380+
users, err := models.ListUsers()
381+
382+
if err != nil {
383+
return err
384+
}
385+
386+
w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
387+
388+
if c.IsSet("admin") {
389+
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
390+
for _, u := range users {
391+
if u.IsAdmin {
392+
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
393+
}
394+
}
395+
} else {
396+
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n")
397+
for _, u := range users {
398+
fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsAdmin, u.IsActive)
399+
}
400+
401+
}
402+
403+
w.Flush()
404+
return nil
405+
406+
}
407+
408+
func runDeleteUser(c *cli.Context) error {
409+
if !c.IsSet("id") {
410+
return fmt.Errorf("--id flag is missing")
411+
}
412+
413+
if err := initDB(); err != nil {
414+
return err
415+
}
416+
417+
user, err := models.GetUserByID(c.Int64("id"))
418+
if err != nil {
419+
return err
420+
}
421+
422+
return models.DeleteUser(user)
423+
}
424+
346425
func runRepoSyncReleases(c *cli.Context) error {
347426
if err := initDB(); err != nil {
348427
return err

models/user.go

+6
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ func (u *User) GetEmail() string {
236236
return u.Email
237237
}
238238

239+
// ListUsers returns a slice of all users found in DB.
240+
func ListUsers() ([]*User, error) {
241+
users := make([]*User, 0)
242+
return users, x.Find(&users)
243+
}
244+
239245
// APIFormat converts a User to api.User
240246
func (u *User) APIFormat() *api.User {
241247
if u == nil {

0 commit comments

Comments
 (0)