collect: fixed issue with writing output to file

This commit is contained in:
David Allen 2024-10-15 15:26:20 -06:00
parent e87b2184a2
commit 27fec74cea
Signed by: towk
GPG key ID: 793B2924A49B3A3F

View file

@ -10,13 +10,13 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath"
"sync" "sync"
"time" "time"
"github.com/OpenCHAMI/magellan/pkg/client" "github.com/OpenCHAMI/magellan/pkg/client"
"github.com/OpenCHAMI/magellan/pkg/crawler" "github.com/OpenCHAMI/magellan/pkg/crawler"
"github.com/OpenCHAMI/magellan/internal/util"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/Cray-HPE/hms-xname/xnames" "github.com/Cray-HPE/hms-xname/xnames"
@ -148,25 +148,20 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams) error {
// write JSON data to file if output path is set using hive partitioning strategy // write JSON data to file if output path is set using hive partitioning strategy
if outputPath != "" { if outputPath != "" {
// make directory if it does exists var (
exists, err := util.PathExists(outputPath) finalPath = fmt.Sprintf("./%s/%s/%d.json", outputPath, data["ID"], time.Now().Unix())
if err == nil && !exists { finalDir = filepath.Dir(finalPath)
err = os.MkdirAll(outputPath, 0o644) )
// if it doesn't, make the directory and write file
err = os.MkdirAll(finalDir, 0o777)
if err == nil { // no error
err = os.WriteFile(path.Clean(finalPath), body, os.ModePerm)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to make directory for output") log.Error().Err(err).Msgf("failed to write collect output to file")
} else {
// make the output directory to store files
outputPath, err := util.MakeOutputDirectory(outputPath, false)
if err != nil {
log.Error().Err(err).Msg("failed to make output directory")
} else {
// write the output to the final path
err = os.WriteFile(path.Clean(fmt.Sprintf("%s/%s/%d.json", params.URI, outputPath, time.Now().Unix())), body, os.ModePerm)
if err != nil {
log.Error().Err(err).Msgf("failed to write data to file")
}
}
} }
} else { // error is set
log.Error().Err(err).Msg("failed to make directory for collect output")
} }
} }