it turns out cleanenv already does all that
This commit is contained in:
parent
9ae7151c4d
commit
dc51b06b51
|
@ -8,8 +8,6 @@ Code for the [burning.moe](https://burning.moe) homepage
|
||||||
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
|
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
|
||||||
- [log](https://github.com/charmbracelet/log)
|
- [log](https://github.com/charmbracelet/log)
|
||||||
- [mage](https://github.com/magefile/mage)
|
- [mage](https://github.com/magefile/mage)
|
||||||
- [toml](https://github.com/BurntSushi/toml)
|
|
||||||
- [yaml](https://github.com/go-yaml/yaml)
|
|
||||||
|
|
||||||
## why?
|
## why?
|
||||||
|
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -3,15 +3,14 @@ module git.burning.moe/celediel/burning.moe
|
||||||
go 1.21.5
|
go 1.21.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.3.2
|
|
||||||
github.com/charmbracelet/log v0.3.1
|
github.com/charmbracelet/log v0.3.1
|
||||||
github.com/go-chi/chi/v5 v5.0.11
|
github.com/go-chi/chi/v5 v5.0.11
|
||||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||||
github.com/magefile/mage v1.15.0
|
github.com/magefile/mage v1.15.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
github.com/charmbracelet/lipgloss v0.9.1 // indirect
|
github.com/charmbracelet/lipgloss v0.9.1 // indirect
|
||||||
github.com/go-logfmt/logfmt v0.6.0 // indirect
|
github.com/go-logfmt/logfmt v0.6.0 // indirect
|
||||||
|
@ -24,5 +23,6 @@ require (
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
||||||
golang.org/x/sys v0.13.0 // indirect
|
golang.org/x/sys v0.13.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/ilyakaznacheev/cleanenv"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const dataDir string = "./templates/data/"
|
const dataDir string = "./templates/data/"
|
||||||
|
@ -56,7 +54,7 @@ func LoadTemplateData(page string) (TemplateData, error) {
|
||||||
|
|
||||||
for _, extension := range dataExtensions {
|
for _, extension := range dataExtensions {
|
||||||
if info, err := os.Stat(output + extension); err == nil && !info.IsDir() {
|
if info, err := os.Stat(output + extension); err == nil && !info.IsDir() {
|
||||||
data, err = loadFromFile(output, extension)
|
err = cleanenv.ReadConfig(output+extension, &data)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// don't try anymore files
|
// don't try anymore files
|
||||||
return data, nil
|
return data, nil
|
||||||
|
@ -67,31 +65,3 @@ func LoadTemplateData(page string) (TemplateData, error) {
|
||||||
// couldn't load anything from file
|
// couldn't load anything from file
|
||||||
return TemplateData{}, errors.New("Couldn't load data from file")
|
return TemplateData{}, errors.New("Couldn't load data from file")
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadFromFile loads TemplateData from the specified filetype (yaml, toml, or json)
|
|
||||||
func loadFromFile(filename, filetype string) (TemplateData, error) {
|
|
||||||
var data TemplateData
|
|
||||||
file, err := os.ReadFile(filename + filetype)
|
|
||||||
if err != nil {
|
|
||||||
return TemplateData{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch filetype {
|
|
||||||
case "json":
|
|
||||||
err = json.Unmarshal(file, &data)
|
|
||||||
if err != nil {
|
|
||||||
return TemplateData{}, err
|
|
||||||
}
|
|
||||||
case "toml":
|
|
||||||
err = toml.Unmarshal(file, &data)
|
|
||||||
if err != nil {
|
|
||||||
return TemplateData{}, err
|
|
||||||
}
|
|
||||||
case "yaml":
|
|
||||||
err = yaml.Unmarshal(file, &data)
|
|
||||||
if err != nil {
|
|
||||||
return TemplateData{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue