-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (50 loc) · 1.2 KB
/
Copy pathmain.go
File metadata and controls
65 lines (50 loc) · 1.2 KB
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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
cmdinternal "github.com/MarioCarrion/videos/2023/transaction-in-context/cmd/internal"
"github.com/MarioCarrion/videos/2023/transaction-in-context/internal/postgresql"
)
func main() {
var (
userID string
roleIDs []uuid.UUID
)
flag.StringVar(&userID, "id", "", "id of user to select")
flag.Func("roles", "comma separated list of Role IDs", func(s string) error {
for _, str := range strings.Split(s, ",") {
id, err := uuid.Parse(str)
if err != nil {
return fmt.Errorf("UUID Parsing error %w", err)
}
roleIDs = append(roleIDs, id)
}
return nil
})
flag.Parse()
if userID == "" {
flag.PrintDefaults()
os.Exit(0)
}
id, err := uuid.Parse(userID)
if err != nil {
log.Fatalln("UUID Parsing error:", err)
}
//-
ctx := context.Background()
conn, err := pgx.Connect(ctx, cmdinternal.NewConnString())
if err != nil {
log.Fatalln("Connection error:", err)
}
userRoleRepo := postgresql.NewUserRole(conn)
if err := userRoleRepo.Insert(ctx, id, roleIDs...); err != nil {
log.Fatalln("userRoleRepo.Insert", err)
}
fmt.Println("Roles inserted")
}