From b6eea7a791a616dacdf390300024449dbb7d54c7 Mon Sep 17 00:00:00 2001 From: michaelpede Date: Fri, 27 Aug 2021 15:43:35 -0700 Subject: [PATCH 1/6] Support for multiple replaces #15 --- main.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 9e6b952..04f4c73 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "io/ioutil" "log" "os" + "sort" + "strings" "path/filepath" "regexp" ) @@ -59,17 +61,45 @@ func main() { find := os.Getenv("INPUT_FIND") replace := os.Getenv("INPUT_REPLACE") + reserved := []string{"INCLUDE","EXCLUDE","FIND","REPLACE"} + + //INPUT_PREFIX := "INPUT_" + files, filesErr := listFiles(include, exclude) check(filesErr) modifiedCount := 0 - for _, path := range files { - modified, findAndReplaceErr := findAndReplace(path, find, replace) - check(findAndReplaceErr) + if find!="" && replace!="" { + for _, path := range files { + modified, findAndReplaceErr := findAndReplace(path, find, replace) + check(findAndReplaceErr) + + if modified { + modifiedCount++ + } + } + } + + for _, pair := range os.Environ() { + if strings.Contains(pair,"INPUT_") { + keyValue := strings.SplitN(pair,"=",2) + find := keyValue[0] + replace := keyValue[1] + + i := sort.Search(len(reserved), func(i int) bool { return reserved[i] == find }) - if modified { - modifiedCount++ + if i == len(reserved) { + for _, path := range files { + modified, findAndReplaceErr := findAndReplace(path, find, replace) + check(findAndReplaceErr) + + if modified { + modifiedCount++ + } + } + } + } } From 49de04d1c8928e6dbd4e258f20c89bd1cc79f9ab Mon Sep 17 00:00:00 2001 From: michaelpede Date: Fri, 27 Aug 2021 15:47:20 -0700 Subject: [PATCH 2/6] tests --- .github/workflows/integration.yml | 8 ++++++++ .github/workflows/publish.yml | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 522aa01..39b0a3d 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -16,7 +16,15 @@ jobs: include: "test/" find: "world" replace: "there" + - name: Self test + id: selftest2 + uses: ./ + with: + include: "test/" + Hello: "there.." + - name: Check outputs and modified files run: | test "${{ steps.selftest.outputs.modifiedFiles }}" == "1" + test "${{ steps.selftest2.outputs.modifiedFiles }}" == "1" grep "Hello there" test/hello.txt diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3605df4..03b5294 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -13,7 +13,7 @@ jobs: - name: Publish to Registry uses: elgohr/Publish-Docker-Github-Action@master with: - name: jacobtomlinson/gha-find-replace + name: mpede/gha-find-replace username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} tags: "latest,${{ env.RELEASE_VERSION }}" From c8b905643642ac118466938b4ce363916e861abe Mon Sep 17 00:00:00 2001 From: michaelpede Date: Fri, 27 Aug 2021 16:10:57 -0700 Subject: [PATCH 3/6] has case-insensitivity --- .github/workflows/integration.yml | 2 +- main.go | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 39b0a3d..1a4ac73 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -27,4 +27,4 @@ jobs: run: | test "${{ steps.selftest.outputs.modifiedFiles }}" == "1" test "${{ steps.selftest2.outputs.modifiedFiles }}" == "1" - grep "Hello there" test/hello.txt + grep "there.. there" test/hello.txt diff --git a/main.go b/main.go index 04f4c73..3c5a3c9 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,8 @@ func doesFileMatch(path string, include string, exclude string) bool { } func findAndReplace(path string, find string, replace string) (bool, error) { + fmt.Println("looking for:") + fmt.Println(find) if find != replace { read, readErr := ioutil.ReadFile(path) check(readErr) @@ -63,7 +65,7 @@ func main() { reserved := []string{"INCLUDE","EXCLUDE","FIND","REPLACE"} - //INPUT_PREFIX := "INPUT_" + INPUT_PREFIX := "INPUT_" files, filesErr := listFiles(include, exclude) check(filesErr) @@ -82,18 +84,20 @@ func main() { } for _, pair := range os.Environ() { - if strings.Contains(pair,"INPUT_") { + if strings.Contains(pair,INPUT_PREFIX) { keyValue := strings.SplitN(pair,"=",2) - find := keyValue[0] + find := strings.SplitN(keyValue[0],"_",2)[1] replace := keyValue[1] i := sort.Search(len(reserved), func(i int) bool { return reserved[i] == find }) if i == len(reserved) { + files, filesErr := listFiles(include, exclude) + check(filesErr) for _, path := range files { - modified, findAndReplaceErr := findAndReplace(path, find, replace) + modified, findAndReplaceErr := findAndReplace(path, "(?i)"+find, replace) check(findAndReplaceErr) - + if modified { modifiedCount++ } From b71fb56e808d112f02f97d64400ba58612d9f6fe Mon Sep 17 00:00:00 2001 From: michaelpede Date: Fri, 27 Aug 2021 16:14:08 -0700 Subject: [PATCH 4/6] proper exclusion of reserved cases --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 3c5a3c9..f292269 100644 --- a/main.go +++ b/main.go @@ -91,7 +91,7 @@ func main() { i := sort.Search(len(reserved), func(i int) bool { return reserved[i] == find }) - if i == len(reserved) { + if i == len(reserved) && reserved[i] != find { files, filesErr := listFiles(include, exclude) check(filesErr) for _, path := range files { From 0b5fad0cfcb23ce7333c0040d60042a9792f2505 Mon Sep 17 00:00:00 2001 From: michaelpede Date: Fri, 27 Aug 2021 16:27:55 -0700 Subject: [PATCH 5/6] stupid function didn't do what I thought it did This is my first time writing GO.... --- main.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index f292269..d36788b 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "log" "os" - "sort" "strings" "path/filepath" "regexp" @@ -89,9 +88,14 @@ func main() { find := strings.SplitN(keyValue[0],"_",2)[1] replace := keyValue[1] - i := sort.Search(len(reserved), func(i int) bool { return reserved[i] == find }) + found := false + for i := 0; i Date: Fri, 27 Aug 2021 16:29:26 -0700 Subject: [PATCH 6/6] don't need debug statements --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index d36788b..f44a92f 100644 --- a/main.go +++ b/main.go @@ -37,8 +37,6 @@ func doesFileMatch(path string, include string, exclude string) bool { } func findAndReplace(path string, find string, replace string) (bool, error) { - fmt.Println("looking for:") - fmt.Println(find) if find != replace { read, readErr := ioutil.ReadFile(path) check(readErr)