generated from jacobtomlinson/go-container-action
-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
133 lines (111 loc) · 2.74 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
package main
import (
"errors"
"fmt"
"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 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 := os.ReadFile(path)
check(readErr)
var newContents string
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 := os.WriteFile(path, []byte(newContents), 0)
check(writeErr)
return true, nil
}
}
return false, nil
}
func setGithubEnvOutput(key string, value int) {
outputFilename := os.Getenv("GITHUB_OUTPUT")
f, err := os.OpenFile(outputFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := fmt.Fprintf(f, "%s=%d\n", key, value); err != nil {
log.Println(err)
}
}
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 {
replace = ""
}
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++
}
}
setGithubEnvOutput("modifiedFiles", modifiedCount)
}