Add fatal missing-version handling with opt-out flag
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
17
README.md
17
README.md
@@ -6,11 +6,18 @@
|
|||||||
|
|
||||||
## Разработчику
|
## Разработчику
|
||||||
|
|
||||||
export PATH=$PATH:/usr/local/go/bin
|
```bash
|
||||||
go mod init src
|
export PATH=$PATH:/usr/local/go/bin
|
||||||
go mod tidy
|
go mod init src
|
||||||
go build
|
go mod tidy
|
||||||
go run .
|
go build
|
||||||
|
go run .
|
||||||
|
```
|
||||||
|
или
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
||||||
## Девопсу
|
## Девопсу
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,12 @@ func gitCommit(bc *BumpConfig, newVersion string, configPath string) {
|
|||||||
log.Fatalf("Stat error for %s: %v", p, err)
|
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 st.Worktree != git.Unmodified {
|
||||||
if _, err := worktree.Add(p); err != nil {
|
if _, err := worktree.Add(p); err != nil {
|
||||||
log.Fatalf("Add %s error: %v", p, err)
|
log.Fatalf("Add %s error: %v", p, err)
|
||||||
|
|||||||
21
src/main.go
21
src/main.go
@@ -116,8 +116,8 @@ func bumpVersion(bc *BumpConfig, part string) (string, error) {
|
|||||||
return newVersion, nil
|
return newVersion, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateFiles обновляет версию в файле
|
// updateFiles обновляет версию в файле; при fatalIfMissing=true возвращает ошибку, если строка не найдена.
|
||||||
func updateFiles(filePaths []string, oldVersion, newVersion string) {
|
func updateFiles(filePaths []string, oldVersion, newVersion string, fatalIfMissing bool) error {
|
||||||
for _, path := range filePaths {
|
for _, path := range filePaths {
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -125,12 +125,20 @@ func updateFiles(filePaths []string, oldVersion, newVersion string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newData := strings.ReplaceAll(string(data), oldVersion, newVersion)
|
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 {
|
if err := os.WriteFile(path, []byte(newData), 0644); err != nil {
|
||||||
log.Printf("Unable to write file %s: %v", path, err)
|
log.Printf("Unable to write file %s: %v", path, err)
|
||||||
} else {
|
continue
|
||||||
|
}
|
||||||
log.Printf("Updated file: %s", path)
|
log.Printf("Updated file: %s", path)
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateConfigFile обновляет исходный конфигурационный файл
|
// updateConfigFile обновляет исходный конфигурационный файл
|
||||||
@@ -201,6 +209,7 @@ func main() {
|
|||||||
noCommit := flag.Bool("no-commit", false, "Do not create a commit")
|
noCommit := flag.Bool("no-commit", false, "Do not create a commit")
|
||||||
tag := flag.Bool("tag", false, "Add a git tag")
|
tag := flag.Bool("tag", false, "Add a git tag")
|
||||||
noTag := flag.Bool("no-tag", false, "Do not 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")
|
push := flag.Bool("push", false, "Force push to repository")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@@ -228,7 +237,9 @@ func main() {
|
|||||||
fmt.Printf("New version: %s\n", newVersion)
|
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 {
|
if err := updateConfigFile(cfg_name, newVersion); err != nil {
|
||||||
|
|||||||
@@ -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 {
|
for _, p := range filePaths {
|
||||||
data, err := os.ReadFile(p)
|
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) {
|
func TestResolveFlag(t *testing.T) {
|
||||||
boolPtr := func(b bool) *bool { return &b }
|
boolPtr := func(b bool) *bool { return &b }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user