96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// GenerateDotEnv creates a new .env file with all environment variables and their defaults
|
|
func GenerateDotEnv(filename string) error {
|
|
envVars := GetAllEnvVars()
|
|
|
|
file, err := os.Create(filename)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create file: %w", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
// Write header
|
|
fmt.Fprintln(file, "# Environment Configuration")
|
|
fmt.Fprintln(file, "# Generated by Project Reshoot")
|
|
fmt.Fprintln(file, "#")
|
|
fmt.Fprintln(file, "# Variables marked as (required) must be set")
|
|
fmt.Fprintln(file, "# Variables with defaults can be left commented out to use the default value")
|
|
fmt.Fprintln(file)
|
|
|
|
// Group by prefix
|
|
groups := map[string][]EnvVar{
|
|
"DB_": {},
|
|
"HWS_": {},
|
|
"HWSAUTH_": {},
|
|
"TMDB_": {},
|
|
"LOG_": {},
|
|
}
|
|
|
|
for _, ev := range envVars {
|
|
assigned := false
|
|
for prefix := range groups {
|
|
if strings.HasPrefix(ev.Name, prefix) {
|
|
groups[prefix] = append(groups[prefix], ev)
|
|
assigned = true
|
|
break
|
|
}
|
|
}
|
|
if !assigned {
|
|
// Handle ungrouped vars
|
|
if _, ok := groups["OTHER"]; !ok {
|
|
groups["OTHER"] = []EnvVar{}
|
|
}
|
|
groups["OTHER"] = append(groups["OTHER"], ev)
|
|
}
|
|
}
|
|
|
|
// Print each group
|
|
groupOrder := []string{"DB_", "HWS_", "HWSAUTH_", "TMDB_", "LOG_", "OTHER"}
|
|
groupTitles := map[string]string{
|
|
"DB_": "Database Configuration",
|
|
"HWS_": "HTTP Web Server Configuration",
|
|
"HWSAUTH_": "Authentication Configuration",
|
|
"TMDB_": "TMDB API Configuration",
|
|
"LOG_": "Logging Configuration",
|
|
"OTHER": "Other Configuration",
|
|
}
|
|
|
|
for _, prefix := range groupOrder {
|
|
vars, ok := groups[prefix]
|
|
if !ok || len(vars) == 0 {
|
|
continue
|
|
}
|
|
|
|
fmt.Fprintf(file, "# %s\n", groupTitles[prefix])
|
|
fmt.Fprintln(file, strings.Repeat("#", len(groupTitles[prefix])+2))
|
|
|
|
for _, ev := range vars {
|
|
// Write description as comment
|
|
if ev.Required {
|
|
fmt.Fprintf(file, "# %s (required)\n", ev.Description)
|
|
// Leave required variables uncommented but empty
|
|
fmt.Fprintf(file, "%s=\n", ev.Name)
|
|
} else if ev.HasDefault {
|
|
fmt.Fprintf(file, "# %s\n", ev.Description)
|
|
// Comment out variables with defaults
|
|
fmt.Fprintf(file, "# %s=%s\n", ev.Name, ev.Default)
|
|
} else {
|
|
fmt.Fprintf(file, "# %s\n", ev.Description)
|
|
// Optional variables without defaults are commented out
|
|
fmt.Fprintf(file, "# %s=\n", ev.Name)
|
|
}
|
|
fmt.Fprintln(file)
|
|
}
|
|
fmt.Fprintln(file)
|
|
}
|
|
|
|
return nil
|
|
}
|