2024-02-16 16:39:19 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"maps"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-03-03 00:48:45 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
2024-02-16 16:39:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// The properties that are depended upon in the source code are declared explicitly in the config struct.
|
|
|
|
// The constructors will set default values for most.
|
|
|
|
// Depending on the command, different defaults will be used (serve is assumed to be a "dev" environment
|
|
|
|
// while build is assumed to be prod)
|
|
|
|
// Some defaults could be overridden by cli flags (eg disable live reload on serve).
|
|
|
|
// The user can override some of those via config yaml.
|
|
|
|
// The non declared values found in config yaml will just be passed as site.config values
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
RootDir string
|
|
|
|
SrcDir string
|
|
|
|
TargetDir string
|
|
|
|
LayoutsDir string
|
|
|
|
IncludesDir string
|
|
|
|
DataDir string
|
|
|
|
|
2024-02-27 12:45:29 +01:00
|
|
|
SiteUrl string
|
|
|
|
PostFormat string
|
|
|
|
Lang string
|
|
|
|
HighlightTheme string
|
2024-02-16 16:39:19 +01:00
|
|
|
|
2024-02-27 16:24:45 +01:00
|
|
|
Minify bool
|
|
|
|
LiveReload bool
|
|
|
|
LinkStatic bool
|
|
|
|
IncludeDrafts bool
|
2024-02-16 16:39:19 +01:00
|
|
|
|
|
|
|
ServerHost string
|
|
|
|
ServerPort int
|
|
|
|
|
|
|
|
pageDefaults map[string]interface{}
|
|
|
|
|
|
|
|
// the user provided overrides, as found in config.yml
|
|
|
|
// these will passed as found as template context
|
|
|
|
overrides map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load(rootDir string) (*Config, error) {
|
|
|
|
// TODO allow to disable minify
|
|
|
|
|
|
|
|
config := &Config{
|
2024-02-27 12:45:29 +01:00
|
|
|
RootDir: rootDir,
|
|
|
|
SrcDir: filepath.Join(rootDir, "src"),
|
|
|
|
TargetDir: filepath.Join(rootDir, "target"),
|
|
|
|
LayoutsDir: filepath.Join(rootDir, "layouts"),
|
|
|
|
IncludesDir: filepath.Join(rootDir, "includes"),
|
|
|
|
DataDir: filepath.Join(rootDir, "data"),
|
|
|
|
PostFormat: "blog/:title.org",
|
|
|
|
Lang: "en",
|
|
|
|
HighlightTheme: "github",
|
|
|
|
Minify: true,
|
|
|
|
LiveReload: false,
|
|
|
|
LinkStatic: false,
|
2024-02-27 16:24:45 +01:00
|
|
|
IncludeDrafts: false,
|
2024-02-27 12:45:29 +01:00
|
|
|
pageDefaults: map[string]interface{}{},
|
2024-02-16 16:39:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// load overrides from config.yml
|
|
|
|
configPath := filepath.Join(rootDir, "config.yml")
|
|
|
|
yamlContent, err := os.ReadFile(configPath)
|
|
|
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
// config file is not mandatory
|
|
|
|
return config, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(yamlContent, &config.overrides)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set user-provided overrides of declared config keys
|
2024-02-27 12:45:29 +01:00
|
|
|
// TODO less copypasty way of declaring config overrides
|
2024-02-16 16:39:19 +01:00
|
|
|
if url, found := config.overrides["url"]; found {
|
|
|
|
config.SiteUrl = url.(string)
|
|
|
|
}
|
2024-02-21 16:56:22 +01:00
|
|
|
if format, found := config.overrides["post_format"]; found {
|
|
|
|
config.PostFormat = format.(string)
|
|
|
|
}
|
|
|
|
if format, found := config.overrides["lang"]; found {
|
|
|
|
config.Lang = format.(string)
|
2024-02-16 16:39:19 +01:00
|
|
|
}
|
2024-02-27 12:45:29 +01:00
|
|
|
if format, found := config.overrides["highlight_theme"]; found {
|
|
|
|
config.HighlightTheme = format.(string)
|
|
|
|
}
|
2024-02-16 16:39:19 +01:00
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2024-02-24 16:39:45 +01:00
|
|
|
func LoadDev(rootDir string, host string, port int, reload bool) (*Config, error) {
|
2024-02-16 16:39:19 +01:00
|
|
|
// TODO revisit is this Load vs LoadDevServer is the best way to handle both modes
|
|
|
|
// TODO some of the options need to be overridable: host, port, live reload at least
|
|
|
|
|
|
|
|
config, err := Load(rootDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup serve command specific overrides (these could be eventually tweaked with flags)
|
2024-02-24 16:39:45 +01:00
|
|
|
config.ServerHost = host
|
|
|
|
config.ServerPort = port
|
|
|
|
config.LiveReload = reload
|
2024-02-16 16:39:19 +01:00
|
|
|
config.Minify = false
|
|
|
|
config.LinkStatic = true
|
2024-02-27 16:24:45 +01:00
|
|
|
config.IncludeDrafts = true
|
2024-02-16 16:39:19 +01:00
|
|
|
config.SiteUrl = fmt.Sprintf("http://%s:%d", config.ServerHost, config.ServerPort)
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config Config) AsContext() map[string]interface{} {
|
|
|
|
context := map[string]interface{}{
|
|
|
|
"url": config.SiteUrl,
|
|
|
|
}
|
|
|
|
maps.Copy(context, config.overrides)
|
|
|
|
return context
|
|
|
|
}
|