/
admin.go
61 lines (49 loc) · 1.27 KB
/
admin.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
package main
import (
"context"
"errors"
"github.com/cobaltspeech/log"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/kylrth/disco-bouncer/internal/db"
"github.com/spf13/cobra"
)
var adminCmd = &cobra.Command{
Use: "admin",
Short: "Manage admins",
}
func init() {
adminCmd.AddCommand(
adminSetPass,
adminDeleteCmd,
)
}
var adminSetPass = &cobra.Command{
Use: "setpass",
Short: "Set the password for a new or existing admin",
Args: cobra.ExactArgs(2),
Run: withLAndDB(func(l log.Logger, pool *pgxpool.Pool, args []string) error {
table := db.NewAdminTable(l, pool)
user := args[0]
pass := args[1]
err := table.ChangePassword(context.Background(), user, pass)
if errors.Is(err, db.ErrNoUser) {
l.Info("msg", "admin does not exist, creating it", "admin", user)
err = table.AddAdmin(context.Background(), user, pass)
}
if err != nil {
return err
}
l.Info("msg", "set password for admin", "admin", user)
return nil
}),
}
var adminDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an admin from the database",
Args: cobra.ExactArgs(1),
Run: withLAndDB(func(l log.Logger, pool *pgxpool.Pool, args []string) error {
table := db.NewAdminTable(l, pool)
user := args[0]
return table.DeleteAdmin(context.Background(), user)
}),
}