- Added detailed comments to clarify the purpose of various functions and types in the Discord bot and HA gateway. - Introduced new methods in the CommandApp for handling light and switch operations, including HandleLightOn, HandleLightOff, HandleLightToggle, and their respective autocomplete functions. - Updated the HAClient interface to include methods for fetching states and calling services, enhancing the interaction with Home Assistant. - Improved the structure of entity and light domain models to include additional attributes and clearer documentation. - Implemented logging enhancements in both the Discord bot and HA gateway to ensure better traceability and context in logs. - Refactored the configuration loading process to streamline environment variable handling and defaults. - Stubbed out switch control methods in the gRPC adapter, indicating future implementation plans. - Enhanced telemetry setup to ensure proper initialization and shutdown procedures for observability.
47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
)
|
|
|
|
// Config holds runtime configuration for the Discord bot process.
|
|
type Config struct {
|
|
DiscordToken string
|
|
GuildID string
|
|
HAGatewayAddr string
|
|
OTELEndpoint string
|
|
LogLevel string
|
|
LogFormat string
|
|
}
|
|
|
|
// Load reads Discord bot configuration from environment variables.
|
|
func Load() (*Config, error) {
|
|
token := os.Getenv("DISCORD_TOKEN")
|
|
if token == "" {
|
|
return nil, errors.New("DISCORD_TOKEN is required but not set")
|
|
}
|
|
|
|
addr := os.Getenv("HA_GATEWAY_ADDR")
|
|
if addr == "" {
|
|
return nil, errors.New("HA_GATEWAY_ADDR is required but not set")
|
|
}
|
|
|
|
return &Config{
|
|
DiscordToken: token,
|
|
GuildID: os.Getenv("GUILD_ID"),
|
|
HAGatewayAddr: addr,
|
|
OTELEndpoint: os.Getenv("OTEL_ENDPOINT"),
|
|
LogLevel: getenvDefault("LOG_LEVEL", "info"),
|
|
LogFormat: getenvDefault("LOG_FORMAT", "json"),
|
|
}, nil
|
|
}
|
|
|
|
// getenvDefault keeps config loading concise for optional variables with defaults.
|
|
func getenvDefault(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|