glucose_exporter/internal/config/config.go
Marvin Preuss 77ad55f1eb feat(config): own PasswordFile type
its possible to unmarshal a PasswordFile path to a read string. this is
nice for docker secret setups. also adds a GetPassword method which
checks if both Password and PasswordFile are declared.
2024-04-02 07:36:59 +00:00

49 lines
999 B
Go

package config
import (
"encoding"
"fmt"
"os"
)
var Cfg Config //nolint:gochecknoglobals
type Config struct {
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 != "" {
return c.Password, nil
}
if c.PasswordFile != "" {
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
}