-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixer: add --on-conflict flag to support renaming (#1127)
This PR adds a new flag to the fix command, `--on-conflict`. This flag can be used to specify the behavior of the fix command when a file's new name conflicts with an existing file or another file's fixed name. Setting --on-conflict to "rename" will cause the fix command to rename the file to a different name at the target location. The new name will be generated by appending a number to the end of the file name. Signed-off-by: Charlie Egan <charlie@styra.com>
- Loading branch information
1 parent
a23bb63
commit 23cb827
Showing
5 changed files
with
315 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package fixer | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// renameCandidate takes a filename and produces a new name with an incremented | ||
// numeric suffix. It correctly handles test files by inserting the increment | ||
// before the "_test" suffix and preserves the original directory. | ||
func renameCandidate(oldName string) string { | ||
dir := filepath.Dir(oldName) | ||
baseWithExt := filepath.Base(oldName) | ||
|
||
ext := filepath.Ext(baseWithExt) | ||
base := strings.TrimSuffix(baseWithExt, ext) | ||
|
||
suffix := "" | ||
if strings.HasSuffix(base, "_test") { | ||
suffix = "_test" | ||
base = strings.TrimSuffix(base, "_test") | ||
} | ||
|
||
re := regexp.MustCompile(`^(.*)_(\d+)$`) | ||
matches := re.FindStringSubmatch(base) | ||
|
||
if len(matches) == 3 { | ||
baseName := matches[1] | ||
numStr := matches[2] | ||
num, _ := strconv.Atoi(numStr) | ||
num++ | ||
base = fmt.Sprintf("%s_%d", baseName, num) | ||
} else { | ||
base += "_1" | ||
} | ||
|
||
newBase := base + suffix + ext | ||
newName := filepath.Join(dir, newBase) | ||
|
||
return newName | ||
} |
Oops, something went wrong.