package main import ( "errors" "ersteller-lib/starter/env" "fmt" "os" "path/filepath" "strings" ) // findRepoRoot walks up from the current working directory until it finds a go.mod file. // It returns the directory containing go.mod, or an error if not found. func findRepoRoot() (string, error) { wd, err := os.Getwd() if err != nil { return "", err } curr := wd for { if _, err := os.Stat(filepath.Join(curr, "go.mod")); err == nil { return curr, nil } parent := filepath.Dir(curr) if parent == curr { break } curr = parent } return "", errors.New("could not locate repository root (go.mod not found in any parent directory)") } func main() { root, err := findRepoRoot() if err != nil { fmt.Println("Error:", err) os.Exit(1) } envPath := filepath.Join(root, ".env") if _, err := os.Stat(envPath); err == nil { fmt.Println(".env already exists at:", envPath) fmt.Println("No changes made. Delete or move the existing .env to regenerate.") return } // Build .env content dynamically from keys exposed by env package // so we don't duplicate the list here. keys := env.EnvKeys() builder := &strings.Builder{} fmt.Fprintln(builder, "# Environment configuration for Salezenify") fmt.Fprintln(builder, "# Generated by cli/generate_env.go") fmt.Fprintln(builder, "# Fill in the appropriate values for your environment.") fmt.Fprintln(builder) // Optional defaults can be set here; leave blank for most keys to avoid assumptions. defaults := map[string]string{ "REPOSITORY_TYPE": "postgres", } for _, k := range keys { v := defaults[k] if v == "" { fmt.Fprintf(builder, "%s=\n", k) } else { fmt.Fprintf(builder, "%s=%s\n", k, v) } } content := builder.String() if err := os.WriteFile(envPath, []byte(content), 0600); err != nil { fmt.Println("Failed to write .env:", err) os.Exit(1) } fmt.Println(".env file created at:", envPath) fmt.Println("Review and update the values as needed.") }