package config import ( "fmt" "path/filepath" "github.com/spf13/viper" ) type Config struct { Repo struct { ScanPath string `mapstructure:"scanPath"` Readme []string `mapstructure:"readme"` MainBranch []string `mapstructure:"mainBranch"` Ignore []string `mapstructure:"ignore,omitempty"` Unlisted []string `mapstructure:"unlisted,omitempty"` } `mapstructure:"repo"` Dirs struct { Templates string `mapstructure:"templates"` Static string `mapstructure:"static"` } `mapstructure:"dirs"` Meta struct { Title string `mapstructure:"title"` Description string `mapstructure:"description"` SyntaxHighlight string `mapstructure:"syntaxHighlight"` } `mapstructure:"meta"` Server struct { Name string `mapstructure:"name,omitempty"` Host string `mapstructure:"host"` Port int `mapstructure:"port"` } `mapstructure:"server"` } func Read(f string) (*Config, error) { v := viper.New() v.SetConfigFile(f) v.SetEnvPrefix("LEGIT") v.AutomaticEnv() if err := v.ReadInConfig(); err != nil { return nil, fmt.Errorf("reading config: %w", err) } c := &Config{} if err := v.Unmarshal(c); err != nil { return nil, fmt.Errorf("parsing config: %w", err) } var err error if c.Repo.ScanPath, err = filepath.Abs(c.Repo.ScanPath); err != nil { return nil, err } if c.Dirs.Templates, err = filepath.Abs(c.Dirs.Templates); err != nil { return nil, err } if c.Dirs.Static, err = filepath.Abs(c.Dirs.Static); err != nil { return nil, err } return c, nil }