package lib import ( "os" "path/filepath" "git.iamthefij.com/iamthefij/slog" "github.com/spf13/viper" ) const ( ConfigDirPerms = 0o755 ConfigFilePerms = 0o600 ) var singleClient *Client func GetClient() (*Client, error) { if singleClient != nil { return singleClient, nil } hostname := viper.GetString("hostname") username := viper.GetString("username") password := viper.GetString("password") var err error singleClient, err = ConnectImap(hostname, username, password) if err != nil { return nil, err } return singleClient, nil } func AddConfigPaths(configFile string) { if configFile != "" { viper.SetConfigFile(configFile) } else { viper.SetConfigName("config") viper.SetConfigType("toml") userConfigDir, err := os.UserConfigDir() if err == nil { userConfigPath := filepath.Join(userConfigDir, "imap-notes") _ = os.MkdirAll(userConfigPath, ConfigDirPerms) viper.AddConfigPath(userConfigPath) } viper.AddConfigPath("$HOME/.imap-notes") } } func InitializeConfig(configFile string) { AddConfigPaths(configFile) if err := viper.ReadInConfig(); err != nil { slog.OnErrFatalf(viper.SafeWriteConfig(), "failed writing default config") slog.OnErrFatalf(viper.ReadInConfig(), "failed to read config") } }