From dc51b06b519c0e87063815ae6346ac455ed2f02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Tue, 23 Jan 2024 18:40:22 -0800 Subject: [PATCH] it turns out cleanenv already does all that --- README.md | 2 -- go.mod | 4 ++-- internal/models/templatedata.go | 34 ++------------------------------- 3 files changed, 4 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index e462483..fe60969 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/go.mod b/go.mod index cada77a..c50d693 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/internal/models/templatedata.go b/internal/models/templatedata.go index 3e40784..47d0523 100644 --- a/internal/models/templatedata.go +++ b/internal/models/templatedata.go @@ -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 -}