-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (86 loc) · 2.44 KB
/
main.go
File metadata and controls
103 lines (86 loc) · 2.44 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/generate/selfserve/config"
"github.com/generate/selfserve/internal/repository"
"github.com/generate/selfserve/internal/service/clerk"
storage "github.com/generate/selfserve/internal/service/storage/postgres"
"github.com/sethvargo/go-envconfig"
)
// command is a runnable CLI subcommand.
type command struct {
description string
run func(ctx context.Context, cfg config.Config, args []string) error
}
// commands is the registry of all available CLI subcommands.
// To add a new command: add an entry here and implement its run func below.
//
// "my-new-command": {
// description: "What this command does",
// run: runMyNewCommand,
// },
var commands = map[string]command{
"sync-users": {
description: "Sync users from Clerk into the database",
run: runSyncUsers,
},
}
func main() {
flag.Usage = printUsage
flag.Parse()
args := flag.Args()
if len(args) == 0 {
printUsage()
os.Exit(1)
}
name := args[0]
cmd, ok := commands[name]
if !ok {
fmt.Fprintf(os.Stderr, "unknown command: %q\n\n", name)
printUsage()
os.Exit(1)
}
ctx := context.Background()
var cfg config.Config
if err := envconfig.Process(ctx, &cfg); err != nil {
log.Fatal("failed to process config:", err)
}
if err := cmd.run(ctx, cfg, args[1:]); err != nil {
log.Fatalf("%s: %v", name, err)
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: cli <command> [args]\n\nAvailable commands:\n")
for name, cmd := range commands {
fmt.Fprintf(os.Stderr, " %-20s %s\n", name, cmd.description)
}
fmt.Fprintln(os.Stderr)
}
// =============================================================================
// Command implementations
// =============================================================================
func runSyncUsers(ctx context.Context, cfg config.Config, _ []string) error {
repo, err := storage.NewRepository(cfg.DB)
if err != nil {
return fmt.Errorf("failed to connect to db: %w", err)
}
defer repo.Close()
usersRepo := repository.NewUsersRepository(repo.DB)
users, err := clerk.FetchUsersFromClerk(cfg.BaseURL+"/users", cfg.SecretKey)
if err != nil {
return err
}
transformed, err := clerk.ValidateAndReformatUserData(users)
if err != nil {
return err
}
if err := usersRepo.BulkInsertUsers(ctx, transformed); err != nil {
return fmt.Errorf("failed to insert users: %w", err)
}
fmt.Println("sync-users completed successfully")
return nil
}