package config import ( "fmt" "github.com/spf13/viper" ) // Config stores all configuration for the application. // The values are read by viper from a config file or environment variables. type Config struct { Title string `mapstructure:"title"` DefaultFile string `mapstructure:"default_file"` ShowTOC bool `mapstructure:"show_toc"` } // LoadConfig reads configuration from file or environment variables. func LoadConfig() (config Config, err error) { viper.AddConfigPath(".") viper.SetConfigName("config") viper.SetConfigType("toml") // Set default values viper.SetDefault("title", "GemReader") viper.SetDefault("default_file", "") viper.SetDefault("show_toc", false) viper.AutomaticEnv() if err = viper.ReadInConfig(); err != nil { // Don't return error if config file doesn't exist, just use defaults if _, ok := err.(viper.ConfigFileNotFoundError); ok { // Config file not found, proceed with defaults err = nil } else { return config, fmt.Errorf("error reading config file: %w", err) } } if err = viper.Unmarshal(&config); err != nil { return config, fmt.Errorf("error parsing config file: %w", err) } return config, nil }