-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_user_delete.go
62 lines (55 loc) · 1.33 KB
/
cmd_user_delete.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
package main
import (
"github.com/folded-ear/datawell/model"
"log"
)
type UserDeleteCommand struct {
Command
username *string
id *int64
}
func (c *UserDeleteCommand) resetFlags() {
if *c.id == 0 {
c.id = nil
}
if *c.username == "" {
c.username = nil
}
}
func (c *UserDeleteCommand) checkFlags() {
if (c.id != nil && c.username != nil) || (c.id == nil && c.username == nil) {
log.Fatalf("Exactly one of --id and --username must be provided")
}
}
var userDeleteCmd = &UserDeleteCommand{
Command: Command{
Name: "user-delete",
Usage: "",
Summary: "delete a user from the system",
},
}
func init() {
userDeleteCmd.Run = userDeleteRun
userDeleteCmd.username = userDeleteCmd.Flag.String("username", "", "The username of the user to delete (disallowed if --id is provided)")
userDeleteCmd.id = userDeleteCmd.Flag.Int64("id", 0, "The id of the user to delete (disallowed if --username is provided)")
}
func userDeleteRun(cmd *Command, args ...string) {
userDeleteCmd.resetFlags()
userDeleteCmd.checkFlags()
db, err := model.Gorm()
db.LogMode(true)
if err != nil {
log.Fatal(err)
}
user := &model.User{}
if userDeleteCmd.id != nil {
user.ID = *userDeleteCmd.id
} else {
user.Username = *userDeleteCmd.username
}
err = db.Where(user).First(user).Error
if err != nil {
log.Fatal(err)
}
db.Delete(user)
}