config/config.go (view raw)
1package config
2
3import (
4 "fmt"
5 "os"
6
7 "gopkg.in/yaml.v3"
8)
9
10var Config ConfigYaml
11
12func init() {
13 err := Config.parseConfig()
14 if err != nil {
15 fmt.Fprintf(os.Stderr, "error: config: %+v\n", err)
16 os.Exit(1)
17 }
18}
19
20type ConfigYaml struct {
21 Title string `yaml:"title"`
22 Desc string `yaml:"description"`
23 DefaultTemplate string `yaml:"-"`
24 Author struct {
25 Name string `yaml:"name"`
26 Email string `yaml:"email"`
27 } `yaml:"author"`
28 URL string `yaml:"url"`
29 PreBuild []string `yaml:"preBuild"`
30 PostBuild []string `yaml:"postBuild"`
31}
32
33// For backward compat with `default-template`
34func (c *ConfigYaml) UnmarshalYAML(value *yaml.Node) error {
35 type Alias ConfigYaml // Create an alias to avoid recursion
36
37 var aux Alias
38
39 if err := value.Decode(&aux); err != nil {
40 return err
41 }
42
43 // Handle the DefaultTemplate field
44 var temp struct {
45 DefaultTemplate1 string `yaml:"default-template"`
46 DefaultTemplate2 string `yaml:"defaultTemplate"`
47 }
48 if err := value.Decode(&temp); err != nil {
49 return err
50 }
51
52 if temp.DefaultTemplate1 != "" {
53 aux.DefaultTemplate = temp.DefaultTemplate1
54 } else {
55 aux.DefaultTemplate = temp.DefaultTemplate2
56 }
57
58 *c = ConfigYaml(aux) // Assign the unmarshalled values back to the original struct
59
60 return nil
61}
62
63func (c *ConfigYaml) parseConfig() error {
64 cf, err := os.ReadFile("config.yaml")
65 if err != nil {
66 return err
67 }
68 if err = yaml.Unmarshal(cf, c); err != nil {
69 return err
70 }
71 return nil
72}