Skip to content

Commit

Permalink
using only huh
Browse files Browse the repository at this point in the history
  • Loading branch information
henrriusdev committed Feb 23, 2025
1 parent ca03cc7 commit 82f92bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
39 changes: 27 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
}

// Read .env file
variables, err := parseEnvFromFile(envFilePath)
variables, descriptions, err := parseEnvFromFile(envFilePath)
if err != nil {
fmt.Println("❌ Error reading .env file:", err)
os.Exit(1)
Expand Down Expand Up @@ -69,7 +69,7 @@ func main() {
variablesTfPath = "./variables.tf"
}

err := generateVariablesTfFile(variablesTfPath, variables)
err := generateVariablesTfFile(variablesTfPath, variables, descriptions)
if err != nil {
fmt.Println("❌ Error creating `variables.tf` file:", err)
os.Exit(1)
Expand All @@ -79,19 +79,21 @@ func main() {
fmt.Println("\n✅ Process completed successfully.")
}

// Reads and parses a `.env` file
func parseEnvFromFile(filePath string) (map[string]string, error) {
// 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, err
return nil, nil, err
}
defer file.Close()
return parseEnv(file)
}

// Reads environment variables from a file
func parseEnv(r io.Reader) (map[string]string, error) {
// 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())
Expand All @@ -100,10 +102,18 @@ func parseEnv(r io.Reader) (map[string]string, error) {
}
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
vars[parts[0]] = parts[1]
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, scanner.Err()
return vars, descriptions, scanner.Err()
}

// Generates the `.tfvars` file
Expand All @@ -124,8 +134,8 @@ func generateTfvarsFile(filePath string, variables map[string]string) error {
return nil
}

// Generates the `variables.tf` file
func generateVariablesTfFile(filePath string, variables map[string]string) error {
// 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
Expand All @@ -134,7 +144,12 @@ func generateVariablesTfFile(filePath string, variables map[string]string) error

writer := bufio.NewWriter(file)
for key := range variables {
line := fmt.Sprintf("variable \"%s\" {\n description = \"\"\n type = string\n}\n\n", key)
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()
Expand Down
44 changes: 16 additions & 28 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import (
)

func TestParseEnv(t *testing.T) {
input := "VAR1=value1\nVAR2=value2\nVAR3=value3"
input := "VAR1=value1 # First variable\nVAR2=value2 # Second variable\nVAR3=value3"
r := strings.NewReader(input)
vars, err := parseEnv(r)
vars, descriptions, err := parseEnv(r)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if vars["VAR1"] != "value1" || vars["VAR2"] != "value2" || vars["VAR3"] != "value3" {
t.Errorf("Parsed values do not match expected output: %v", vars)
}

if descriptions["VAR1"] != "First variable" || descriptions["VAR2"] != "Second variable" {
t.Errorf("Parsed descriptions do not match expected output: %v", descriptions)
}
}

func TestParseEnvFromFile(t *testing.T) {
fileContent := "TEST_VAR=hello\nSECOND_VAR=world"
fileContent := "TEST_VAR=hello # A test variable\nSECOND_VAR=world"
filePath := "test.env"

err := os.WriteFile(filePath, []byte(fileContent), 0o644)
Expand All @@ -29,37 +33,17 @@ func TestParseEnvFromFile(t *testing.T) {
}
defer os.Remove(filePath)

vars, err := parseEnvFromFile(filePath)
vars, descriptions, err := parseEnvFromFile(filePath)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if vars["TEST_VAR"] != "hello" || vars["SECOND_VAR"] != "world" {
t.Errorf("Parsed values do not match expected output: %v", vars)
}
}

func TestGenerateTfvarsFile(t *testing.T) {
variables := map[string]string{
"VAR1": "value1",
"VAR2": "value2",
}
filePath := "test.tfvars"

err := generateTfvarsFile(filePath, variables)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer os.Remove(filePath)

content, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}

expected := "VAR1 = \"value1\"\nVAR2 = \"value2\"\n"
if string(content) != expected {
t.Errorf("File content mismatch. Expected:\n%s\nGot:\n%s", expected, string(content))
if descriptions["TEST_VAR"] != "A test variable" {
t.Errorf("Parsed descriptions do not match expected output: %v", descriptions)
}
}

Expand All @@ -68,9 +52,13 @@ func TestGenerateVariablesTfFile(t *testing.T) {
"MY_VAR": "",
"ANOTHER_VAR": "",
}
descriptions := map[string]string{
"MY_VAR": "A sample variable",
"ANOTHER_VAR": "Another sample variable",
}
filePath := "test.variables.tf"

err := generateVariablesTfFile(filePath, variables)
err := generateVariablesTfFile(filePath, variables, descriptions)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand All @@ -81,7 +69,7 @@ func TestGenerateVariablesTfFile(t *testing.T) {
t.Fatalf("Error reading file: %v", err)
}

expected := "variable \"MY_VAR\" {\n description = \"\"\n type = string\n}\n\nvariable \"ANOTHER_VAR\" {\n description = \"\"\n type = string\n}\n\n"
expected := "variable \"MY_VAR\" {\n description = \"A sample variable\"\n type = string\n}\n\nvariable \"ANOTHER_VAR\" {\n description = \"Another sample variable\"\n type = string\n}\n\n"
if string(content) != expected {
t.Errorf("File content mismatch. Expected:\n%s\nGot:\n%s", expected, string(content))
}
Expand Down

0 comments on commit 82f92bd

Please sign in to comment.