Skip to content

Commit 3a10e03

Browse files
authoredApr 26, 2023
fix(cli): check error when writing on the output (#122)
1 parent 9a1e22f commit 3a10e03

File tree

13 files changed

+53
-25
lines changed

13 files changed

+53
-25
lines changed
 

‎pkg/cmd/dictionary/entries/browse/browse.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,14 @@ func runBrowseCmd(opts *BrowseOptions) error {
144144
for _, entry := range entries {
145145
if opts.IncludeDefaultStopwords {
146146
// print all entries (default stopwords included)
147-
p.Print(opts.IO, entry)
147+
if err = p.Print(opts.IO, entry); err != nil {
148+
return err
149+
}
148150
} else if entry.Type == shared.CustomEntryType {
149151
// print only custom entries
150-
p.Print(opts.IO, entry)
152+
if err = p.Print(opts.IO, entry); err != nil {
153+
return err
154+
}
151155
}
152156
}
153157

@@ -156,7 +160,9 @@ func runBrowseCmd(opts *BrowseOptions) error {
156160

157161
// in case no entry is found in all the dictionaries
158162
if hasNoEntries {
159-
fmt.Fprintf(opts.IO.Out, "%s No entries found.\n\n", cs.WarningIcon())
163+
if _, err = fmt.Fprintf(opts.IO.Out, "%s No entries found.\n\n", cs.WarningIcon()); err != nil {
164+
return err
165+
}
160166
// go to the next dictionary
161167
break
162168
}

‎pkg/cmd/dictionary/entries/clear/clear.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func runClearCmd(opts *ClearOptions) error {
126126
}
127127

128128
if totalEntries == 0 {
129-
fmt.Fprintf(opts.IO.Out, "%s No entries to clear in %s dictionary.\n", cs.WarningIcon(), utils.SliceToReadableString(dictionariesNames))
129+
if _, err = fmt.Fprintf(opts.IO.Out, "%s No entries to clear in %s dictionary.\n", cs.WarningIcon(), utils.SliceToReadableString(dictionariesNames)); err != nil {
130+
return err
131+
}
130132
return nil
131133
}
132134

‎pkg/cmd/dictionary/entries/import/import.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ func runImportCmd(opts *ImportOptions) error {
141141
opts.IO.StopProgressIndicator()
142142

143143
if err := opts.Scanner.Err(); err != nil {
144-
145144
return err
146145
}
147146

@@ -193,8 +192,8 @@ func runImportCmd(opts *ImportOptions) error {
193192
}
194193

195194
opts.IO.StopProgressIndicator()
196-
fmt.Fprintf(opts.IO.Out, "%s Successfully imported %s entries on %s in %v\n", cs.SuccessIcon(), cs.Bold(fmt.Sprint(len(entries))), cs.Bold(opts.DictionaryName), time.Since(elapsed))
197-
return nil
195+
_, err = fmt.Fprintf(opts.IO.Out, "%s Successfully imported %s entries on %s in %v\n", cs.SuccessIcon(), cs.Bold(fmt.Sprint(len(entries))), cs.Bold(opts.DictionaryName), time.Since(elapsed))
196+
return err
198197
}
199198

200199
func ValidateDictionaryEntry(entry shared.DictionaryEntry, currentLine int) error {

‎pkg/cmd/events/tail/tail.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,17 @@ func runTailCmd(opts *TailOptions) error {
139139
return err
140140
}
141141
} else {
142-
printEvent(opts.IO, event)
142+
if err := printEvent(opts.IO, event); err != nil {
143+
return err
144+
}
143145
}
144146
}
145147
}
146148

147149
return nil
148150
}
149151

150-
func printEvent(io *iostreams.IOStreams, event insights.EventWrapper) {
152+
func printEvent(io *iostreams.IOStreams, event insights.EventWrapper) error {
151153
cs := io.ColorScheme()
152154

153155
timeLayout := "2006-01-02 15:04:05"
@@ -159,5 +161,6 @@ func printEvent(io *iostreams.IOStreams, event insights.EventWrapper) {
159161
colorizedStatus = cs.Red(fmt.Sprint(event.Status))
160162
}
161163

162-
fmt.Fprintf(io.Out, "%s [%s] %s %s [%s] %s\n", cs.Bold(formatedTime), colorizedStatus, event.Event.EventType, cs.Bold(event.Event.Index), event.Event.EventName, event.Event.UserToken)
164+
_, err := fmt.Fprintf(io.Out, "%s [%s] %s %s [%s] %s\n", cs.Bold(formatedTime), colorizedStatus, event.Event.EventType, cs.Bold(event.Event.Index), event.Event.EventName, event.Event.UserToken)
165+
return err
163166
}

‎pkg/cmd/objects/browse/browse.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func runBrowseCmd(opts *BrowseOptions) error {
110110
}
111111
return err
112112
}
113-
p.Print(opts.IO, iObject)
113+
if err = p.Print(opts.IO, iObject); err != nil {
114+
return err
115+
}
116+
114117
}
115118
}

‎pkg/cmd/objects/delete/delete.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ func runDeleteCmd(opts *DeleteOptions) error {
138138
}
139139

140140
if nbObjectsToDelete == 0 {
141-
fmt.Fprintf(opts.IO.Out, "%s No objects to delete. %s\n", cs.WarningIcon(), extra)
141+
if _, err = fmt.Fprintf(opts.IO.Out, "%s No objects to delete. %s\n", cs.WarningIcon(), extra); err != nil {
142+
return err
143+
}
142144
return nil
143145
}
144146

‎pkg/cmd/objects/operations/operations.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ func runOperationsCmd(opts *OperationsOptions) error {
163163
// Process operations
164164
opts.IO.StartProgressIndicatorWithLabel(fmt.Sprintf("Processing %s operations", cs.Bold(fmt.Sprint(len(operations)))))
165165
res, err := client.MultipleBatch(operations)
166-
167166
if err != nil {
168167
opts.IO.StopProgressIndicator()
169168
return err
@@ -179,14 +178,16 @@ func runOperationsCmd(opts *OperationsOptions) error {
179178
}
180179

181180
opts.IO.StopProgressIndicator()
182-
fmt.Fprintf(opts.IO.Out, "%s Successfully processed %s operations in %v\n", cs.SuccessIcon(), cs.Bold(fmt.Sprint(len(operations))), time.Since(elapsed))
183-
return nil
181+
_, err = fmt.Fprintf(opts.IO.Out, "%s Successfully processed %s operations in %v\n", cs.SuccessIcon(), cs.Bold(fmt.Sprint(len(operations))), time.Since(elapsed))
182+
return err
184183
}
185184

186185
// ValidateBatchOperation checks that the batch operation is valid
187186
func ValidateBatchOperation(p search.BatchOperationIndexed) error {
188-
allowedActions := []string{string(search.AddObject), string(search.UpdateObject), string(search.PartialUpdateObject),
189-
string(search.PartialUpdateObjectNoCreate), string(search.DeleteObject)}
187+
allowedActions := []string{
188+
string(search.AddObject), string(search.UpdateObject), string(search.PartialUpdateObject),
189+
string(search.PartialUpdateObjectNoCreate), string(search.DeleteObject),
190+
}
190191
extra := fmt.Sprintf("valid actions are %s", utils.SliceToReadableString(allowedActions))
191192

192193
if p.Action == "" {

‎pkg/cmd/objects/update/update.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ func runUpdateCmd(opts *UpdateOptions) error {
189189
}
190190

191191
opts.IO.StopProgressIndicator()
192-
fmt.Fprintf(opts.IO.Out, "%s Successfully updated %s objects on %s in %v\n", cs.SuccessIcon(), cs.Bold(fmt.Sprint(len(objects))), cs.Bold(opts.Index), time.Since(elapsed))
193-
return nil
192+
_, err = fmt.Fprintf(opts.IO.Out, "%s Successfully updated %s objects on %s in %v\n", cs.SuccessIcon(), cs.Bold(fmt.Sprint(len(objects))), cs.Bold(opts.Index), time.Since(elapsed))
193+
return err
194194
}

‎pkg/cmd/profile/add/add.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ func runAddCmd(opts *AddOptions) error {
164164
}
165165
}
166166

167-
fmt.Fprintf(opts.IO.Out, "%s Profile '%s' (%s) added successfully%s\n", cs.SuccessIcon(), opts.Profile.Name, opts.Profile.ApplicationID, extra)
167+
if _, err = fmt.Fprintf(opts.IO.Out, "%s Profile '%s' (%s) added successfully%s\n", cs.SuccessIcon(), opts.Profile.Name, opts.Profile.ApplicationID, extra); err != nil {
168+
return err
169+
}
168170
}
169171

170172
return nil

‎pkg/cmd/profile/remove/remove.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ func runRemoveCmd(opts *RemoveOptions) error {
103103
if len(opts.config.ConfiguredProfiles()) == 0 {
104104
extra = ". Add a profile with 'algolia profile add'."
105105
}
106-
fmt.Fprintf(opts.IO.Out, "%s '%s' removed successfully%s\n", cs.SuccessIcon(), opts.Profile, extra)
106+
if _, err = fmt.Fprintf(opts.IO.Out, "%s '%s' removed successfully%s\n", cs.SuccessIcon(), opts.Profile, extra); err != nil {
107+
return err
108+
}
107109
}
108110

109111
return nil

‎pkg/cmd/profile/setdefault/setdefault.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ func runSetDefaultCmd(opts *SetDefaultOptions) error {
7676

7777
if opts.IO.IsStdoutTTY() {
7878
if defaultName != "" {
79-
fmt.Fprintf(opts.IO.Out, "%s Default profile successfuly changed from '%s' to '%s'.\n", cs.SuccessIcon(), defaultName, opts.Profile)
79+
if _, err = fmt.Fprintf(opts.IO.Out, "%s Default profile successfuly changed from '%s' to '%s'.\n", cs.SuccessIcon(), defaultName, opts.Profile); err != nil {
80+
return err
81+
}
8082
} else {
81-
fmt.Fprintf(opts.IO.Out, "%s Default profile successfuly set to '%s'.\n", cs.SuccessIcon(), opts.Profile)
83+
if _, err = fmt.Fprintf(opts.IO.Out, "%s Default profile successfuly set to '%s'.\n", cs.SuccessIcon(), opts.Profile); err != nil {
84+
return err
85+
}
8286
}
8387
}
8488

‎pkg/cmd/rules/browse/browse.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func runListCmd(opts *ExportOptions) error {
8383
}
8484
return err
8585
}
86-
p.Print(opts.IO, iObject)
86+
if err = p.Print(opts.IO, iObject); err != nil {
87+
return err
88+
}
8789
}
8890
}

‎pkg/cmd/synonyms/browse/browse.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func runBrowseCmd(opts *BrowseOptions) error {
8282
}
8383
return err
8484
}
85-
p.Print(opts.IO, iObject)
85+
if err = p.Print(opts.IO, iObject); err != nil {
86+
return err
87+
}
8688
}
8789
}

0 commit comments

Comments
 (0)
Please sign in to comment.