Skip to content

Commit fdba13a

Browse files
committed
Update error messages and address review comments
1 parent 11440b6 commit fdba13a

9 files changed

Lines changed: 63 additions & 53 deletions

File tree

cli/cmd/ds-load/main.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67

@@ -13,26 +14,18 @@ import (
1314
"github.com/aserto-dev/ds-load/sdk/common/kongyaml"
1415
)
1516

16-
const errWritingToStdErr = 2
17-
1817
func main() {
1918
pluginEnum := ""
2019

2120
pluginFinder, err := plugin.NewHomeDirFinder(true)
2221
if err != nil {
23-
if _, err := os.Stderr.WriteString(err.Error()); err != nil {
24-
os.Exit(errWritingToStdErr)
25-
}
26-
22+
_, _ = fmt.Fprint(os.Stderr, err)
2723
os.Exit(1)
2824
}
2925

3026
plugins, err := pluginFinder.Find()
3127
if err != nil {
32-
if _, err := os.Stderr.WriteString(err.Error()); err != nil {
33-
os.Exit(errWritingToStdErr)
34-
}
35-
28+
_, _ = fmt.Fprint(os.Stderr, err)
3629
os.Exit(1)
3730
}
3831

cli/pkg/app/cli.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/aserto-dev/ds-load/cli/pkg/plugin"
1010
"github.com/aserto-dev/ds-load/sdk/common/cc"
1111
"github.com/aserto-dev/ds-load/sdk/common/version"
12+
"github.com/pkg/errors"
1213
)
1314

1415
type CLI struct {
@@ -51,8 +52,8 @@ func (listPlugins *ListPluginsCmd) Run(c *cc.CommonCtx) error {
5152
}
5253

5354
for _, p := range plugins {
54-
if _, err := os.Stdout.WriteString(p.Name + " " + p.Path + "\n"); err != nil {
55-
return err
55+
if _, err := fmt.Fprint(os.Stdout, p.Name, p.Path); err != nil {
56+
return errors.Wrapf(err, "failed to write plugin info for %q", p.Name)
5657
}
5758
}
5859

cli/pkg/app/exec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ func listenOnStderr(c *cc.CommonCtx, wg *sync.WaitGroup, stderr io.ReadCloser) {
155155

156156
for {
157157
line, err := scanner.ReadBytes('\n')
158+
if err != nil {
159+
c.Log.Fatal().Err(err).Send()
160+
}
161+
158162
if _, err := os.Stderr.Write(line); err != nil {
159163
c.Log.Fatal().Err(err).Send()
160164
}

plugins/auth0/pkg/fetch/fetch.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ func (f *Fetcher) fetchUsers(ctx context.Context, outputWriter *js.JSONArrayWrit
8989
}
9090

9191
for _, user := range users {
92-
obj, skip := f.buildOutputObjects(ctx, user, errorWriter)
93-
if skip {
92+
obj, err := f.buildOutputObjects(ctx, user)
93+
if err != nil {
94+
common.WriteErrorWithExitCode(errorWriter, err, 1)
95+
}
96+
97+
if obj == nil {
9498
continue
9599
}
96100

@@ -111,42 +115,40 @@ func (f *Fetcher) fetchUsers(ctx context.Context, outputWriter *js.JSONArrayWrit
111115
}
112116

113117
// return a object map to output or a boolean to skip current user.
114-
func (f *Fetcher) buildOutputObjects(ctx context.Context, user *management.User, errorWriter io.Writer) (map[string]any, bool) {
118+
func (f *Fetcher) buildOutputObjects(ctx context.Context, user *management.User) (map[string]any, error) {
115119
var obj map[string]any
116120

117121
res, err := user.MarshalJSON()
118122
if err != nil {
119-
common.WriteErrorWithExitCode(errorWriter, err, 1)
120-
return obj, true
123+
return nil, err
121124
}
122125

123126
if err := json.Unmarshal(res, &obj); err != nil {
124-
common.WriteErrorWithExitCode(errorWriter, err, 1)
125-
return obj, true
127+
return nil, err
126128
}
127129

128130
obj["email_verified"] = user.GetEmailVerified()
129131
obj["object_type"] = "user"
130132

131133
if f.Roles {
132-
roles, err := f.getUserRoles(ctx, *user.ID)
134+
roles, err := f.fetchUserRoles(ctx, *user.ID)
133135
if err != nil {
134-
common.WriteErrorWithExitCode(errorWriter, err, 1)
136+
return nil, err
135137
} else {
136138
obj["roles"] = roles
137139
}
138140
}
139141

140142
if f.Orgs {
141-
orgs, err := f.getOrgs(ctx, *user.ID)
143+
orgs, err := f.fetchOrgs(ctx, *user.ID)
142144
if err != nil {
143-
common.WriteErrorWithExitCode(errorWriter, err, 1)
145+
return nil, err
144146
} else {
145147
obj["orgs"] = orgs
146148
}
147149
}
148150

149-
return obj, false
151+
return obj, nil
150152
}
151153

152154
func (f *Fetcher) fetchGroups(ctx context.Context, outputWriter *js.JSONArrayWriter, errorWriter io.Writer) error {
@@ -159,7 +161,7 @@ func (f *Fetcher) fetchGroups(ctx context.Context, outputWriter *js.JSONArrayWri
159161
opts = append(opts, management.Query(f.getConnectionQuery()))
160162
}
161163

162-
roles, more, err := f.getRoles(ctx, opts)
164+
roles, more, err := f.fetchRoles(ctx, opts)
163165
if err != nil {
164166
common.WriteErrorWithExitCode(errorWriter, err, 1)
165167
return err
@@ -239,7 +241,7 @@ func (f *Fetcher) getUsers(ctx context.Context, opts []management.RequestOption)
239241
return ul.UserList(), ul.HasNext(), nil
240242
}
241243

242-
func (f *Fetcher) getRoles(ctx context.Context, opts []management.RequestOption) ([]*management.Role, bool, error) {
244+
func (f *Fetcher) fetchRoles(ctx context.Context, opts []management.RequestOption) ([]*management.Role, bool, error) {
243245
roles, err := f.client.Mgmt.Role.List(ctx, opts...)
244246
if err != nil {
245247
return nil, false, err
@@ -252,7 +254,7 @@ func (f *Fetcher) getRoles(ctx context.Context, opts []management.RequestOption)
252254
return roles.Roles, roles.HasNext(), nil
253255
}
254256

255-
func (f *Fetcher) getUserRoles(ctx context.Context, uID string) ([]map[string]any, error) {
257+
func (f *Fetcher) fetchUserRoles(ctx context.Context, uID string) ([]map[string]any, error) {
256258
page := 0
257259
finished := false
258260

@@ -290,7 +292,7 @@ func (f *Fetcher) getUserRoles(ctx context.Context, uID string) ([]map[string]an
290292
return results, nil
291293
}
292294

293-
func (f *Fetcher) getOrgs(ctx context.Context, uID string) ([]map[string]any, error) {
295+
func (f *Fetcher) fetchOrgs(ctx context.Context, uID string) ([]map[string]any, error) {
294296
page := 0
295297
finished := false
296298

plugins/azuread/pkg/app/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ func (cmd *VersionCmd) Run() error {
3131
return nil
3232
}
3333

34-
func createAzureAdClient(ctx context.Context,
34+
func createAzureAdClient(
35+
ctx context.Context,
3536
tenant, clientID, clientSecret, refreshToken string,
3637
) (*azureclient.AzureADClient, error) {
3738
if refreshToken != "" {

plugins/azuread/pkg/azureclient/credential.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ func (c *RefreshTokenCredential) GetToken(ctx context.Context, options policy.To
8282
// retrieve the access token and expiration
8383
token, ok := responseData["access_token"].(string)
8484
if !ok {
85-
return accessToken, status.Error(codes.InvalidArgument, "failed to cast access token to string")
85+
return accessToken, status.Error(codes.InvalidArgument, "'access_token' must be a string")
8686
}
8787

8888
accessToken.Token = token
8989

9090
expiresIn, ok := responseData["expires_in"].(float64)
9191
if !ok {
92-
return accessToken, status.Error(codes.InvalidArgument, "failed to convert token expiration time")
92+
return accessToken, status.Error(codes.InvalidArgument, "'exprires_in' must be a float")
9393
}
9494

9595
accessToken.ExpiresOn = time.Now().Add(time.Second * time.Duration(int(expiresIn)))

plugins/azuread/pkg/fetch/fetch.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ func (f *Fetcher) fetchGroups(ctx context.Context, jsonWriter *js.JSONArrayWrite
7272
for _, group := range aadGroups {
7373
writer := kiota.NewJsonSerializationWriter()
7474

75-
err := group.Serialize(writer)
76-
if err != nil {
75+
if err := group.Serialize(writer); err != nil {
7776
common.WriteErrorWithExitCode(errorWriter, err, 1)
7877
return err
7978
}

plugins/azureadb2c/pkg/azureclient/credential.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ func (c *RefreshTokenCredential) GetToken(ctx context.Context, options policy.To
8181
// retrieve the access token and expiration
8282
token, ok := responseData["access_token"].(string)
8383
if !ok {
84-
return accessToken, status.Error(codes.InvalidArgument, "failed to cast access token to string")
84+
return accessToken, status.Error(codes.InvalidArgument, "'access_token' must be a string")
8585
}
8686

8787
accessToken.Token = token
8888

8989
expiresIn, ok := responseData["expires_in"].(float64)
9090
if !ok {
91-
return accessToken, status.Error(codes.InvalidArgument, "failed to convert token expiration time")
91+
return accessToken, status.Error(codes.InvalidArgument, "'exprires_in' must be a float")
9292
}
9393

9494
accessToken.ExpiresOn = time.Now().Add(time.Second * time.Duration(int(expiresIn)))

plugins/cognito/pkg/fetch/fetch.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"io"
7+
"iter"
78

89
"github.com/aserto-dev/ds-load/plugins/cognito/pkg/cognitoclient"
910
"github.com/aserto-dev/ds-load/sdk/common"
@@ -31,8 +32,14 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer
3132
defer writer.Close()
3233

3334
if f.groups {
34-
if err := f.fetchGroups(writer, errorWriter); err != nil {
35-
return err
35+
for obj, err := range f.fetchGroups() {
36+
if err != nil {
37+
common.WriteErrorWithExitCode(errorWriter, err, 1)
38+
}
39+
40+
if err := writer.Write(obj); err != nil {
41+
_, _ = errorWriter.Write([]byte(err.Error()))
42+
}
3643
}
3744
}
3845

@@ -89,28 +96,31 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter, errorWriter io.Writer
8996
return nil
9097
}
9198

92-
func (f *Fetcher) fetchGroups(writer *js.JSONArrayWriter, errorWriter io.Writer) error {
99+
func (f *Fetcher) fetchGroups() iter.Seq2[map[string]any, error] {
93100
groups, err := f.cognitoClient.ListGroups()
94101
if err != nil {
95-
_, _ = errorWriter.Write([]byte(err.Error()))
102+
return func(yield func(map[string]any, error) bool) {
103+
if !(yield(nil, err)) {
104+
return
105+
}
106+
}
96107
}
97108

98-
for _, group := range groups {
99-
groupBytes, err := json.Marshal(group)
100-
if err != nil {
101-
common.WriteErrorWithExitCode(errorWriter, err, 1)
102-
return err
103-
}
109+
return func(yield func(map[string]any, error) bool) {
110+
for _, group := range groups {
111+
groupBytes, err := json.Marshal(group)
112+
if err != nil {
113+
if !yield(nil, err) {
114+
return
115+
}
116+
}
104117

105-
var obj map[string]interface{}
106-
if err := json.Unmarshal(groupBytes, &obj); err != nil {
107-
_, _ = errorWriter.Write([]byte(err.Error()))
108-
}
118+
var obj map[string]interface{}
119+
err = json.Unmarshal(groupBytes, &obj)
109120

110-
if err := writer.Write(obj); err != nil {
111-
_, _ = errorWriter.Write([]byte(err.Error()))
121+
if !(yield(obj, err)) {
122+
return
123+
}
112124
}
113125
}
114-
115-
return nil
116126
}

0 commit comments

Comments
 (0)