-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (42 loc) · 980 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
)
func main() {
if len(os.Args) == 1 {
panic("Need file")
}
if len(os.Args) == 2 {
panic("Need output file")
}
filename := os.Args[1]
output := os.Args[2]
yml, err := ioutil.ReadFile(filename)
if err != nil {
panic(err.Error())
}
r, err := regexp.Compile(`(\${.*})`)
if err != nil {
panic(err.Error())
}
ymlText := string(yml)
varBytes := r.FindAll(yml, 10)
for _, varByte := range varBytes {
fmt.Println("Finded variable:", string(varByte))
varPattern, err := regexp.Compile(`([A-Z_-az]+)`)
if err != nil {
panic(err.Error())
}
varName := string(varPattern.Find(varByte))
ymlPattern := regexp.MustCompile(fmt.Sprintf(`\${%s}`, varName))
ymlText = ymlPattern.ReplaceAllString(ymlText, os.Getenv(varName))
}
err = ioutil.WriteFile(output, []byte(ymlText), 0644)
if err != nil {
panic(err.Error())
}
fmt.Println("File has been successfully generated:", output)
}