all repos — vite @ 1ba292b8a8589cd78155ddf1cdbde2f2edc87596

a fast (this time, actually) and minimal static site generator

config/config.go (view raw)

 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
package config

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v3"
)

var Config ConfigYaml

func init() {
	err := Config.parseConfig()
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: config: %+v\n", err)
		os.Exit(1)
	}
}

type ConfigYaml struct {
	Title           string `yaml:"title"`
	Desc            string `yaml:"description"`
	DefaultTemplate string `yaml:"-"`
	Author          struct {
		Name  string `yaml:"name"`
		Email string `yaml:"email"`
	} `yaml:"author"`
	URL       string   `yaml:"url"`
	PreBuild  []string `yaml:"preBuild"`
	PostBuild []string `yaml:"postBuild"`
}

// For backward compat with `default-template`
func (c *ConfigYaml) UnmarshalYAML(value *yaml.Node) error {
	type Alias ConfigYaml // Create an alias to avoid recursion

	var aux Alias

	if err := value.Decode(&aux); err != nil {
		return err
	}

	// Handle the DefaultTemplate field
	var temp struct {
		DefaultTemplate1 string `yaml:"default-template"`
		DefaultTemplate2 string `yaml:"defaultTemplate"`
	}
	if err := value.Decode(&temp); err != nil {
		return err
	}

	if temp.DefaultTemplate1 != "" {
		aux.DefaultTemplate = temp.DefaultTemplate1
	} else {
		aux.DefaultTemplate = temp.DefaultTemplate2
	}

	*c = ConfigYaml(aux) // Assign the unmarshalled values back to the original struct

	return nil
}

func (c *ConfigYaml) parseConfig() error {
	cf, err := os.ReadFile("config.yaml")
	if err != nil {
		return err
	}
	if err = yaml.Unmarshal(cf, c); err != nil {
		return err
	}
	return nil
}