Skip to content

Commit

Permalink
Skip to modify the file if the updater returned errors (#161)
Browse files Browse the repository at this point in the history
* Check results are keeping updatable format even after replaced

* Add failing example for bad result
  • Loading branch information
kachick committed Apr 23, 2024
1 parent 5d564ab commit f4d4331
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion examples/simple.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
will_be_replaced: '0.39.0' # selfup { "regex": "\\d[^']+", "script": "echo 0.76.9" }
not_be_replacedA: '0.39.0' # selfup { "regex": "\\d[^']+", "script": "echo 0.39.0" }
not_be_replacedB: ':<' # selfup { "regex": ":<", "script": "echo ':)'" }
not_be_replacedB: ':<' # selfup { "regex": ":[<\\)]", "script": "echo ':)'" }
broken: ':<' # selfup { "regex": ":[<\\)]", "script": "this_command_does_not_exist_so_raise_errors_and_do_not_update_this_file" }
17 changes: 10 additions & 7 deletions internal/selfup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ func Update(path string, prefix string, skipBy string, isColor bool) (Result, er
}
replacer := strings.TrimSuffix(string(out), "\n")
extracted := re.FindString(head)
replaced := strings.Replace(head, extracted, replacer, 1)
if !isChanged {
isChanged = replaced != head
}
extractedToEnsure := re.FindString(replaced)
if replacer != extractedToEnsure {
return Result{}, xerrors.Errorf("%s:%d: The result of updater command has malformed format: %s", path, lineNumber, replacer)
}
estimation := " "
suffix := ""
if extracted != replacer {
if replaced != head {
replacedCount += 1
estimation = "✓"
if isColor {
Expand All @@ -80,13 +88,8 @@ func Update(path string, prefix string, skipBy string, isColor bool) (Result, er
}
suffix = fmt.Sprintf(" => %s", replacer)
}
fmt.Println(fmt.Sprintf("%s %s:%d: %s", estimation, path, lineNumber, extracted) + suffix)

replaced := strings.Replace(head, extracted, replacer, 1)
if !isChanged {
isChanged = replaced != head
}
newLines = append(newLines, replaced+match+tail)
fmt.Println(fmt.Sprintf("%s %s:%d: %s", estimation, path, lineNumber, extracted) + suffix)
}

return Result{
Expand Down

0 comments on commit f4d4331

Please sign in to comment.