Add fatal missing-version handling with opt-out flag
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-01-01 13:11:33 +03:00
parent f9865168c9
commit 123cd38a18
4 changed files with 56 additions and 12 deletions

View File

@@ -6,11 +6,18 @@
## Разработчику
export PATH=$PATH:/usr/local/go/bin
go mod init src
go mod tidy
go build
go run .
```bash
export PATH=$PATH:/usr/local/go/bin
go mod init src
go mod tidy
go build
go run .
```
или
```bash
make
```
## Девопсу

View File

@@ -86,7 +86,12 @@ func gitCommit(bc *BumpConfig, newVersion string, configPath string) {
log.Fatalf("Stat error for %s: %v", p, err)
}
st := status[p]
st, ok := status[p]
if !ok || st == nil {
// nothing to stage for this file
continue
}
if st.Worktree != git.Unmodified {
if _, err := worktree.Add(p); err != nil {
log.Fatalf("Add %s error: %v", p, err)

View File

@@ -116,8 +116,8 @@ func bumpVersion(bc *BumpConfig, part string) (string, error) {
return newVersion, nil
}
// updateFiles обновляет версию в файле
func updateFiles(filePaths []string, oldVersion, newVersion string) {
// updateFiles обновляет версию в файле; при fatalIfMissing=true возвращает ошибку, если строка не найдена.
func updateFiles(filePaths []string, oldVersion, newVersion string, fatalIfMissing bool) error {
for _, path := range filePaths {
data, err := os.ReadFile(path)
if err != nil {
@@ -125,12 +125,20 @@ func updateFiles(filePaths []string, oldVersion, newVersion string) {
continue
}
newData := strings.ReplaceAll(string(data), oldVersion, newVersion)
if newData == string(data) {
log.Printf("Version %s not found in %s; skip update", oldVersion, path)
if fatalIfMissing {
return fmt.Errorf("version %s not found in %s", oldVersion, path)
}
continue
}
if err := os.WriteFile(path, []byte(newData), 0644); err != nil {
log.Printf("Unable to write file %s: %v", path, err)
} else {
continue
}
log.Printf("Updated file: %s", path)
}
}
return nil
}
// updateConfigFile обновляет исходный конфигурационный файл
@@ -201,6 +209,7 @@ func main() {
noCommit := flag.Bool("no-commit", false, "Do not create a commit")
tag := flag.Bool("tag", false, "Add a git tag")
noTag := flag.Bool("no-tag", false, "Do not add a git tag")
noFatal := flag.Bool("no-fatal", false, "Do not fail if a configured file does not contain the current version")
push := flag.Bool("push", false, "Force push to repository")
flag.Parse()
@@ -228,7 +237,9 @@ func main() {
fmt.Printf("New version: %s\n", newVersion)
// Обновляем файлы, указанные в конфигурации
updateFiles(bc.FilePaths, bc.CurrentVersion, newVersion)
if err := updateFiles(bc.FilePaths, bc.CurrentVersion, newVersion, !*noFatal); err != nil {
log.Fatalf("Error updating files: %v", err)
}
// Обновляем конфигурационный файл
if err := updateConfigFile(cfg_name, newVersion); err != nil {

View File

@@ -200,7 +200,9 @@ func TestUpdateFiles(t *testing.T) {
}
}
updateFiles(filePaths, oldV, newV)
if err := updateFiles(filePaths, oldV, newV, true); err != nil {
t.Fatalf("updateFiles returned error: %v", err)
}
for _, p := range filePaths {
data, err := os.ReadFile(p)
@@ -213,6 +215,25 @@ func TestUpdateFiles(t *testing.T) {
}
}
func TestUpdateFilesMissingVersionFatal(t *testing.T) {
tmpDir := t.TempDir()
oldV := "1.2.3"
newV := "1.2.4"
target := filepath.Join(tmpDir, "README.md")
if err := os.WriteFile(target, []byte("no version here\n"), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
if err := updateFiles([]string{target}, oldV, newV, true); err == nil {
t.Fatalf("expected error when version is missing")
}
if err := updateFiles([]string{target}, oldV, newV, false); err != nil {
t.Fatalf("did not expect error when no-fatal is set: %v", err)
}
}
func TestResolveFlag(t *testing.T) {
boolPtr := func(b bool) *bool { return &b }