Adjusted file save implementation for readability, and to handle missing folders

This commit is contained in:
luke-hagar-sp
2023-10-03 15:38:03 -05:00
parent fb8f03186e
commit d0866e1683
2 changed files with 22 additions and 8 deletions

View File

@@ -13,9 +13,9 @@ import (
)
func SaveJSONFile[T any](formattedResponse T, fileName string, folderPath string) error {
savePath := GetSanitizedPath(folderPath, fileName, "json")
saveName := GetSanitizedPath(fileName, "json")
log.Debug("Saving JSON file", "file path", savePath)
log.Debug("Saving JSON file", "path", folderPath, "file", saveName)
dataToSave, err := json.MarshalIndent(formattedResponse, "", " ")
if err != nil {
@@ -24,11 +24,25 @@ func SaveJSONFile[T any](formattedResponse T, fileName string, folderPath string
log.Debug("Formatted Data", "data", string(dataToSave))
return WriteFile(savePath, dataToSave)
saveErr := WriteFile(folderPath, saveName, dataToSave)
if saveErr != nil {
return saveErr
}
return nil
}
func WriteFile(path string, data []byte) error {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0777)
func WriteFile(folderPath string, filePath string, data []byte) error {
// Create the folder if it doesn't exist
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
err = os.MkdirAll(folderPath, 0777)
if err != nil {
return err
}
}
file, err := os.OpenFile(path.Join(folderPath, filePath), os.O_CREATE|os.O_RDWR, 0777)
if err != nil {
return err
}
@@ -54,8 +68,8 @@ func WriteFile(path string, data []byte) error {
}
// GetSanitizedPath makes sure the provided path works on all OS
func GetSanitizedPath(filePath string, fileName string, extension string) string {
return path.Join(filePath, sanitize.PathName(fileName)+"."+extension)
func GetSanitizedPath(fileName string, extension string) string {
return sanitize.PathName(fileName) + "." + extension
}
func WriteTable(writer io.Writer, headers []string, entries [][]string) {

View File

@@ -171,7 +171,7 @@ func SaveResults[T any](formattedResponse []T, fileName string, filePath string,
outputType := outputTypes[i]
switch outputType {
case "json":
savePath := output.GetSanitizedPath(filePath, fileName, "json")
savePath := output.GetSanitizedPath(fileName, "json")
log.Info("Saving Results", "file", savePath)
err := output.SaveJSONFile(formattedResponse, fileName, filePath)
if err != nil {