@@ -242,18 +242,13 @@ func replaceFragmentInFile(output, findData, newData, pattern string) (bool, err
242
242
243
243
newContent := strings .Join (newlines , "\n " )
244
244
245
- err = os .WriteFile (output , []byte (newContent ), 0644 )
246
- if err != nil {
247
- return false , fmt .Errorf ("err write in file: %w" , err )
248
- }
249
-
250
- return true , nil
245
+ return util .FmtAndSave ([]byte (newContent ), output )
251
246
}
252
247
248
+ // extractFragments returned array with coordinates start and end text
253
249
func extractFragments (pattern string , lines []string ) ([][]int , error ) {
254
250
var (
255
- reFragments [][]int
256
- ff [][]int
251
+ reFragments [][2 ]int
257
252
start = - 1
258
253
)
259
254
@@ -264,20 +259,22 @@ func extractFragments(pattern string, lines []string) ([][]int, error) {
264
259
for i , line := range lines {
265
260
if re .MatchString (line ) {
266
261
if start != - 1 {
267
- reFragments = append (reFragments , []int {start , i })
262
+ reFragments = append (reFragments , [2 ]int {start , i })
268
263
}
269
264
start = i
270
265
}
271
266
}
272
267
273
268
if start != - 1 {
274
- reFragments = append (reFragments , []int {start , len (lines )})
269
+ reFragments = append (reFragments , [2 ]int {start , len (lines )})
275
270
}
276
271
277
272
if len (reFragments ) == 0 {
278
273
return nil , fmt .Errorf ("no reFragments found with pattern: %s" , pattern )
279
274
}
280
275
276
+ var ff [][]int
277
+
281
278
// split big fragment
282
279
for _ , fragment := range reFragments {
283
280
ll := lines [fragment [0 ]:fragment [1 ]]
@@ -310,7 +307,7 @@ func UpdateFile(data interface{}, output, tmpl, pattern string) (bool, error) {
310
307
311
308
filePart = append (filePart , lines [fragment [0 ]:fragment [1 ]]... )
312
309
for _ , part := range filePart {
313
- if strings . Contains (part , "func" ) || strings . Contains ( part , "struct {" ) {
310
+ if isStartedRow (part ) {
314
311
findRow = part
315
312
break
316
313
}
@@ -323,6 +320,14 @@ func UpdateFile(data interface{}, output, tmpl, pattern string) (bool, error) {
323
320
return true , nil
324
321
}
325
322
323
+ // isStartedRow find row where start function or structure
324
+ func isStartedRow (row string ) bool {
325
+ if strings .Contains (row , "func" ) || strings .Contains (row , "struct" ) {
326
+ return true
327
+ }
328
+ return false
329
+ }
330
+
326
331
func GoFileName (namespace string ) string {
327
332
if parts := strings .SplitN (namespace , "." , 2 ); len (parts ) >= 2 {
328
333
return strings .ToLower (parts [1 ])
0 commit comments