2024-03-26 17:54:35 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-04-04 07:24:22 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-03-26 17:54:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SaveConfig(filePath, fileName string, obj any) {
|
|
|
|
file, err := json.MarshalIndent(obj, "", " ")
|
|
|
|
if err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Msg("failed to JSON marshal the uploadedFiles")
|
2024-03-26 17:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
absolutePath := filepath.Join(filePath, fileName)
|
2024-04-25 22:47:06 +00:00
|
|
|
err = os.WriteFile(absolutePath, file, 0600)
|
2024-03-26 17:54:35 +00:00
|
|
|
if err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Str("filepath", absolutePath).Msg("failed to save configuration file")
|
2024-03-26 17:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Str("filepath", uploadFilePath).Msg("failed to read file")
|
2024-03-26 17:54:35 +00:00
|
|
|
} else {
|
|
|
|
err = json.Unmarshal(file, &obj)
|
|
|
|
if err != nil {
|
2024-04-04 07:24:22 +00:00
|
|
|
log.Error().Err(err).Str("filepath", uploadFilePath).Msg("failed to parse file as JSON")
|
2024-03-26 17:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|