fix changed not saved to the file
This commit is contained in:
@@ -50,12 +50,18 @@ func FixCommand(c *cli.Context) error {
|
||||
// Run line checkers and fixers.
|
||||
lines := SplitIntoLines(fileContent)
|
||||
lineNo := 1
|
||||
for _, line := range lines {
|
||||
for i, line := range lines {
|
||||
for ruleName, ruleValue := range rules {
|
||||
if lineChecker, ok := lineCheckers[ruleName]; ok {
|
||||
result := lineChecker(ruleValue, line)
|
||||
if !result.isOk {
|
||||
fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk)
|
||||
if result.fixer != nil {
|
||||
lines[i] = result.fixer(ruleValue, line)
|
||||
hasChanged = true
|
||||
fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": fixed")
|
||||
} else {
|
||||
fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk)
|
||||
}
|
||||
// Don't show more than 1 error per line.
|
||||
break
|
||||
}
|
||||
@@ -64,13 +70,18 @@ func FixCommand(c *cli.Context) error {
|
||||
lineNo++
|
||||
}
|
||||
|
||||
// Rejoin the lines if any were changed
|
||||
if hasChanged {
|
||||
fileHandler, err := os.Open(f)
|
||||
fileContent = strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
if hasChanged {
|
||||
err := os.WriteFile(f, []byte(fileContent), 0644)
|
||||
if err != nil {
|
||||
fmt.Println("Could not write to " + f)
|
||||
fmt.Println("Could not write to " + f + ": " + err.Error())
|
||||
} else {
|
||||
fmt.Println("Wrote to " + f)
|
||||
}
|
||||
fileHandler.WriteString(fileContent)
|
||||
fmt.Println("Wrote to " + f)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user