refactoring yaml save/load into functions

This commit is contained in:
Niko Abeler 2022-10-13 20:48:01 +02:00
parent 9fe09af2e0
commit bdb08657e3
4 changed files with 37 additions and 69 deletions

23
files.go Normal file
View File

@ -0,0 +1,23 @@
package owl
import (
"os"
"gopkg.in/yaml.v2"
)
func saveToYaml(path string, data interface{}) error {
bytes, err := yaml.Marshal(data)
if err != nil {
return err
}
return os.WriteFile(path, bytes, 0644)
}
func loadFromYaml(path string, data interface{}) error {
bytes, err := os.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(bytes, data)
}

35
post.go
View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os"
"path" "path"
"sort" "sort"
"sync" "sync"
@ -235,16 +234,8 @@ func (post *Post) IncomingWebmentions() []WebmentionIn {
return []WebmentionIn{} return []WebmentionIn{}
} }
data, err := os.ReadFile(fileName)
if err != nil {
return []WebmentionIn{}
}
webmentions := []WebmentionIn{} webmentions := []WebmentionIn{}
err = yaml.Unmarshal(data, &webmentions) loadFromYaml(fileName, &webmentions)
if err != nil {
return []WebmentionIn{}
}
return webmentions return webmentions
} }
@ -256,16 +247,8 @@ func (post *Post) OutgoingWebmentions() []WebmentionOut {
return []WebmentionOut{} return []WebmentionOut{}
} }
data, err := os.ReadFile(fileName)
if err != nil {
return []WebmentionOut{}
}
webmentions := []WebmentionOut{} webmentions := []WebmentionOut{}
err = yaml.Unmarshal(data, &webmentions) loadFromYaml(fileName, &webmentions)
if err != nil {
return []WebmentionOut{}
}
return webmentions return webmentions
} }
@ -291,12 +274,7 @@ func (post *Post) PersistIncomingWebmention(webmention WebmentionIn) error {
wms = append(wms, webmention) wms = append(wms, webmention)
} }
data, err := yaml.Marshal(wms) err := saveToYaml(post.IncomingWebmentionsFile(), wms)
if err != nil {
return err
}
err = os.WriteFile(post.IncomingWebmentionsFile(), data, 0644)
if err != nil { if err != nil {
return err return err
} }
@ -325,12 +303,7 @@ func (post *Post) PersistOutgoingWebmention(webmention *WebmentionOut) error {
wms = append(wms, *webmention) wms = append(wms, *webmention)
} }
data, err := yaml.Marshal(wms) err := saveToYaml(post.OutgoingWebmentionsFile(), wms)
if err != nil {
return err
}
err = os.WriteFile(post.OutgoingWebmentionsFile(), data, 0644)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,8 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"gopkg.in/yaml.v2"
) )
//go:embed embed/initial/base.html //go:embed embed/initial/base.html
@ -42,8 +40,7 @@ func CreateRepository(name string, config RepoConfig) (Repository, error) {
if config.Domain == "" { if config.Domain == "" {
config.Domain = "http://localhost:8080" config.Domain = "http://localhost:8080"
} }
config_data, _ := yaml.Marshal(config) saveToYaml(path.Join(newRepo.Dir(), "config.yml"), config)
os.WriteFile(path.Join(newRepo.Dir(), "config.yml"), config_data, 0644)
// copy all files from static_files embed.FS to StaticDir // copy all files from static_files embed.FS to StaticDir
staticFiles, _ := embed_files.ReadDir("embed/initial/static") staticFiles, _ := embed_files.ReadDir("embed/initial/static")
@ -145,12 +142,11 @@ func (repo *Repository) CreateUser(name string) (User, error) {
os.WriteFile(path.Join(user_dir, "meta", "VERSION"), []byte(VERSION), 0644) os.WriteFile(path.Join(user_dir, "meta", "VERSION"), []byte(VERSION), 0644)
os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte(base_template), 0644) os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte(base_template), 0644)
meta, _ := yaml.Marshal(UserConfig{ saveToYaml(new_user.ConfigFile(), UserConfig{
Title: name, Title: name,
SubTitle: "", SubTitle: "",
HeaderColor: "#bdd6be", HeaderColor: "#bdd6be",
}) })
os.WriteFile(new_user.ConfigFile(), meta, 0644)
return new_user, nil return new_user, nil
} }
@ -182,15 +178,8 @@ func (repo Repository) PostAliases() (map[string]*Post, error) {
} }
func (repo Repository) Config() (RepoConfig, error) { func (repo Repository) Config() (RepoConfig, error) {
config_path := path.Join(repo.Dir(), "config.yml") meta := RepoConfig{}
config_data, err := ioutil.ReadFile(config_path) err := loadFromYaml(path.Join(repo.Dir(), "config.yml"), &meta)
if err != nil { return meta, err
return RepoConfig{}, err
}
var meta RepoConfig
err = yaml.Unmarshal(config_data, &meta)
if err != nil {
return RepoConfig{}, err
}
return meta, nil
} }

25
user.go
View File

@ -196,30 +196,13 @@ func (user User) Template() (string, error) {
} }
func (user User) Config() (UserConfig, error) { func (user User) Config() (UserConfig, error) {
config_path := user.ConfigFile() meta := UserConfig{}
config_data, err := ioutil.ReadFile(config_path) err := loadFromYaml(user.ConfigFile(), &meta)
if err != nil { return meta, err
return UserConfig{}, err
}
var meta UserConfig
err = yaml.Unmarshal(config_data, &meta)
if err != nil {
return UserConfig{}, err
}
return meta, nil
} }
func (user User) SetConfig(new_config UserConfig) error { func (user User) SetConfig(new_config UserConfig) error {
config_path := user.ConfigFile() return saveToYaml(user.ConfigFile(), new_config)
config_data, err := yaml.Marshal(new_config)
if err != nil {
return err
}
err = ioutil.WriteFile(config_path, config_data, 0644)
if err != nil {
return err
}
return nil
} }
func (user User) PostAliases() (map[string]*Post, error) { func (user User) PostAliases() (map[string]*Post, error) {