-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
116 lines (100 loc) · 2.54 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package vss
import (
"log"
"os"
"path/filepath"
"github.com/pelletier/go-toml/v2"
)
type GoldMarkHighlightConfig struct {
Style *string
WithNumbers *bool `toml:"with_numbers"`
}
type GoldMarkRendererOptions struct {
WithUnsafe *bool `toml:"with_unsafe"`
}
// GoldMarkConfig is a struct for configuring goldmark.
// なぜ Pointer なのか?
// config file にキーを設定したかどうかを判定するため
type GoldMarkConfig struct {
HighlightConfig *GoldMarkHighlightConfig `toml:"highlight"`
RendererOptions *GoldMarkRendererOptions `toml:"renderer"`
}
type BuildConfig struct {
IgnoreFiles []string `toml:"ignore_files"`
Goldmark GoldMarkConfig `toml:"goldmark"`
}
type Config struct {
Build BuildConfig `toml:"build"`
SiteTitle string `toml:"site_title"`
SiteDescription string `toml:"site_description"`
BaseUrl string `toml:"base_url"`
// The following settings are not in config.toml
Dist string // dist directory
Static string // static directory
Layouts string // layouts directory
}
// LoadConfig loads a TOML text into a Config struct.
func LoadConfig() (*Config, error) {
path, err := cliConfigFile()
if err != nil {
return nil, err
}
c, err := loadConfigFile(path)
if err != nil {
return nil, err
}
// set default values
c.Dist = "dist"
c.Static = "static"
c.Layouts = "layouts"
return c, nil
}
func loadConfigFile(path string) (*Config, error) {
log.Printf("[INFO] Loading config file: %s", path)
d, err := os.ReadFile(path)
if err != nil {
return nil, err
}
config := &Config{}
err = toml.Unmarshal(d, config)
if err != nil {
return nil, err
}
return config, nil
}
// AsMap returns a map[string]interface{} representation of the Config struct.
func (c *Config) AsMap() map[string]interface{} {
return map[string]interface{}{
"site_title": c.SiteTitle,
"site_description": c.SiteDescription,
"base_url": c.BaseUrl,
}
}
func configFile() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Join(dir, "vss.toml"), nil
}
// cliConfigFileOverride returns the value of VSS_CONFIG_FILE if set.
func cliConfigFileOverride() string {
return os.Getenv("VSS_CONFIG_FILE")
}
func cliConfigFile() (string, error) {
configFilePath := cliConfigFileOverride()
if configFilePath == "" {
var err error
configFilePath, err = configFile()
if err != nil {
return "", err
}
}
f, err := os.Open(configFilePath)
if err == nil {
f.Close()
return configFilePath, nil
} else {
return "", err
}
}