all repos — legit @ 962e84f92cc6d50e624b666bf2aefa21d8656e6f

Unnamed repository; edit this file 'description' to name the repository.

internal/config/config.go (view raw)

 1package config
 2
 3import (
 4	"fmt"
 5	"path/filepath"
 6	"strings"
 7
 8	"github.com/spf13/viper"
 9)
10
11type Config struct {
12	Repo struct {
13		ScanPath   string   `mapstructure:"scanPath"`
14		Readme     []string `mapstructure:"readme"`
15		MainBranch []string `mapstructure:"mainBranch"`
16		Ignore     []string `mapstructure:"ignore,omitempty"`
17		Unlisted   []string `mapstructure:"unlisted,omitempty"`
18	} `mapstructure:"repo"`
19	Dirs struct {
20		Templates string `mapstructure:"templates"`
21		Static    string `mapstructure:"static"`
22	} `mapstructure:"dirs"`
23	Meta struct {
24		Title           string `mapstructure:"title"`
25		Description     string `mapstructure:"description"`
26		SyntaxHighlight string `mapstructure:"syntaxHighlight"`
27		Favicon         string `mapstructure:"favicon"`
28
29		FaviconHref string
30		FaviconType string
31	} `mapstructure:"meta"`
32	Server struct {
33		Name string `mapstructure:"name,omitempty"`
34		Host string `mapstructure:"host"`
35		Port int    `mapstructure:"port"`
36	} `mapstructure:"server"`
37}
38
39func Read(f string) (*Config, error) {
40	v := viper.New()
41	v.SetConfigFile(f)
42	v.SetEnvPrefix("LEGIT")
43	v.AutomaticEnv()
44
45	if err := v.ReadInConfig(); err != nil {
46		return nil, fmt.Errorf("reading config: %w", err)
47	}
48
49	c := &Config{}
50	if err := v.Unmarshal(c); err != nil {
51		return nil, fmt.Errorf("parsing config: %w", err)
52	}
53
54	var err error
55	if c.Repo.ScanPath, err = filepath.Abs(c.Repo.ScanPath); err != nil {
56		return nil, err
57	}
58	if c.Dirs.Templates, err = filepath.Abs(c.Dirs.Templates); err != nil {
59		return nil, err
60	}
61	if c.Dirs.Static, err = filepath.Abs(c.Dirs.Static); err != nil {
62		return nil, err
63	}
64
65	// Default favicon and compute MIME type
66	if c.Meta.Favicon == "" {
67		c.Meta.Favicon = "favicon.svg"
68	}
69	c.Meta.FaviconHref = "/static/" + c.Meta.Favicon
70	switch strings.ToLower(filepath.Ext(c.Meta.Favicon)) {
71	case ".svg":
72		c.Meta.FaviconType = "image/svg+xml"
73	case ".png":
74		c.Meta.FaviconType = "image/png"
75	case ".jpg", ".jpeg":
76		c.Meta.FaviconType = "image/jpeg"
77	default:
78		c.Meta.FaviconType = "image/x-icon"
79	}
80
81	return c, nil
82}