2024-11-02 18:11:56 +00:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Load() map[string]string {
|
|
|
|
var env map[string]string = make(map[string]string)
|
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
validEnv := []string{"MODE", "OPENWEATHERMAP_API_KEY", "LATITUDE", "LONGITUDE", "PORT"}
|
|
|
|
|
2024-11-02 18:11:56 +00:00
|
|
|
envpath := "./.env"
|
|
|
|
|
|
|
|
if _, err := os.Stat(envpath); err == nil {
|
|
|
|
|
|
|
|
dotenv, err := godotenv.Read(envpath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error loading .env file: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
env = dotenv
|
|
|
|
} else {
|
|
|
|
fmt.Println("No .env file found", err)
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
for _, key := range validEnv {
|
|
|
|
tempenv := os.Getenv(key)
|
|
|
|
if tempenv != "" {
|
|
|
|
env[key] = tempenv
|
|
|
|
}
|
2024-11-02 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
if len(env) == 0 {
|
|
|
|
fmt.Println("no environment variables are set")
|
|
|
|
os.Exit(1)
|
2024-11-02 18:11:56 +00:00
|
|
|
}
|
2024-11-07 09:17:33 +00:00
|
|
|
|
|
|
|
if val, ok := env["MODE"]; ok {
|
|
|
|
if val == "" {
|
|
|
|
env["MODE"] = "calc"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
env["MODE"] = "calc"
|
2024-11-02 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
env["MODE"] = strings.ToLower(env["MODE"])
|
|
|
|
switch env["MODE"] {
|
|
|
|
case "both":
|
|
|
|
checkEnvWeather(env)
|
|
|
|
checkEnvCalc(env)
|
|
|
|
case "weather":
|
|
|
|
checkEnvWeather(env)
|
|
|
|
default:
|
|
|
|
env["MODE"] = "calc"
|
2024-11-02 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkEnvWeather(env map[string]string) {
|
|
|
|
// Is there an API key for openweathermap?
|
|
|
|
if val, ok := env["OPENWEATHERMAP_API_KEY"]; ok {
|
|
|
|
if val == "" {
|
|
|
|
print("OPENWEATHERMAP_API_KEY is not set")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("OPENWEATHERMAP_API_KEY is not set")
|
|
|
|
os.Exit(1)
|
2024-11-02 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
// Is there a LATITUDE and LONGITUDE?
|
|
|
|
if val, ok := env["LATITUDE"]; ok {
|
|
|
|
if val == "" {
|
|
|
|
print("LATITUDE is not set")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("LATITUDE is not set")
|
2024-11-02 18:11:56 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-11-07 09:17:33 +00:00
|
|
|
if val, ok := env["LONGITUDE"]; ok {
|
|
|
|
if val == "" {
|
|
|
|
print("LONGITUDE is not set")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("LONGITUDE is not set")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2024-11-02 18:11:56 +00:00
|
|
|
|
2024-11-07 09:17:33 +00:00
|
|
|
func checkEnvCalc(env map[string]string) {
|
|
|
|
// Check for calc variables
|
2024-11-02 18:11:56 +00:00
|
|
|
}
|