2024-03-20 08:58:17 +01:00
|
|
|
package config
|
|
|
|
|
2024-04-02 09:02:59 +02:00
|
|
|
import (
|
|
|
|
"encoding"
|
|
|
|
"fmt"
|
2024-04-02 09:57:33 +02:00
|
|
|
"log/slog"
|
2024-04-02 09:02:59 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-03-20 08:58:17 +01:00
|
|
|
var Cfg Config //nolint:gochecknoglobals
|
|
|
|
|
|
|
|
type Config struct {
|
2024-04-02 09:02:59 +02:00
|
|
|
Email string `env:"EMAIL,required"`
|
|
|
|
Password string `env:"PASSWORD,expand"`
|
|
|
|
PasswordFile PasswordFile `env:"PASSWORD_FILE,expand"`
|
|
|
|
CacheDir string `env:"CACHE_DIR,expand" envDefault:"/var/cache/glucose_exporter"`
|
|
|
|
Debug bool `env:"DEBUG"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetPassword() (string, error) {
|
|
|
|
if c.PasswordFile != "" && c.Password != "" {
|
|
|
|
return "", ErrTooManyPasswords
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Password != "" {
|
2024-04-02 09:57:33 +02:00
|
|
|
slog.Debug("read password in config")
|
|
|
|
|
2024-04-02 09:02:59 +02:00
|
|
|
return c.Password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.PasswordFile != "" {
|
2024-04-02 09:57:33 +02:00
|
|
|
slog.Debug("read password file in config")
|
|
|
|
|
2024-04-02 09:02:59 +02:00
|
|
|
return string(c.PasswordFile), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", ErrMissingPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
type PasswordFile string
|
|
|
|
|
|
|
|
var _ encoding.TextUnmarshaler = (*PasswordFile)(nil)
|
|
|
|
|
|
|
|
func (pf *PasswordFile) UnmarshalText(text []byte) error {
|
|
|
|
b, err := os.ReadFile(string(text))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading password file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*pf = PasswordFile(string(b))
|
|
|
|
|
|
|
|
return nil
|
2024-03-20 08:58:17 +01:00
|
|
|
}
|