it turns out cleanenv already does all that

This commit is contained in:
Lilian Jónsdóttir 2024-01-23 18:40:22 -08:00
parent 9ae7151c4d
commit dc51b06b51
3 changed files with 4 additions and 36 deletions

View file

@ -8,8 +8,6 @@ Code for the [burning.moe](https://burning.moe) homepage
- [cleanenv](https://github.com/ilyakaznacheev/cleanenv)
- [log](https://github.com/charmbracelet/log)
- [mage](https://github.com/magefile/mage)
- [toml](https://github.com/BurntSushi/toml)
- [yaml](https://github.com/go-yaml/yaml)
## why?

4
go.mod
View file

@ -3,15 +3,14 @@ module git.burning.moe/celediel/burning.moe
go 1.21.5
require (
github.com/BurntSushi/toml v1.3.2
github.com/charmbracelet/log v0.3.1
github.com/go-chi/chi/v5 v5.0.11
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/magefile/mage v1.15.0
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
@ -24,5 +23,6 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // 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
)

View file

@ -1,15 +1,13 @@
package models
import (
"encoding/json"
"errors"
"html/template"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v3"
"github.com/ilyakaznacheev/cleanenv"
)
const dataDir string = "./templates/data/"
@ -56,7 +54,7 @@ func LoadTemplateData(page string) (TemplateData, error) {
for _, extension := range dataExtensions {
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 {
// don't try anymore files
return data, nil
@ -67,31 +65,3 @@ func LoadTemplateData(page string) (TemplateData, error) {
// couldn't load anything 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
}