@@ -41,7 +41,7 @@ func RandomSuffix() (string, error) {
41
41
bi := new (big.Int )
42
42
r , err := rand .Int (rand .Reader , bi .SetInt64 (int64 (len (source ))))
43
43
if err != nil {
44
- return "" , err
44
+ return "" , fmt . Errorf ( "failed to generate random number: %w" , err )
45
45
}
46
46
res [i ] = source [r .Int64 ()]
47
47
}
@@ -67,23 +67,27 @@ func InsertCode(filename, target, code string) error {
67
67
//nolint:gosec // false positive
68
68
contents , err := os .ReadFile (filename )
69
69
if err != nil {
70
- return err
70
+ return fmt . Errorf ( "failed to read file %q: %w" , filename , err )
71
71
}
72
72
idx := strings .Index (string (contents ), target )
73
73
if idx == - 1 {
74
74
return fmt .Errorf ("string %s not found in %s" , target , string (contents ))
75
75
}
76
76
out := string (contents [:idx + len (target )]) + code + string (contents [idx + len (target ):])
77
77
//nolint:gosec // false positive
78
- return os .WriteFile (filename , []byte (out ), 0o644 )
78
+ if errWriteFile := os .WriteFile (filename , []byte (out ), 0o644 ); errWriteFile != nil {
79
+ return fmt .Errorf ("failed to write file %q: %w" , filename , errWriteFile )
80
+ }
81
+
82
+ return nil
79
83
}
80
84
81
- // InsertCodeIfNotExist insert code if it does not already exists
85
+ // InsertCodeIfNotExist insert code if it does not already exist
82
86
func InsertCodeIfNotExist (filename , target , code string ) error {
83
87
//nolint:gosec // false positive
84
88
contents , err := os .ReadFile (filename )
85
89
if err != nil {
86
- return err
90
+ return fmt . Errorf ( "failed to read file %q: %w" , filename , err )
87
91
}
88
92
89
93
idx := strings .Index (string (contents ), code )
@@ -98,7 +102,7 @@ func InsertCodeIfNotExist(filename, target, code string) error {
98
102
func AppendCodeIfNotExist (filename , code string ) error {
99
103
contents , err := os .ReadFile (filename )
100
104
if err != nil {
101
- return err
105
+ return fmt . Errorf ( "failed to read file %q: %w" , filename , err )
102
106
}
103
107
104
108
if strings .Contains (string (contents ), code ) {
@@ -112,16 +116,19 @@ func AppendCodeIfNotExist(filename, code string) error {
112
116
func AppendCodeAtTheEnd (filename , code string ) error {
113
117
f , err := os .OpenFile (filename , os .O_APPEND | os .O_WRONLY , 0o644 )
114
118
if err != nil {
115
- return err
119
+ return fmt . Errorf ( "failed to open file %q: %w" , filename , err )
116
120
}
117
121
defer func () {
118
122
if err = f .Close (); err != nil {
119
123
return
120
124
}
121
125
}()
122
126
123
- _ , err = f .WriteString (code )
124
- return err
127
+ if _ , errWriteString := f .WriteString (code ); errWriteString != nil {
128
+ return fmt .Errorf ("failed to write to file %q: %w" , filename , errWriteString )
129
+ }
130
+
131
+ return nil
125
132
}
126
133
127
134
// UncommentCode searches for target in the file and remove the comment prefix
@@ -130,19 +137,19 @@ func UncommentCode(filename, target, prefix string) error {
130
137
//nolint:gosec // false positive
131
138
content , err := os .ReadFile (filename )
132
139
if err != nil {
133
- return err
140
+ return fmt . Errorf ( "failed to read file %q: %w" , filename , err )
134
141
}
135
142
strContent := string (content )
136
143
137
144
idx := strings .Index (strContent , target )
138
145
if idx < 0 {
139
- return fmt .Errorf ("unable to find the code %s to be uncomment" , target )
146
+ return fmt .Errorf ("unable to find the code %q to be uncomment" , target )
140
147
}
141
148
142
149
out := new (bytes.Buffer )
143
150
_ , err = out .Write (content [:idx ])
144
151
if err != nil {
145
- return err
152
+ return fmt . Errorf ( "failed to write to file %q: %w" , filename , err )
146
153
}
147
154
148
155
scanner := bufio .NewScanner (bytes .NewBufferString (target ))
@@ -151,23 +158,26 @@ func UncommentCode(filename, target, prefix string) error {
151
158
}
152
159
for {
153
160
if _ , err = out .WriteString (strings .TrimPrefix (scanner .Text (), prefix )); err != nil {
154
- return err
161
+ return fmt . Errorf ( "failed to write to file %q: %w" , filename , err )
155
162
}
156
163
// Avoid writing a newline in case the previous line was the last in target.
157
164
if ! scanner .Scan () {
158
165
break
159
166
}
160
167
if _ , err = out .WriteString ("\n " ); err != nil {
161
- return err
168
+ return fmt . Errorf ( "failed to write to file %q: %w" , filename , err )
162
169
}
163
170
}
164
171
165
- _ , err = out .Write (content [idx + len (target ):])
166
- if err != nil {
167
- return err
172
+ if _ , err = out .Write (content [idx + len (target ):]); err != nil {
173
+ return fmt .Errorf ("failed to write to file %q: %w" , filename , err )
168
174
}
169
175
//nolint:gosec // false positive
170
- return os .WriteFile (filename , out .Bytes (), 0o644 )
176
+ if err = os .WriteFile (filename , out .Bytes (), 0o644 ); err != nil {
177
+ return fmt .Errorf ("failed to write file %q: %w" , filename , err )
178
+ }
179
+
180
+ return nil
171
181
}
172
182
173
183
// CommentCode searches for target in the file and adds the comment prefix
@@ -176,42 +186,44 @@ func CommentCode(filename, target, prefix string) error {
176
186
// Read the file content
177
187
content , err := os .ReadFile (filename )
178
188
if err != nil {
179
- return err
189
+ return fmt . Errorf ( "failed to read file %q: %w" , filename , err )
180
190
}
181
191
strContent := string (content )
182
192
183
193
// Find the target code to be commented
184
194
idx := strings .Index (strContent , target )
185
195
if idx < 0 {
186
- return fmt .Errorf ("unable to find the code %s to be commented" , target )
196
+ return fmt .Errorf ("unable to find the code %q to be commented" , target )
187
197
}
188
198
189
199
// Create a buffer to hold the modified content
190
200
out := new (bytes.Buffer )
191
- _ , err = out .Write (content [:idx ])
192
- if err != nil {
193
- return err
201
+ if _ , err = out .Write (content [:idx ]); err != nil {
202
+ return fmt .Errorf ("failed to write to file %q: %w" , filename , err )
194
203
}
195
204
196
205
// Add the comment prefix to each line of the target code
197
206
scanner := bufio .NewScanner (bytes .NewBufferString (target ))
198
207
for scanner .Scan () {
199
208
if _ , err = out .WriteString (prefix + scanner .Text () + "\n " ); err != nil {
200
- return err
209
+ return fmt . Errorf ( "failed to write to file %q: %w" , filename , err )
201
210
}
202
211
}
203
212
204
213
// Write the rest of the file content
205
- _ , err = out .Write (content [idx + len (target ):])
206
- if err != nil {
207
- return err
214
+ if _ , err = out .Write (content [idx + len (target ):]); err != nil {
215
+ return fmt .Errorf ("failed to write to file %q: %w" , filename , err )
208
216
}
209
217
210
218
// Write the modified content back to the file
211
- return os .WriteFile (filename , out .Bytes (), 0o644 )
219
+ if err = os .WriteFile (filename , out .Bytes (), 0o644 ); err != nil {
220
+ return fmt .Errorf ("failed to write file %q: %w" , filename , err )
221
+ }
222
+
223
+ return nil
212
224
}
213
225
214
- // EnsureExistAndReplace check if the content exists and then do the replace
226
+ // EnsureExistAndReplace check if the content exists and then do the replacement
215
227
func EnsureExistAndReplace (input , match , replace string ) (string , error ) {
216
228
if ! strings .Contains (input , match ) {
217
229
return "" , fmt .Errorf ("can't find %q" , match )
@@ -223,20 +235,19 @@ func EnsureExistAndReplace(input, match, replace string) (string, error) {
223
235
func ReplaceInFile (path , oldValue , newValue string ) error {
224
236
info , err := os .Stat (path )
225
237
if err != nil {
226
- return err
238
+ return fmt . Errorf ( "failed to stat file %q: %w" , path , err )
227
239
}
228
240
//nolint:gosec // false positive
229
241
b , err := os .ReadFile (path )
230
242
if err != nil {
231
- return err
243
+ return fmt . Errorf ( "failed to read file %q: %w" , path , err )
232
244
}
233
245
if ! strings .Contains (string (b ), oldValue ) {
234
246
return errors .New ("unable to find the content to be replaced" )
235
247
}
236
248
s := strings .Replace (string (b ), oldValue , newValue , - 1 )
237
- err = os .WriteFile (path , []byte (s ), info .Mode ())
238
- if err != nil {
239
- return err
249
+ if err = os .WriteFile (path , []byte (s ), info .Mode ()); err != nil {
250
+ return fmt .Errorf ("failed to write file %q: %w" , path , err )
240
251
}
241
252
return nil
242
253
}
@@ -249,25 +260,26 @@ func ReplaceInFile(path, oldValue, newValue string) error {
249
260
func ReplaceRegexInFile (path , match , replace string ) error {
250
261
matcher , err := regexp .Compile (match )
251
262
if err != nil {
252
- return err
263
+ return fmt . Errorf ( "failed to compile regular expression %q: %w" , match , err )
253
264
}
254
265
info , err := os .Stat (path )
255
266
if err != nil {
256
- return err
267
+ return fmt . Errorf ( "failed to stat file %q: %w" , path , err )
257
268
}
258
269
//nolint:gosec // false positive
259
270
b , err := os .ReadFile (path )
260
271
if err != nil {
261
- return err
272
+ return fmt . Errorf ( "failed to read file %q: %w" , path , err )
262
273
}
263
274
s := matcher .ReplaceAllString (string (b ), replace )
264
275
if s == string (b ) {
265
276
return errors .New ("unable to find the content to be replaced" )
266
277
}
267
- err = os . WriteFile ( path , [] byte ( s ), info . Mode ())
268
- if err != nil {
269
- return err
278
+
279
+ if err = os . WriteFile ( path , [] byte ( s ), info . Mode ()); err != nil {
280
+ return fmt . Errorf ( "failed to write file %q: %w" , path , err )
270
281
}
282
+
271
283
return nil
272
284
}
273
285
@@ -276,7 +288,7 @@ func HasFileContentWith(path, text string) (bool, error) {
276
288
//nolint:gosec
277
289
contents , err := os .ReadFile (path )
278
290
if err != nil {
279
- return false , err
291
+ return false , fmt . Errorf ( "failed to read file %q: %w" , path , err )
280
292
}
281
293
282
294
return strings .Contains (string (contents ), text ), nil
0 commit comments