-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_user_edit.go
68 lines (61 loc) · 1.5 KB
/
cmd_user_edit.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 main
import (
"github.com/folded-ear/datawell/model"
"log"
)
type UserEditCommand struct {
Command
name *string
username *string
id *int64
}
func (c *UserEditCommand) resetFlags() {
if *c.id == 0 {
c.id = nil
}
if *c.username == "" {
c.username = nil
}
}
func (c *UserEditCommand) 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")
}
if *c.name == "" {
log.Fatalf("You have to specify something to update.")
}
}
var userEditCmd = &UserEditCommand{
Command: Command{
Name: "user-edit",
Usage: "",
Summary: "edit a user in the system",
},
}
func init() {
userEditCmd.Run = userEditRun
userEditCmd.name = userEditCmd.Flag.String("name", "", "The user's updated name")
userEditCmd.username = userEditCmd.Flag.String("username", "", "The username of the user to update (disallowed if --id is provided)")
userEditCmd.id = userEditCmd.Flag.Int64("id", 0, "The id of the user to update (disallowed if --username is provided)")
}
func userEditRun(cmd *Command, args ...string) {
userEditCmd.resetFlags()
userEditCmd.checkFlags()
db, err := model.Gorm()
db.LogMode(true)
if err != nil {
log.Fatal(err)
}
user := &model.User{}
if userEditCmd.id != nil {
user.ID = *userEditCmd.id
} else {
user.Username = *userEditCmd.username
}
err = db.Where(user).First(user).Error
if err != nil {
log.Fatal(err)
}
user.Name = *userEditCmd.name
db.Save(user)
}