Files
worker/config/env/env.go
Gleb Tv 2c7a0236da feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
2026-07-13 17:55:14 +03:00

96 строки
1.8 KiB
Go

// Package env provides functionality.
package env
import (
"log"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
func init() {
LoadEnvFiles()
}
// LoadEnvFiles provides functionality.
func LoadEnvFiles() {
env := getEnvironment()
envFile := ".env." + env
if tryLoadEnv(envFile, env) {
return
}
if tryLoadEnv(".env", "") {
return
}
if env != "test" {
log.Printf("Warning: No .env file found")
} else {
log.Printf("Note: No .env file loaded in test environment (this is normal)")
}
}
func tryLoadEnv(filename, envName string) bool {
if godotenv.Load(filename) == nil {
if envName != "" {
log.Printf(".env.%s file loaded successfully", envName)
} else {
log.Println(".env file loaded successfully")
}
return true
}
if cwd := os.Getenv("CWD"); cwd != "" {
path := filepath.Join(cwd, filename)
if godotenv.Load(path) == nil {
if envName != "" {
log.Printf(".env.%s file loaded from %s", envName, cwd)
} else {
log.Printf(".env file loaded from %s", cwd)
}
return true
}
}
dir, _ := os.Getwd()
for {
path := filepath.Join(dir, filename)
if godotenv.Load(path) == nil {
if envName != "" {
log.Printf(".env.%s file loaded from %s", envName, dir)
} else {
log.Printf(".env file loaded from %s", dir)
}
return true
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
break
}
dir = parent
}
return false
}
// getEnvironment determines the current environment from various environment variables
func getEnvironment() string {
// Check RSMON_ENV first (rsmon-specific)
if env := os.Getenv("RSMON_ENV"); env != "" {
return env
}
// Then check GO_ENV (standard)
if env := os.Getenv("GO_ENV"); env != "" {
return env
}
// Default to development
return "development"
}