File tree Expand file tree Collapse file tree 7 files changed +175
-133
lines changed Expand file tree Collapse file tree 7 files changed +175
-133
lines changed Original file line number Diff line number Diff line change
1
+ * .tf
2
+ .DS_Store
3
+ .DS_Store ?
4
+ * .swp
5
+ .vscode /*
6
+ ! .vscode /settings.json
7
+ ! .vscode /tasks.json
8
+ ! .vscode /launch.json
9
+ ! .vscode /extensions.json
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "os"
5
+ "path/filepath"
6
+
7
+ log "github.com/sirupsen/logrus"
8
+ )
9
+
10
+ func getAllFiles (ext string ) ([]string , error ) {
11
+ dir , err := os .Getwd ()
12
+ checkError (err )
13
+ var files []string
14
+ log .Infof ("Finding files in %q directory" , dir )
15
+ files , err = filepath .Glob (tfFileExt )
16
+ checkError (err )
17
+
18
+ if len (files ) == 0 {
19
+ log .Infof ("No files with .tf extensions found in %q" , dir )
20
+ os .Exit (0 )
21
+ }
22
+ return files , nil
23
+ }
24
+
25
+ func fileExists (name string ) bool {
26
+ if _ , err := os .Stat (name ); err == nil {
27
+ return true
28
+ }
29
+ return false
30
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "bufio"
5
+ "html/template"
6
+ "os"
7
+ "strings"
8
+ "sync"
9
+
10
+ log "github.com/sirupsen/logrus"
11
+ )
12
+
13
+ var replacer * strings.Replacer
14
+ var varPrefix = "var."
15
+ var tfFileExt = "*.tf"
16
+
17
+ var dstFile = "./variables.tf"
18
+ var varTemplate = template .Must (template .New ("var_file" ).Parse (`{{range .}}
19
+ variable "{{ . }}" {
20
+ description = ""
21
+ }
22
+ {{end}}
23
+ ` ))
24
+
25
+ func init () {
26
+ replacer = strings .NewReplacer (":" , "." ,
27
+ "]" , "" ,
28
+ "}" , "" ,
29
+ "{" , "" ,
30
+ "\" " , "" ,
31
+ ")" , "" ,
32
+ "(" , "" ,
33
+ "[" , "" ,
34
+ "," , "" ,
35
+ "var." , "" ,
36
+ " " , "" ,
37
+ )
38
+ }
39
+ func main () {
40
+ if fileExists (dstFile ) {
41
+ userPromt ()
42
+ }
43
+
44
+ tfFiles , err := getAllFiles (tfFileExt )
45
+ checkError (err )
46
+ var wg sync.WaitGroup
47
+ messages := make (chan string )
48
+ wg .Add (len (tfFiles ))
49
+ t := & terraformVars {}
50
+
51
+ for _ , file := range tfFiles {
52
+ go func (file string ) {
53
+ defer wg .Done ()
54
+ fileHandle , _ := os .Open (file )
55
+ defer fileHandle .Close ()
56
+ fileScanner := bufio .NewScanner (fileHandle )
57
+ for fileScanner .Scan () {
58
+ messages <- fileScanner .Text ()
59
+ }
60
+ }(file )
61
+ }
62
+ go func () {
63
+ for text := range messages {
64
+ t .matchVarPref (text , varPrefix )
65
+ }
66
+ }()
67
+ wg .Wait ()
68
+ f , err := os .Create (dstFile )
69
+ checkError (err )
70
+
71
+ err = varTemplate .Execute (f , t .Variables )
72
+ checkError (err )
73
+ log .Infof ("Variables are generated to %q file" , dstFile )
74
+
75
+ }
Original file line number Diff line number Diff line change
1
+ package main
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+
7
+ log "github.com/sirupsen/logrus"
8
+ )
9
+
10
+ func checkError (e error ) {
11
+ if e != nil {
12
+ log .Fatal (e )
13
+ }
14
+ }
15
+
16
+ func containsElement (slice []string , value string ) bool {
17
+ if len (slice ) == 0 {
18
+ return false
19
+ }
20
+ for _ , s := range slice {
21
+ if value == s {
22
+ return true
23
+ }
24
+ }
25
+ return false
26
+ }
27
+
28
+ func userPromt () {
29
+ var response string
30
+ log .Warnf ("File %q already exists, type yes if you want replace" , dstFile )
31
+ fmt .Print ("-> " )
32
+ _ , err := fmt .Scanln (& response )
33
+ checkError (err )
34
+ if response != "yes" {
35
+ os .Exit (0 )
36
+ }
37
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "regexp"
5
+ "strings"
6
+ )
7
+
8
+ type terraformVars struct {
9
+ Variables []string
10
+ }
11
+
12
+ func (t * terraformVars ) matchVarPref (row , varPrefix string ) {
13
+ if strings .Contains (row , varPrefix ) {
14
+ pattern := regexp .MustCompile (`var.([a-z?_]+)` )
15
+ match := pattern .FindAllStringSubmatch (row , 1 )
16
+ if len (match ) != 0 {
17
+ res := replacer .Replace (match [0 ][0 ])
18
+ if ! containsElement (t .Variables , res ) {
19
+ t .Variables = append (t .Variables , res )
20
+ }
21
+ }
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments