forked from jacobtomlinson/gha-find-replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (112 loc) · 2.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/gobwas/glob"
)
var ErrEnvVarEmpty = errors.New("getenv: environment variable empty")
func getenvStr(key string) (string, error) {
v := os.Getenv(key)
if v == "" {
return v, ErrEnvVarEmpty
}
return v, nil
}
func getenvInt(key string, def int) (int, error) {
s, err := getenvStr(key)
if err != nil {
return 0, err
}
v, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return v, nil
}
func getenvBool(key string) (bool, error) {
s, err := getenvStr(key)
if err != nil {
return true, err
}
v, err := strconv.ParseBool(s)
if err != nil {
return true, err
}
return v, nil
}
func check(e error) {
if e != nil {
log.Fatalf("error: %v", e)
}
}
func listFiles(include string, exclude string) ([]string, error) {
fileList := []string{}
err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error {
if doesFileMatch(path, include, exclude) {
fileList = append(fileList, path)
}
return nil
})
return fileList, err
}
func doesFileMatch(path string, include string, exclude string) bool {
if fi, err := os.Stat(path); err == nil && !fi.IsDir() {
includeGlob := glob.MustCompile(include)
excludeGlob := glob.MustCompile(exclude)
return includeGlob.Match(path) && !excludeGlob.Match(path)
}
return false
}
func findAndReplace(path string, find string, replace string, regex bool) (bool, error) {
if find != replace {
read, readErr := ioutil.ReadFile(path)
check(readErr)
var newContents = ""
if regex {
re := regexp.MustCompile(find)
newContents = re.ReplaceAllString(string(read), replace)
} else {
newContents = strings.ReplaceAll(string(read), find, replace)
}
if newContents != string(read) {
writeErr := ioutil.WriteFile(path, []byte(newContents), 0)
check(writeErr)
return true, nil
}
}
return false, nil
}
func main() {
include, _ := getenvStr("INPUT_INCLUDE")
exclude, _ := getenvStr("INPUT_EXCLUDE")
find, findErr := getenvStr("INPUT_FIND")
replace, replaceErr := getenvStr("INPUT_REPLACE")
regex, regexErr := getenvBool("INPUT_REGEX")
if findErr != nil {
panic(errors.New("gha-find-replace: expected with.find to be a string"))
}
if replaceErr != nil {
panic(errors.New("gha-find-replace: expected with.replace to be a string"))
}
if regexErr != nil {
regex = true
}
files, filesErr := listFiles(include, exclude)
check(filesErr)
modifiedCount := 0
for _, path := range files {
modified, findAndReplaceErr := findAndReplace(path, find, replace, regex)
check(findAndReplaceErr)
if modified {
modifiedCount++
}
}
//fmt.Println(fmt.Sprintf( echo `modifiedFiles`=modifiedCount >> $GITHUB_OUTPUT))
}