Skip to content

Commit ebcb245

Browse files
committed
Create a custom error writer in sdk
1 parent 0170d18 commit ebcb245

24 files changed

Lines changed: 147 additions & 170 deletions

File tree

plugins/auth0/pkg/app/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/aserto-dev/ds-load/plugins/auth0/pkg/auth0client"
88
"github.com/aserto-dev/ds-load/plugins/auth0/pkg/fetch"
9+
"github.com/aserto-dev/ds-load/sdk/common"
910
"github.com/aserto-dev/ds-load/sdk/common/cc"
1011
)
1112

@@ -45,5 +46,5 @@ func (f *FetchCmd) Run(ctx *cc.CommonCtx) error {
4546
WithSAML(f.SAML).
4647
WithConnectionName(f.ConnectionName)
4748

48-
return fetcher.Fetch(ctx.Context, os.Stdout, os.Stderr)
49+
return fetcher.Fetch(ctx.Context, os.Stdout, common.ErrorWriter{Writer: os.Stderr})
4950
}

plugins/auth0/pkg/fetch/fetch.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (f *Fetcher) WithConnectionName(connectionName string) *Fetcher {
6464
return f
6565
}
6666

67-
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
67+
func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter common.ErrorWriter) error {
6868
writer := js.NewJSONArrayWriter(outputWriter)
6969
defer writer.Close()
7070

@@ -78,7 +78,7 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer
7878
return f.fetchUsers(ctx, writer, errorWriter)
7979
}
8080

81-
func (f *Fetcher) fetchUsers(ctx context.Context, outputWriter *js.JSONArrayWriter, errorWriter io.Writer) error {
81+
func (f *Fetcher) fetchUsers(ctx context.Context, outputWriter *js.JSONArrayWriter, errorWriter common.ErrorWriter) error {
8282
page := 0
8383

8484
for {
@@ -89,15 +89,13 @@ func (f *Fetcher) fetchUsers(ctx context.Context, outputWriter *js.JSONArrayWrit
8989

9090
users, more, err := f.fetchUsersList(ctx, opts)
9191
if err != nil {
92-
common.WriteErrorWithExitCode(errorWriter, err, 1)
92+
errorWriter.Error(err)
9393
return err
9494
}
9595

9696
for _, user := range users {
9797
obj, err := f.userToMap(ctx, user)
98-
if err != nil {
99-
common.WriteErrorWithExitCode(errorWriter, err, 1)
100-
}
98+
errorWriter.Error(err)
10199

102100
if obj == nil {
103101
continue
@@ -156,7 +154,7 @@ func (f *Fetcher) userToMap(ctx context.Context, user *management.User) (map[str
156154
return obj, nil
157155
}
158156

159-
func (f *Fetcher) fetchGroups(ctx context.Context, outputWriter *js.JSONArrayWriter, errorWriter io.Writer) error {
157+
func (f *Fetcher) fetchGroups(ctx context.Context, outputWriter *js.JSONArrayWriter, errorWriter common.ErrorWriter) error {
160158
page := 0
161159

162160
for f.Roles {
@@ -168,7 +166,7 @@ func (f *Fetcher) fetchGroups(ctx context.Context, outputWriter *js.JSONArrayWri
168166

169167
roles, more, err := f.fetchRoles(ctx, opts)
170168
if err != nil {
171-
common.WriteErrorWithExitCode(errorWriter, err, 1)
169+
errorWriter.Error(err)
172170
return err
173171
}
174172

@@ -178,7 +176,7 @@ func (f *Fetcher) fetchGroups(ctx context.Context, outputWriter *js.JSONArrayWri
178176
var obj map[string]any
179177

180178
if err := json.Unmarshal([]byte(res), &obj); err != nil {
181-
common.WriteErrorWithExitCode(errorWriter, err, 1)
179+
errorWriter.Error(err)
182180
continue
183181
}
184182

plugins/azuread/pkg/app/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
"github.com/aserto-dev/ds-load/plugins/azuread/pkg/fetch"
7+
"github.com/aserto-dev/ds-load/sdk/common"
78
"github.com/aserto-dev/ds-load/sdk/common/cc"
89
)
910

@@ -29,5 +30,5 @@ func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
2930
return err
3031
}
3132

32-
return fetcher.WithGroups(cmd.Groups).Fetch(ctx.Context, os.Stdout, os.Stderr)
33+
return fetcher.WithGroups(cmd.Groups).Fetch(ctx.Context, os.Stdout, common.ErrorWriter{Writer: os.Stderr})
3334
}

plugins/azuread/pkg/fetch/fetch.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,24 @@ func (f *Fetcher) WithGroups(groups bool) *Fetcher {
3333
return f
3434
}
3535

36-
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
36+
func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter common.ErrorWriter) error {
3737
jsonWriter := js.NewJSONArrayWriter(outputWriter)
3838
defer jsonWriter.Close()
3939

4040
if f.Groups {
4141
for obj, err := range f.fetchGroups(ctx) {
42-
if err != nil {
43-
common.WriteErrorWithExitCode(errorWriter, err, 1)
44-
}
42+
errorWriter.Error(err)
4543

46-
if err := jsonWriter.Write(obj); err != nil {
47-
_, _ = errorWriter.Write([]byte(err.Error()))
48-
}
44+
err := jsonWriter.Write(obj)
45+
errorWriter.ErrorNoExitCode(err)
4946
}
5047
}
5148

5249
for user, err := range f.fetchUsers(ctx) {
53-
if err != nil {
54-
common.WriteErrorWithExitCode(errorWriter, err, 1)
55-
}
50+
errorWriter.Error(err)
5651

57-
if err := jsonWriter.Write(user); err != nil {
58-
_, _ = errorWriter.Write([]byte(err.Error()))
59-
}
52+
err := jsonWriter.Write(user)
53+
errorWriter.ErrorNoExitCode(err)
6054
}
6155

6256
return nil

plugins/azureadb2c/pkg/app/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
"github.com/aserto-dev/ds-load/plugins/azureadb2c/pkg/fetch"
7+
"github.com/aserto-dev/ds-load/sdk/common"
78
"github.com/aserto-dev/ds-load/sdk/common/cc"
89
)
910

@@ -29,5 +30,5 @@ func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
2930
return err
3031
}
3132

32-
return fetcher.WithGroups(cmd.Groups).Fetch(ctx.Context, os.Stdout, os.Stderr)
33+
return fetcher.WithGroups(cmd.Groups).Fetch(ctx.Context, os.Stdout, common.ErrorWriter{Writer: os.Stderr})
3334
}

plugins/azureadb2c/pkg/fetch/fetch.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,24 @@ func (f *Fetcher) WithGroups(groups bool) *Fetcher {
3333
return f
3434
}
3535

36-
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
36+
func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter common.ErrorWriter) error {
3737
jsonWriter := js.NewJSONArrayWriter(outputWriter)
3838
defer jsonWriter.Close()
3939

4040
if f.Groups {
4141
for obj, err := range f.fetchGroups(ctx) {
42-
if err != nil {
43-
common.WriteErrorWithExitCode(errorWriter, err, 1)
44-
}
42+
errorWriter.Error(err)
4543

46-
if err := jsonWriter.Write(obj); err != nil {
47-
_, _ = errorWriter.Write([]byte(err.Error()))
48-
}
44+
err := jsonWriter.Write(obj)
45+
errorWriter.ErrorNoExitCode(err)
4946
}
5047
}
5148

5249
for user, err := range f.fetchUsers(ctx) {
53-
if err != nil {
54-
common.WriteErrorWithExitCode(errorWriter, err, 1)
55-
}
50+
errorWriter.Error(err)
5651

57-
if err := jsonWriter.Write(user); err != nil {
58-
_, _ = errorWriter.Write([]byte(err.Error()))
59-
}
52+
err := jsonWriter.Write(user)
53+
errorWriter.ErrorNoExitCode(err)
6054
}
6155

6256
return nil

plugins/cognito/pkg/app/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/aserto-dev/ds-load/plugins/cognito/pkg/cognitoclient"
77
"github.com/aserto-dev/ds-load/plugins/cognito/pkg/fetch"
8+
"github.com/aserto-dev/ds-load/sdk/common"
89
"github.com/aserto-dev/ds-load/sdk/common/cc"
910
)
1011

@@ -29,5 +30,5 @@ func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
2930

3031
fetcher = fetcher.WithGroups(cmd.Groups)
3132

32-
return fetcher.Fetch(ctx.Context, os.Stdout, os.Stderr)
33+
return fetcher.Fetch(ctx.Context, os.Stdout, common.ErrorWriter{Writer: os.Stderr})
3334
}

plugins/cognito/pkg/fetch/fetch.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,21 @@ func (f *Fetcher) WithGroups(groups bool) *Fetcher {
2929
return f
3030
}
3131

32-
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
32+
func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter common.ErrorWriter) error {
3333
writer := js.NewJSONArrayWriter(outputWriter)
3434
defer writer.Close()
3535

3636
if f.groups {
3737
for obj, err := range f.fetchGroups() {
38-
if err != nil {
39-
common.WriteErrorWithExitCode(errorWriter, err, 1)
40-
}
38+
errorWriter.Error(err)
4139

42-
if err := writer.Write(obj); err != nil {
43-
_, _ = errorWriter.Write([]byte(err.Error()))
44-
}
40+
err := writer.Write(obj)
41+
errorWriter.ErrorNoExitCode(err)
4542
}
4643
}
4744

4845
users, err := f.cognitoClient.ListUsers(ctx)
49-
if err != nil {
50-
_, _ = errorWriter.Write([]byte(err.Error()))
51-
}
46+
errorWriter.ErrorNoExitCode(err)
5247

5348
for _, user := range users {
5449
attributes := make(map[string]string)
@@ -58,41 +53,38 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer
5853

5954
userBytes, err := json.Marshal(user)
6055
if err != nil {
61-
common.WriteErrorWithExitCode(errorWriter, err, 1)
56+
errorWriter.Error(err)
6257
return err
6358
}
6459

6560
var obj map[string]any
66-
if err := json.Unmarshal(userBytes, &obj); err != nil {
67-
_, _ = errorWriter.Write([]byte(err.Error()))
68-
}
61+
err = json.Unmarshal(userBytes, &obj)
62+
errorWriter.ErrorNoExitCode(err)
6963

7064
obj["Attributes"] = attributes
7165

7266
if f.groups {
7367
groups, err := f.cognitoClient.GetGroupsForUser(*user.Username)
7468
if err != nil {
75-
_, _ = errorWriter.Write([]byte(err.Error()))
69+
errorWriter.ErrorNoExitCode(err)
7670
continue
7771
}
7872

7973
groupBytes, err := json.Marshal(groups.Groups)
8074
if err != nil {
81-
_, _ = errorWriter.Write([]byte(err.Error()))
75+
errorWriter.ErrorNoExitCode(err)
8276
return err
8377
}
8478

8579
var grps []map[string]string
86-
if err := json.Unmarshal(groupBytes, &grps); err != nil {
87-
_, _ = errorWriter.Write([]byte(err.Error()))
88-
}
80+
err = json.Unmarshal(groupBytes, &grps)
81+
errorWriter.ErrorNoExitCode(err)
8982

9083
obj["Groups"] = grps
9184
}
9285

93-
if err := writer.Write(obj); err != nil {
94-
_, _ = errorWriter.Write([]byte(err.Error()))
95-
}
86+
err = writer.Write(obj)
87+
errorWriter.ErrorNoExitCode(err)
9688
}
9789

9890
return nil

plugins/fusionauth/pkg/app/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/aserto-dev/ds-load/plugins/fusionauth/pkg/fetch"
77
"github.com/aserto-dev/ds-load/plugins/fusionauth/pkg/fusionauthclient"
8+
"github.com/aserto-dev/ds-load/sdk/common"
89
"github.com/aserto-dev/ds-load/sdk/common/cc"
910
)
1011

@@ -27,5 +28,5 @@ func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
2728

2829
fetcher = fetcher.WithGroups(cmd.Groups).WithHost(cmd.HostURL)
2930

30-
return fetcher.Fetch(ctx.Context, os.Stdout, os.Stderr)
31+
return fetcher.Fetch(ctx.Context, os.Stdout, common.ErrorWriter{Writer: os.Stderr})
3132
}

plugins/fusionauth/pkg/fetch/fetch.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88

99
"github.com/aserto-dev/ds-load/plugins/fusionauth/pkg/fusionauthclient"
10+
"github.com/aserto-dev/ds-load/sdk/common"
1011
"github.com/aserto-dev/ds-load/sdk/common/js"
1112
)
1213

@@ -32,50 +33,44 @@ func (f *Fetcher) WithHost(host string) *Fetcher {
3233
return f
3334
}
3435

35-
func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer) error {
36+
func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter common.ErrorWriter) error {
3637
writer := js.NewJSONArrayWriter(outputWriter)
3738
defer writer.Close()
3839

3940
users, err := f.fusionauthClient.ListUsers(ctx)
40-
if err != nil {
41-
_, _ = errorWriter.Write([]byte(err.Error()))
42-
}
41+
errorWriter.ErrorNoExitCode(err)
4342

4443
for i := range users {
4544
user := &users[i]
4645

4746
userBytes, err := json.Marshal(user)
4847
if err != nil {
49-
_, _ = errorWriter.Write([]byte(err.Error()))
48+
errorWriter.ErrorNoExitCode(err)
5049
return err
5150
}
5251

5352
var obj map[string]any
5453
if err := json.Unmarshal(userBytes, &obj); err != nil {
55-
_, _ = errorWriter.Write([]byte(err.Error()))
54+
errorWriter.ErrorNoExitCode(err)
5655
return err
5756
}
5857

5958
if user.ImageUrl != "" {
6059
obj["picture"] = fmt.Sprintf("%s%s", f.host, user.ImageUrl)
6160
}
6261

63-
if err := writer.Write(obj); err != nil {
64-
_, _ = errorWriter.Write([]byte(err.Error()))
65-
}
62+
err = writer.Write(obj)
63+
errorWriter.ErrorNoExitCode(err)
6664
}
6765

6866
if f.groups {
6967
groups, err := f.fusionauthClient.ListGroups(ctx)
70-
if err != nil {
71-
_, _ = errorWriter.Write([]byte(err.Error()))
72-
}
68+
errorWriter.ErrorNoExitCode(err)
7369

7470
for i := range groups {
7571
group := &groups[i]
76-
if err := writer.Write(group); err != nil {
77-
_, _ = errorWriter.Write([]byte(err.Error()))
78-
}
72+
err := writer.Write(group)
73+
errorWriter.ErrorNoExitCode(err)
7974
}
8075
}
8176

0 commit comments

Comments
 (0)