Upload files to "internal/config"
This commit is contained in:
parent
d0c3372e82
commit
664b680ac4
1 changed files with 44 additions and 0 deletions
44
internal/config/config.go
Normal file
44
internal/config/config.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue