config/config.go (view raw)
1package config
2
3import (
4 "fmt"
5 "path/filepath"
6
7 "github.com/spf13/viper"
8)
9
10type Config struct {
11 Repo struct {
12 ScanPath string `mapstructure:"scanPath"`
13 Readme []string `mapstructure:"readme"`
14 MainBranch []string `mapstructure:"mainBranch"`
15 Ignore []string `mapstructure:"ignore,omitempty"`
16 Unlisted []string `mapstructure:"unlisted,omitempty"`
17 } `mapstructure:"repo"`
18 Dirs struct {
19 Templates string `mapstructure:"templates"`
20 Static string `mapstructure:"static"`
21 } `mapstructure:"dirs"`
22 Meta struct {
23 Title string `mapstructure:"title"`
24 Description string `mapstructure:"description"`
25 SyntaxHighlight string `mapstructure:"syntaxHighlight"`
26 } `mapstructure:"meta"`
27 Server struct {
28 Name string `mapstructure:"name,omitempty"`
29 Host string `mapstructure:"host"`
30 Port int `mapstructure:"port"`
31 } `mapstructure:"server"`
32}
33
34func Read(f string) (*Config, error) {
35 v := viper.New()
36 v.SetConfigFile(f)
37 v.SetEnvPrefix("LEGIT")
38 v.AutomaticEnv()
39
40 if err := v.ReadInConfig(); err != nil {
41 return nil, fmt.Errorf("reading config: %w", err)
42 }
43
44 c := &Config{}
45 if err := v.Unmarshal(c); err != nil {
46 return nil, fmt.Errorf("parsing config: %w", err)
47 }
48
49 var err error
50 if c.Repo.ScanPath, err = filepath.Abs(c.Repo.ScanPath); err != nil {
51 return nil, err
52 }
53 if c.Dirs.Templates, err = filepath.Abs(c.Dirs.Templates); err != nil {
54 return nil, err
55 }
56 if c.Dirs.Static, err = filepath.Abs(c.Dirs.Static); err != nil {
57 return nil, err
58 }
59
60 return c, nil
61}