From 123cd38a18638821903d44fb77baa3aa028db9a9 Mon Sep 17 00:00:00 2001 From: Ruslan Popov Date: Thu, 1 Jan 2026 13:11:33 +0300 Subject: [PATCH] Add fatal missing-version handling with opt-out flag --- README.md | 17 ++++++++++++----- src/git.go | 7 ++++++- src/main.go | 21 ++++++++++++++++----- src/main_test.go | 23 ++++++++++++++++++++++- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3ffbea8..a109893 100644 --- a/README.md +++ b/README.md @@ -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 +``` ## Девопсу diff --git a/src/git.go b/src/git.go index 9a32a61..2e28034 100644 --- a/src/git.go +++ b/src/git.go @@ -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) diff --git a/src/main.go b/src/main.go index 23a7ccd..5bd5cfc 100644 --- a/src/main.go +++ b/src/main.go @@ -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 { - log.Printf("Updated file: %s", path) + 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 { diff --git a/src/main_test.go b/src/main_test.go index 1b357ee..5cdc4eb 100644 --- a/src/main_test.go +++ b/src/main_test.go @@ -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 }