This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
77 lines (68 loc) · 1.75 KB
/
configuration.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
package ortfomk
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v3"
)
type OutputTemplates struct {
Media string
Translated string
Rest string
}
type Configuration struct {
Development struct {
OutputTo OutputTemplates `yaml:"output to"`
}
Production struct {
UploadTo OutputTemplates `yaml:"upload to"`
AvailableAt OutputTemplates `yaml:"available at"`
}
AdditionalData []string `yaml:"additional data"`
}
func LoadConfiguration(path string) (Configuration, error) {
if path == "" {
path = "ortfomk.yaml"
if _, err := os.Stat(path); os.IsNotExist(err) {
LogWarning("No ortfomk.yaml found, using default configuration. A ortfomk.yaml file will be generated.")
defaultConfig, err := yaml.Marshal(DefaultConfiguration())
if err != nil {
panic(err)
}
ioutil.WriteFile("ortfomk.yaml", []byte(defaultConfig), 0o644)
return DefaultConfiguration(), nil
}
}
config := Configuration{}
raw, err := os.ReadFile(path)
if err != nil {
return Configuration{}, fmt.Errorf("while reading configuration file: %w", err)
}
err = yaml.Unmarshal(raw, &config)
if err != nil {
return Configuration{}, fmt.Errorf("while parsing configuration file: %w", err)
}
LogDebug("Loaded configuration: %#v", config)
return config, nil
}
func DefaultConfiguration() Configuration {
return Configuration{
Development: struct {
OutputTo OutputTemplates "yaml:\"output to\""
}{
OutputTo: OutputTemplates{
Media: "media/",
Translated: "<language>/",
Rest: "/",
},
},
Production: struct {
UploadTo OutputTemplates "yaml:\"upload to\""
AvailableAt OutputTemplates "yaml:\"available at\""
}{
UploadTo: OutputTemplates{},
AvailableAt: OutputTemplates{},
},
AdditionalData: []string{},
}
}