refactoring yaml save/load into functions

master
Niko Abeler 2 months ago
parent 9fe09af2e0
commit bdb08657e3
  1. 23
      files.go
  2. 35
      post.go
  3. 23
      repository.go
  4. 25
      user.go

@ -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)
}

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

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

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

Loading…
Cancel
Save