Skip to content

Commit 2464a0d

Browse files
author
andrey
committed
Used fmtAndSave, added comments
1 parent 66461fc commit 2464a0d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

mfd/files.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,13 @@ func replaceFragmentInFile(output, findData, newData, pattern string) (bool, err
242242

243243
newContent := strings.Join(newlines, "\n")
244244

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)
251246
}
252247

248+
// extractFragments returned array with coordinates start and end text
253249
func extractFragments(pattern string, lines []string) ([][]int, error) {
254250
var (
255-
reFragments [][]int
256-
ff [][]int
251+
reFragments [][2]int
257252
start = -1
258253
)
259254

@@ -264,20 +259,22 @@ func extractFragments(pattern string, lines []string) ([][]int, error) {
264259
for i, line := range lines {
265260
if re.MatchString(line) {
266261
if start != -1 {
267-
reFragments = append(reFragments, []int{start, i})
262+
reFragments = append(reFragments, [2]int{start, i})
268263
}
269264
start = i
270265
}
271266
}
272267

273268
if start != -1 {
274-
reFragments = append(reFragments, []int{start, len(lines)})
269+
reFragments = append(reFragments, [2]int{start, len(lines)})
275270
}
276271

277272
if len(reFragments) == 0 {
278273
return nil, fmt.Errorf("no reFragments found with pattern: %s", pattern)
279274
}
280275

276+
var ff [][]int
277+
281278
// split big fragment
282279
for _, fragment := range reFragments {
283280
ll := lines[fragment[0]:fragment[1]]
@@ -310,7 +307,7 @@ func UpdateFile(data interface{}, output, tmpl, pattern string) (bool, error) {
310307

311308
filePart = append(filePart, lines[fragment[0]:fragment[1]]...)
312309
for _, part := range filePart {
313-
if strings.Contains(part, "func") || strings.Contains(part, "struct {") {
310+
if isStartedRow(part) {
314311
findRow = part
315312
break
316313
}
@@ -323,6 +320,14 @@ func UpdateFile(data interface{}, output, tmpl, pattern string) (bool, error) {
323320
return true, nil
324321
}
325322

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+
326331
func GoFileName(namespace string) string {
327332
if parts := strings.SplitN(namespace, ".", 2); len(parts) >= 2 {
328333
return strings.ToLower(parts[1])

0 commit comments

Comments
 (0)