Files
lst/controller/load_env.go

46 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/joho/godotenv"
)
func loadEnv() {
exePath, _ := os.Executable()
exeDir := filepath.Dir(exePath)
// Normalize both to lowercase absolute paths for Windows safety
exePathLower := strings.ToLower(exePath)
tempDirLower := strings.ToLower(filepath.Clean(os.TempDir()))
// Heuristic: if exe lives *inside* the system temp dir → assume go run
if strings.HasPrefix(exePathLower, tempDirLower) {
fmt.Println("Detected go run loading ../.env")
err := godotenv.Load("../.env")
if err != nil {
fmt.Println("ERROR loading .env:", err)
} else {
fmt.Println(".env successfully loaded")
}
return
}
// Otherwise → normal compiled exe
fmt.Println("Detected compiled exe loading exeDir/.env")
if err := godotenv.Load(filepath.Join(exeDir, ".env")); err != nil {
fmt.Println("Didn't find exeDir/.env trying ../.env as fallback")
err := godotenv.Load("../.env")
if err != nil {
fmt.Println("ERROR loading .env:", err)
} else {
fmt.Println(".env successfully loaded")
}
}
}