saving new config file only, use name.json as filename

This commit is contained in:
Baipyrus 2024-09-24 15:25:11 +02:00
parent 6d4f6db33d
commit 89af1d1cc1

View File

@ -2,6 +2,7 @@ package util
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
"strings" "strings"
@ -47,15 +48,14 @@ func ReadConfigs(cfgPath string) ([]*Config, error) {
return configs, err return configs, err
} }
func SaveConfig(name string, config Config) error { func SaveConfig(cfgPath string, config Config) error {
configs, _ := ReadConfigs(name) data, err := json.Marshal(config)
configs = append(configs, &config)
data, err := json.Marshal(configs)
if err != nil { if err != nil {
return err return err
} }
err = os.WriteFile(name, data, 0666) cfgName := fmt.Sprintf("%s.json", config.Name)
cfgFile := filepath.Join(cfgPath, cfgName)
err = os.WriteFile(cfgFile, data, 0666)
return err return err
} }