Skip to content

Commit

Permalink
support globs in replacements path
Browse files Browse the repository at this point in the history
  • Loading branch information
Magic-Mayo committed Sep 12, 2024
1 parent b825a72 commit fcbd67e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/added-20240911-234212.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: added
body: Support filepath globs in replacements
time: 2024-09-11T23:42:12.277856663-04:00
custom:
Issue: "713"
25 changes: 20 additions & 5 deletions core/replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"text/template"
)
Expand Down Expand Up @@ -39,6 +40,13 @@ type ReplaceData struct {
// replace: ' "version": "{{.VersionNoPrefix}}",'
type Replacement struct {
// Path of the file to find and replace in.
// Also supports Go filepath globs.
// example: yaml
// # Will match any .json file in the current directory
// replacements:
// - path: *.json
// find: ' "version": ".*",'
// replace: ' "version": "{{.VersionNoPrefix}}",'
Path string `yaml:"path" required:"true"`
// Regular expression to search for in the file.
// Capture groups are supported and can be used in the replace value.
Expand Down Expand Up @@ -67,7 +75,7 @@ func (r Replacement) Execute(data ReplaceData) error {
return err
}

fileData, err := os.ReadFile(r.Path)
globs, err := filepath.Glob(r.Path)
if err != nil {
return err
}
Expand All @@ -82,11 +90,18 @@ func (r Replacement) Execute(data ReplaceData) error {
return err
}

newData := regex.ReplaceAll(fileData, buf.Bytes())
for _, path := range globs {
fileData, err := os.ReadFile(path)
if err != nil {
return err
}

err = os.WriteFile(r.Path, newData, CreateFileMode)
if err != nil {
return err
newData := regex.ReplaceAll(fileData, buf.Bytes())

err = os.WriteFile(path, newData, CreateFileMode)
if err != nil {
return err
}
}

return nil
Expand Down
74 changes: 64 additions & 10 deletions core/replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"os"
"strings"
"testing"

"github.com/miniscruff/changie/then"
Expand Down Expand Up @@ -126,16 +127,6 @@ level1:
then.FileContents(t, endData, filepath)
}

func TestErrorBadFileRead(t *testing.T) {
then.WithTempDir(t)

rep := Replacement{
Path: "does not exist",
}
err := rep.Execute(ReplaceData{})
then.NotNil(t, err)
}

func TestErrorBadTemplateParse(t *testing.T) {
then.WithTempDir(t)

Expand Down Expand Up @@ -180,3 +171,66 @@ func TestErrorBadTemplateExec(t *testing.T) {
err := rep.Execute(ReplaceData{})
then.NotNil(t, err)
}

func testGlobs(t *testing.T, paths []string, rep Replacement) {
toReplace := `{
"version": "1.0.0"
}`
replaceWith := "1.2.3"

for _, path := range paths {
split := strings.Split(path, "/")
if split[0] != path {
err := os.Mkdir(split[0], os.ModePerm)
then.Nil(t, err)
}
err := os.WriteFile(path, []byte(toReplace), os.ModePerm)

Check failure on line 187 in core/replacement_test.go

View workflow job for this annotation

GitHub Actions / Build

assignments should only be cuddled with other assignments (wsl)
then.Nil(t, err)
}

err := rep.Execute(ReplaceData{
VersionNoPrefix: replaceWith,
})
then.Nil(t, err)

expected := `{
"version": "` + replaceWith + `"
}`

for _, path := range paths {
then.FileContents(t, expected, path)
}
}

func TestGlobs(t *testing.T) {
then.WithTempDir(t)

paths := []string{"a.json", "b.json"}
rep := Replacement{
Path: "*.json",
Find: ` "version": ".*"`,
Replace: ` "version": "{{.VersionNoPrefix}}"`,
}
testGlobs(t, paths, rep)

paths = []string{"c/a.json", "d/b.json"}
rep.Path = "*/*.json"
testGlobs(t, paths, rep)

paths = []string{"e/a.json", "f/b.json"}
rep.Path = "[ef]/[ab].json"
testGlobs(t, paths, rep)

paths = []string{"f.json", "b.jsop", "c.jsof"}
rep.Path = "*.jso?"
testGlobs(t, paths, rep)
}

func TestBadGlob(t *testing.T) {
rep := Replacement{
Path: `[]`,
}

err := rep.Execute(ReplaceData{})
then.NotNil(t, err)
}

0 comments on commit fcbd67e

Please sign in to comment.