humiditycalc/configs/config.go

100 lines
1.8 KiB
Go
Raw Normal View History

2024-11-02 18:11:56 +00:00
package configs
import (
"fmt"
"os"
2024-11-07 09:30:40 +00:00
"strings"
2024-11-02 18:11:56 +00:00
"github.com/joho/godotenv"
)
func Load() map[string]string {
var env map[string]string = make(map[string]string)
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)
}
for _, key := range validEnv {
tempenv := os.Getenv(key)
if tempenv != "" {
env[key] = tempenv
}
2024-11-02 18:11:56 +00:00
}
if len(env) == 0 {
fmt.Println("no environment variables are set")
os.Exit(1)
2024-11-02 18:11:56 +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
}
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
}
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
}
// 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)
}
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
func checkEnvCalc(env map[string]string) {
// Check for calc variables
2024-11-02 18:11:56 +00:00
}