-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
158 lines (137 loc) Β· 3.87 KB
/
main.go
File metadata and controls
158 lines (137 loc) Β· 3.87 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/charmbracelet/huh"
)
func main() {
// Ask for the .env file path
var envFilePath string
huh.NewInput().
Title("π `.env` File").
Description("Enter the path to your `.env` file (Press Enter to use current directory)").
Value(&envFilePath).
Run()
if envFilePath == "" {
envFilePath = "./.env"
}
// Read .env file
variables, descriptions, err := parseEnvFromFile(envFilePath)
if err != nil {
fmt.Println("β Error reading .env file:", err)
os.Exit(1)
}
// Ask for the .tfvars file path
var tfvarsPath string
huh.NewInput().
Title("πΎ `.tfvars` File").
Description("Enter the path to save `.tfvars` (Press Enter to use current directory)").
Value(&tfvarsPath).
Run()
if tfvarsPath == "" {
tfvarsPath = "./terraform.tfvars"
}
// Generate `.tfvars` file
err = generateTfvarsFile(tfvarsPath, variables)
if err != nil {
fmt.Println("β Error creating `.tfvars` file:", err)
os.Exit(1)
}
// Ask if the user wants to create `variables.tf`
var createVarFile bool
huh.NewConfirm().
Title("π Generate `variables.tf`").
Description("Do you want to generate a `variables.tf` file?").
Affirmative("Yes").
Negative("No").
Value(&createVarFile).
Run()
// Generate `variables.tf` if requested
if createVarFile {
var variablesTfPath string
huh.NewInput().
Title("π `variables.tf` File").
Description("Enter the path to save `variables.tf` (Press Enter to use current directory)").
Value(&variablesTfPath).
Run()
if variablesTfPath == "" {
variablesTfPath = "./variables.tf"
}
err := generateVariablesTfFile(variablesTfPath, variables, descriptions)
if err != nil {
fmt.Println("β Error creating `variables.tf` file:", err)
os.Exit(1)
}
}
fmt.Println("\nβ
Process completed successfully.")
}
// Reads and parses a `.env` file with support for comments
func parseEnvFromFile(filePath string) (map[string]string, map[string]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, nil, err
}
defer file.Close()
return parseEnv(file)
}
// Reads environment variables from a file with support for inline comments
func parseEnv(r io.Reader) (map[string]string, map[string]string, error) {
vars := make(map[string]string)
descriptions := make(map[string]string) // Store comments as descriptions
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
valueParts := strings.SplitN(parts[1], "#", 2)
value := strings.TrimSpace(valueParts[0])
vars[key] = value
// Store the comment (if present)
if len(valueParts) > 1 {
descriptions[key] = strings.TrimSpace(valueParts[1])
}
}
}
return vars, descriptions, scanner.Err()
}
// Generates the `.tfvars` file
func generateTfvarsFile(filePath string, variables map[string]string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for key, value := range variables {
line := fmt.Sprintf("%s = %s\n", key, value)
writer.WriteString(line)
}
writer.Flush()
return nil
}
// Generates the `variables.tf` file with descriptions from `.env`
func generateVariablesTfFile(filePath string, variables map[string]string, descriptions map[string]string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for key := range variables {
desc := descriptions[key] // Get the description from `.env`
if desc == "" {
desc = "No description available"
}
line := fmt.Sprintf("variable \"%s\" {\n description = \"%s\"\n type = string\n}\n\n", key, desc)
writer.WriteString(line)
}
writer.Flush()
return nil
}