-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch.go
More file actions
83 lines (67 loc) · 1.67 KB
/
fetch.go
File metadata and controls
83 lines (67 loc) · 1.67 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
package fetch
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/aserto-dev/ds-load/plugins/fusionauth/pkg/fusionauthclient"
"github.com/aserto-dev/ds-load/sdk/common/js"
)
type Fetcher struct {
fusionauthClient *fusionauthclient.FusionAuthClient
groups bool
host string
}
func New(client *fusionauthclient.FusionAuthClient) (*Fetcher, error) {
return &Fetcher{
fusionauthClient: client,
}, nil
}
func (f *Fetcher) WithGroups(groups bool) *Fetcher {
f.groups = groups
return f
}
func (f *Fetcher) WithHost(host string) *Fetcher {
f.host = host
return f
}
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
writer := js.NewJSONArrayWriter(outputWriter)
defer writer.Close()
users, err := f.fusionauthClient.ListUsers(ctx)
if err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
}
for i := range users {
user := &users[i]
userBytes, err := json.Marshal(user)
if err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
return err
}
var obj map[string]any
if err := json.Unmarshal(userBytes, &obj); err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
return err
}
if user.ImageUrl != "" {
obj["picture"] = fmt.Sprintf("%s%s", f.host, user.ImageUrl)
}
if err := writer.Write(obj); err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
}
}
if f.groups {
groups, err := f.fusionauthClient.ListGroups(ctx)
if err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
}
for i := range groups {
group := &groups[i]
if err := writer.Write(group); err != nil {
_, _ = errorWriter.Write([]byte(err.Error()))
}
}
}
return nil
}