70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
LogLevel string `yaml:"log_level"`
|
|
Accounts []AccountConfig `yaml:"accounts"`
|
|
}
|
|
|
|
type AccountConfig struct {
|
|
Name string `yaml:"name"`
|
|
MaildirPath string `yaml:"maildir_path"`
|
|
MbsyncAccount string `yaml:"mbsync_account"`
|
|
PasswordCommand string `yaml:"password_command"`
|
|
TokenFile string `yaml:"token_file"`
|
|
TokenEnv string `yaml:"token_env"`
|
|
}
|
|
|
|
// mbsyncChannel returns the mbsync channel/group name to pass to mbsync,
|
|
// falling back to the account name if mbsync_account is not set.
|
|
func (a AccountConfig) mbsyncChannel() string {
|
|
if a.MbsyncAccount != "" {
|
|
return a.MbsyncAccount
|
|
}
|
|
return a.Name
|
|
}
|
|
|
|
func (a AccountConfig) validate() error {
|
|
if a.Name == "" {
|
|
return fmt.Errorf("account name is required")
|
|
}
|
|
if a.MaildirPath == "" {
|
|
return fmt.Errorf("account %q: maildir_path is required", a.Name)
|
|
}
|
|
n := 0
|
|
if a.PasswordCommand != "" {
|
|
n++
|
|
}
|
|
if a.TokenFile != "" {
|
|
n++
|
|
}
|
|
if a.TokenEnv != "" {
|
|
n++
|
|
}
|
|
if n == 0 {
|
|
return fmt.Errorf("account %q: one of password_command, token_file, or token_env is required", a.Name)
|
|
}
|
|
if n > 1 {
|
|
return fmt.Errorf("account %q: only one of password_command, token_file, or token_env may be set", a.Name)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Config) unmarshal(data []byte) error {
|
|
return yaml.Unmarshal(data, c)
|
|
}
|
|
|
|
func defaultConfigPath() string {
|
|
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
|
|
return filepath.Join(xdg, "mbrts", "config.yaml")
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".config", "mbrts", "config.yaml")
|
|
}
|