From 036a5d3ab7fa1d41f8536913a70a50efe9c2934d Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Tue, 15 Oct 2024 15:25:03 -0600 Subject: [PATCH 1/3] util: refactor checking if path exists --- internal/util/path.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/internal/util/path.go b/internal/util/path.go index c2e3e58..de63954 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -2,6 +2,7 @@ package util import ( "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -13,15 +14,9 @@ import ( // // Returns whether the path exists and no error if successful, // otherwise, it returns false with an error. -func PathExists(path string) (bool, error) { - _, err := os.Stat(path) - if err == nil { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err +func PathExists(path string) (fs.FileInfo, bool) { + fi, err := os.Stat(path) + return fi, !os.IsNotExist(err) } // SplitPathForViper() is an utility function to split a path into 3 parts: @@ -51,17 +46,14 @@ func MakeOutputDirectory(path string, overwrite bool) (string, error) { final := path + "/" + dirname // check if path is valid and directory - pathExists, err := PathExists(final) - if err != nil { - return "", fmt.Errorf("failed to check for existing path: %v", err) - } + _, pathExists := PathExists(final) if pathExists && !overwrite { // make sure it is directory with 0o644 permissions return "", fmt.Errorf("found existing path: %v", final) } // create directory with data + time - err = os.MkdirAll(final, 0766) + err := os.MkdirAll(final, 0766) if err != nil { return "", fmt.Errorf("failed to make directory: %v", err) } From e87b2184a29296eeb3dbef4aaf2166ebcb43c606 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Tue, 15 Oct 2024 15:25:42 -0600 Subject: [PATCH 2/3] cache: refactor after updating util --- internal/cache/sqlite/sqlite.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/cache/sqlite/sqlite.go b/internal/cache/sqlite/sqlite.go index 7a04978..594fd92 100644 --- a/internal/cache/sqlite/sqlite.go +++ b/internal/cache/sqlite/sqlite.go @@ -85,11 +85,9 @@ func DeleteScannedAssets(path string, results ...magellan.RemoteAsset) error { func GetScannedAssets(path string) ([]magellan.RemoteAsset, error) { // check if path exists first to prevent creating the database - exists, err := util.PathExists(path) + _, exists := util.PathExists(path) if !exists { return nil, fmt.Errorf("no file found") - } else if err != nil { - return nil, err } // now check if the file is the SQLite database From 27fec74ceacb13cbe894afa8a05a7ec9e4c3fed3 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Tue, 15 Oct 2024 15:26:20 -0600 Subject: [PATCH 3/3] collect: fixed issue with writing output to file --- internal/collect.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/internal/collect.go b/internal/collect.go index 8c03050..e853802 100644 --- a/internal/collect.go +++ b/internal/collect.go @@ -10,13 +10,13 @@ import ( "net/http" "os" "path" + "path/filepath" "sync" "time" "github.com/OpenCHAMI/magellan/pkg/client" "github.com/OpenCHAMI/magellan/pkg/crawler" - "github.com/OpenCHAMI/magellan/internal/util" "github.com/rs/zerolog/log" "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 if outputPath != "" { - // make directory if it does exists - exists, err := util.PathExists(outputPath) - if err == nil && !exists { - err = os.MkdirAll(outputPath, 0o644) + var ( + finalPath = fmt.Sprintf("./%s/%s/%d.json", outputPath, data["ID"], time.Now().Unix()) + finalDir = filepath.Dir(finalPath) + ) + // 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 { - log.Error().Err(err).Msg("failed to make directory for output") - } 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") - } - } + log.Error().Err(err).Msgf("failed to write collect output to file") } + + } else { // error is set + log.Error().Err(err).Msg("failed to make directory for collect output") } }