-
Notifications
You must be signed in to change notification settings - Fork 28
/
userimport.go
83 lines (67 loc) · 1.72 KB
/
userimport.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package e2e
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/authgear/authgear-server/pkg/lib/config/configsource"
"github.com/authgear/authgear-server/pkg/lib/deps"
"github.com/authgear/authgear-server/pkg/lib/infra/task"
"github.com/authgear/authgear-server/pkg/lib/userimport"
)
type End2End struct {
Context context.Context
}
type NoopTaskQueue struct{}
func (q NoopTaskQueue) Enqueue(param task.Param) {
}
func (c *End2End) ImportUsers(appID string, jsonPath string) error {
cfg, err := LoadConfigFromEnv()
if err != nil {
return err
}
cfg.ConfigSource = &configsource.Config{
Type: configsource.TypeDatabase,
Watch: false,
}
taskQueueFactory := deps.TaskQueueFactory(func(provider *deps.AppProvider) task.Queue {
return NoopTaskQueue{}
})
p, err := deps.NewRootProvider(
cfg.EnvironmentConfig,
cfg.ConfigSource,
cfg.BuiltinResourceDirectory,
cfg.CustomResourceDirectory,
taskQueueFactory,
)
if err != nil {
return err
}
configSrcController := newConfigSourceController(p, context.Background())
err = configSrcController.Open()
if err != nil {
return err
}
defer configSrcController.Close()
appCtx, err := configSrcController.ResolveContext(appID)
if err != nil {
return err
}
appProvider := p.NewAppProvider(c.Context, appCtx)
userImport := newUserImport(appProvider, c.Context)
jsoFile, err := os.Open(jsonPath)
if err != nil {
return err
}
defer jsoFile.Close()
var request userimport.Request
err = json.NewDecoder(jsoFile).Decode(&request)
if err != nil {
return err
}
res := userImport.ImportRecords(c.Context, &request)
if res.Summary.Failed > 0 {
return fmt.Errorf("failed to import %d records due to %v", res.Summary.Failed, res.Details)
}
return nil
}