forked from mdouchement/standardfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (55 loc) · 1.39 KB
/
main.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
package main
import (
"fmt"
"log"
"github.com/asdine/storm/v3"
"github.com/asdine/storm/v3/q"
"github.com/cherya/standardfile/internal/database"
"github.com/cherya/standardfile/internal/model"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
func main() {
c := &cobra.Command{
Use: "rmuser",
Short: "Remove a user from the database",
Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
//
//
fmt.Println("Opening", args[0])
db, err := storm.Open(args[0], database.StormCodec)
if err != nil {
return errors.Wrap(err, "could not open database")
}
defer db.Close()
// Fetch user
var user model.User
err = db.One("Email", args[1], &user)
if err != nil {
if err == storm.ErrNotFound {
fmt.Println("No account for this email")
return nil
}
return errors.Wrap(err, "find user by mail")
}
fmt.Println("User found:", user.ID)
// Deleting user's items
err = db.Select(q.Eq("UserID", user.ID)).Delete(&model.Item{})
if err != nil && err != storm.ErrNotFound {
return errors.Wrap(err, "delete items")
}
fmt.Println("Items removed")
// Delete user
err = db.DeleteStruct(&user)
if err != nil && err != storm.ErrNotFound {
return errors.Wrap(err, "delete user")
}
fmt.Println("User removed")
return nil
},
}
if err := c.Execute(); err != nil {
log.Fatalf("%+v", err)
}
}