fix: single ReadFile fn

This commit is contained in:
NhanPT 2023-02-19 14:15:02 +07:00
parent e23b7d6b65
commit 11aca2bbae
4 changed files with 12 additions and 27 deletions

View file

@ -2,8 +2,6 @@ package modules
import (
"fmt"
"io"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
@ -46,13 +44,7 @@ func LoadConfig() {
}
AppConfig = defaultConfig
file, err := os.Open(filepath.Join("data", "config.yaml"))
if err != nil {
fmt.Printf("failed reading file: %s", err)
return
}
defer file.Close()
yamlFile, err := io.ReadAll(file)
yamlFile, err := ReadFile(filepath.Join("data", "config.yaml"))
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return

View file

@ -3,8 +3,6 @@ package modules
import (
"fmt"
"index/suffixarray"
"io"
"os"
"path/filepath"
"regexp"
"strings"
@ -39,13 +37,7 @@ type BookmarkData struct {
func LoadContent() ContentData {
emptyData := ContentData{}
file, err := os.Open(filepath.Join("data", "data.yaml"))
if err != nil {
fmt.Printf("failed reading file: %s", err)
return emptyData
}
defer file.Close()
yamlFile, err := io.ReadAll(file)
yamlFile, err := ReadFile(filepath.Join("data", "data.yaml"))
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return emptyData

View file

@ -23,6 +23,15 @@ func Exists(path string) bool {
return false
}
func ReadFile(path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return io.ReadAll(file)
}
func CopyFile(source string, dest string) (err error) {
sourcefile, err := os.Open(source)
if err != nil {

View file

@ -2,9 +2,7 @@ package modules
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
@ -43,13 +41,7 @@ type LanguageWeather struct {
}
func LoadLanguage(language string) Language {
file, err := os.Open(filepath.Join("languages", language+".yaml"))
if err != nil {
fmt.Printf("failed reading file: %s", err)
return LoadLanguage("en")
}
defer file.Close()
yamlFile, err := io.ReadAll(file)
yamlFile, err := ReadFile(filepath.Join("languages", language+".yaml"))
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return LoadLanguage("en")