Copy over environment.go and generate .env file in starter
This commit is contained in:
Vendored
+79
@@ -2,7 +2,9 @@ package env
|
||||
|
||||
import (
|
||||
. "ersteller-lib"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
@@ -100,3 +102,80 @@ func EnvKeys() []string {
|
||||
EnvKeyKeycloakClientSecret,
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateEnvFile creates a .env file template with all required environment variables
|
||||
// from the EnvKeys function. The file will contain comments and default values where appropriate.
|
||||
func GenerateEnvFile(rootPath string, overwrite bool) error {
|
||||
envPath := filepath.Join(rootPath, ".env")
|
||||
Debug("envPath:", envPath)
|
||||
if !overwrite {
|
||||
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 fmt.Errorf(".env already exists at: %s", envPath)
|
||||
}
|
||||
}
|
||||
file, err := os.Create(envPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create .env file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Write header comment
|
||||
_, err = file.WriteString("# Environment Variables Configuration\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to .env file: %w", err)
|
||||
}
|
||||
_, err = file.WriteString("# Generated from EnvKeys function\n\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to .env file: %w", err)
|
||||
}
|
||||
|
||||
// Get all environment keys
|
||||
keys := EnvKeys()
|
||||
|
||||
// Define default values and comments for specific keys
|
||||
defaults := map[string]string{
|
||||
EnvKeyRepositoryType: "postgres",
|
||||
EnvKeyIsLocal: "false",
|
||||
EnvKeyIsDev: "false",
|
||||
}
|
||||
|
||||
comments := map[string]string{
|
||||
EnvKeyRepositoryType: "# Repository type (postgres, sqlite, etc.)",
|
||||
EnvKeyDatabaseURL: "# Database connection URL",
|
||||
EnvKeySessionSecret: "# Secret key for session management",
|
||||
EnvKeyBaseURL: "# Base URL of the application",
|
||||
EnvKeyIsLocal: "# Set to true for local development",
|
||||
EnvKeyIsDev: "# Set to true for development mode",
|
||||
EnvKeyGoogleClientID: "# Google OAuth client ID",
|
||||
EnvKeyGoogleClientSecret: "# Google OAuth client secret",
|
||||
EnvKeyGoogleRedirectURL: "# Google OAuth redirect URL",
|
||||
EnvKeyKeycloakDiscoveryURL: "# Keycloak discovery URL",
|
||||
EnvKeyKeycloakClientID: "# Keycloak client ID",
|
||||
EnvKeyKeycloakClientSecret: "# Keycloak client secret",
|
||||
}
|
||||
|
||||
// Write each environment variable
|
||||
for _, key := range keys {
|
||||
// Write comment if available
|
||||
if comment, exists := comments[key]; exists {
|
||||
_, err = file.WriteString(comment + "\n")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to .env file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Write the environment variable with default value if available
|
||||
if defaultValue, exists := defaults[key]; exists {
|
||||
_, err = file.WriteString(fmt.Sprintf("%s=%s\n", key, defaultValue))
|
||||
} else {
|
||||
_, err = file.WriteString(fmt.Sprintf("%s=\n", key))
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to .env file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user