mirror of
https://github.com/kemko/memes-telegram-integration.git
synced 2026-01-01 15:55:41 +03:00
38 lines
762 B
Go
38 lines
762 B
Go
package memogram
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/caarlos0/env"
|
|
"github.com/joho/godotenv"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Config struct {
|
|
ServerAddr string `env:"SERVER_ADDR,required"`
|
|
BotToken string `env:"BOT_TOKEN,required"`
|
|
Data string `env:"DATA"`
|
|
}
|
|
|
|
func getConfigFromEnv() (*Config, error) {
|
|
envFileName := ".env"
|
|
if _, err := os.Stat(envFileName); err == nil {
|
|
err := godotenv.Load(envFileName)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
}
|
|
|
|
config := Config{}
|
|
if err := env.Parse(&config); err != nil {
|
|
return nil, errors.Wrap(err, "invalid configuration")
|
|
}
|
|
if config.Data == "" {
|
|
// Default to `data.txt` if not specified.
|
|
config.Data = "data.txt"
|
|
}
|
|
config.Data = path.Join(".", config.Data)
|
|
return &config, nil
|
|
}
|