-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
67 lines (51 loc) · 1.25 KB
/
reader.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
package GoTextReplace
import (
"bufio"
"os"
"regexp"
"gopkg.in/yaml.v2"
)
type Tags struct {
Names []string
}
// YAML file and source file path should be assigned to the Struct, then Mapper method needs to be called to execute the task
type TextReplace struct {
Tags Tags
Filename string
FileObj *os.File
YAMLfile string
OutputPath string
OutputFileName string
Keys map[string]interface{}
}
// Read the file and get the Tags
func (t *TextReplace) Reader() (Tags, *os.File, error) {
file, err := os.Open(t.Filename)
if err != nil {
return Tags{}, file, err
}
scanner := bufio.NewScanner(file)
var Tags Tags
tagRegex := regexp.MustCompile(`\{\{(.+?)\}\}`)
for scanner.Scan() {
line := scanner.Text()
matches := tagRegex.FindAllStringSubmatch(line, -1)
for _, match := range matches {
Tags.Names = append(Tags.Names, match[1])
}
}
err = scanner.Err()
if err != nil {
return Tags, file, err
}
return Tags, file, nil
}
// Get YAML Key-Values from file
func (t *TextReplace) GetYAMLValues() (map[string]interface{}, error) {
var data map[string]interface{}
ymlFile, err := os.ReadFile(t.YAMLfile)
CheckError(err)
err = yaml.Unmarshal(ymlFile, &data)
CheckError(err)
return data, nil
}