This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/utils/config.go
2023-05-22 13:08:08 +02:00

49 lines
1.2 KiB
Go

package utils
import (
"fmt"
"github.com/caarlos0/env"
)
type Config struct {
E621APIKey string `env:"e621_API_KEY"`
E621Username string `env:"e621_USERNAME"`
Neo4jURL string `env:"NEO4J_URL"`
Neo4jUsername string `env:"NEO4J_USERNAME"`
Neo4jPassword string `env:"NEO4J_PASSWORD"`
}
// LoadConfig loads the configuration from environment variables
func LoadConfig() (*Config, error) {
config := &Config{}
if err := env.Parse(config); err != nil {
return nil, fmt.Errorf("error parsing configuration: %w", err)
}
if err := ValidateConfig(config); err != nil {
return nil, fmt.Errorf("configuration validation failed: %w", err)
}
return config, nil
}
// ValidateConfig checks if all required fields in the configuration are set
func ValidateConfig(config *Config) error {
if config.E621APIKey == "" {
return fmt.Errorf("e621_API_KEY is not set")
}
if config.E621Username == "" {
return fmt.Errorf("e621_USERNAME is not set")
}
if config.Neo4jURL == "" {
return fmt.Errorf("NEO4J_URL is not set")
}
if config.Neo4jUsername == "" {
return fmt.Errorf("NEO4J_USERNAME is not set")
}
if config.Neo4jPassword == "" {
return fmt.Errorf("NEO4J_PASSWORD is not set")
}
return nil
}