8
8
"os"
9
9
"os/exec"
10
10
"path/filepath"
11
+ "regexp"
11
12
"strconv"
12
13
"strings"
13
14
@@ -16,7 +17,20 @@ import (
16
17
"github.com/hashicorp/hcl"
17
18
)
18
19
19
- func MergeEnvTf (email string , data []map [string ]interface {}) error {
20
+ func InitializeFolder (folderPath string ) error {
21
+ err := os .RemoveAll (folderPath )
22
+ if err != nil {
23
+ return err
24
+ }
25
+ err = os .MkdirAll (folderPath , os .ModePerm )
26
+ if err != nil {
27
+ return err
28
+ }
29
+
30
+ return nil
31
+ }
32
+
33
+ func MergeEnvTf (userFolderPath string , data []map [string ]interface {}) error {
20
34
for _ , item := range data {
21
35
folderPath := item ["type" ].(string )
22
36
@@ -32,12 +46,12 @@ func MergeEnvTf(email string, data []map[string]interface{}) error {
32
46
return err
33
47
}
34
48
35
- userMainPath := filepath .Join ("usertf" , email , "main.tf" )
49
+ userMainPath := filepath .Join (userFolderPath , "main.tf" )
36
50
if err := AppendFile (userMainPath , mainContent ); err != nil {
37
51
return err
38
52
}
39
53
40
- userVarPath := filepath .Join ("usertf" , email , "variables.tf" )
54
+ userVarPath := filepath .Join (userFolderPath , "variables.tf" )
41
55
if err := AppendFile (userVarPath , varContent ); err != nil {
42
56
return err
43
57
}
@@ -52,7 +66,7 @@ func AppendFile(filePath string, content []byte) error {
52
66
return ioutil .WriteFile (filePath , content , 0o644 )
53
67
}
54
68
55
- newContent := append (exist , []byte ("\n " )... )
69
+ newContent := append (exist , []byte ("\n \n " )... )
56
70
newContent = append (newContent , content ... )
57
71
58
72
err = ioutil .WriteFile (filePath , newContent , 0o644 )
@@ -63,15 +77,18 @@ func AppendFile(filePath string, content []byte) error {
63
77
return nil
64
78
}
65
79
66
- func CreateTfvars (email string , data []map [string ]interface {}) error {
80
+ func CreateTfvars (userFolderPath string , data []map [string ]interface {}) error {
67
81
var v map [string ]interface {}
68
- variablesFile := filepath .Join ("usertf" , email , "variables.tf" )
82
+ variablesFile := filepath .Join (userFolderPath , "variables.tf" )
69
83
existingVariables , err := ioutil .ReadFile (variablesFile )
70
84
if err != nil {
71
85
return err
72
86
}
73
87
74
- err = hcl .Unmarshal (existingVariables , & v )
88
+ re := regexp .MustCompile (`\{[^{}]*\}` )
89
+ fileContent := re .ReplaceAllString (string (existingVariables ), "{}" )
90
+
91
+ err = hcl .Unmarshal ([]byte (fileContent ), & v )
75
92
if err != nil {
76
93
return err
77
94
}
@@ -141,7 +158,7 @@ func CreateTfvars(email string, data []map[string]interface{}) error {
141
158
tfvars .WriteString ("\n " )
142
159
}
143
160
144
- writePath := filepath .Join ("usertf" , email , "terraform.tfvars" )
161
+ writePath := filepath .Join (userFolderPath , "terraform.tfvars" )
145
162
err = ioutil .WriteFile (writePath , []byte (tfvars .String ()), 0o644 )
146
163
if err != nil {
147
164
return err
0 commit comments