mirror of
https://github.com/mudler/LocalAI.git
synced 2024-06-07 19:40:48 +00:00
b85dad0286
Signed-off-by: Chris Jowett <421501+cryptk@users.noreply.github.com>
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func SaveConfig(filePath, fileName string, obj any) {
|
|
file, err := json.MarshalIndent(obj, "", " ")
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("failed to JSON marshal the uploadedFiles")
|
|
}
|
|
|
|
absolutePath := filepath.Join(filePath, fileName)
|
|
err = os.WriteFile(absolutePath, file, 0644)
|
|
if err != nil {
|
|
log.Error().Err(err).Str("filepath", absolutePath).Msg("failed to save configuration file")
|
|
}
|
|
}
|
|
|
|
func LoadConfig(filePath, fileName string, obj interface{}) {
|
|
uploadFilePath := filepath.Join(filePath, fileName)
|
|
|
|
_, err := os.Stat(uploadFilePath)
|
|
if os.IsNotExist(err) {
|
|
log.Debug().Msgf("No configuration file found at %s", uploadFilePath)
|
|
return
|
|
}
|
|
|
|
file, err := os.ReadFile(uploadFilePath)
|
|
if err != nil {
|
|
log.Error().Err(err).Str("filepath", uploadFilePath).Msg("failed to read file")
|
|
} else {
|
|
err = json.Unmarshal(file, &obj)
|
|
if err != nil {
|
|
log.Error().Err(err).Str("filepath", uploadFilePath).Msg("failed to parse file as JSON")
|
|
}
|
|
}
|
|
}
|