mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 21:27:17 -07:00
Merge pull request #63 from OpenCHAMI/fix-output-flag
Fix issues related to writing output to file
This commit is contained in:
commit
f674ecb8be
3 changed files with 20 additions and 35 deletions
4
internal/cache/sqlite/sqlite.go
vendored
4
internal/cache/sqlite/sqlite.go
vendored
|
|
@ -85,11 +85,9 @@ func DeleteScannedAssets(path string, results ...magellan.RemoteAsset) error {
|
||||||
|
|
||||||
func GetScannedAssets(path string) ([]magellan.RemoteAsset, error) {
|
func GetScannedAssets(path string) ([]magellan.RemoteAsset, error) {
|
||||||
// check if path exists first to prevent creating the database
|
// check if path exists first to prevent creating the database
|
||||||
exists, err := util.PathExists(path)
|
_, exists := util.PathExists(path)
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, fmt.Errorf("no file found")
|
return nil, fmt.Errorf("no file found")
|
||||||
} else if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// now check if the file is the SQLite database
|
// now check if the file is the SQLite database
|
||||||
|
|
|
||||||
|
|
@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -13,15 +14,9 @@ import (
|
||||||
//
|
//
|
||||||
// Returns whether the path exists and no error if successful,
|
// Returns whether the path exists and no error if successful,
|
||||||
// otherwise, it returns false with an error.
|
// otherwise, it returns false with an error.
|
||||||
func PathExists(path string) (bool, error) {
|
func PathExists(path string) (fs.FileInfo, bool) {
|
||||||
_, err := os.Stat(path)
|
fi, err := os.Stat(path)
|
||||||
if err == nil {
|
return fi, !os.IsNotExist(err)
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SplitPathForViper() is an utility function to split a path into 3 parts:
|
// 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
|
final := path + "/" + dirname
|
||||||
|
|
||||||
// check if path is valid and directory
|
// check if path is valid and directory
|
||||||
pathExists, err := PathExists(final)
|
_, pathExists := PathExists(final)
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to check for existing path: %v", err)
|
|
||||||
}
|
|
||||||
if pathExists && !overwrite {
|
if pathExists && !overwrite {
|
||||||
// make sure it is directory with 0o644 permissions
|
// make sure it is directory with 0o644 permissions
|
||||||
return "", fmt.Errorf("found existing path: %v", final)
|
return "", fmt.Errorf("found existing path: %v", final)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create directory with data + time
|
// create directory with data + time
|
||||||
err = os.MkdirAll(final, 0766)
|
err := os.MkdirAll(final, 0766)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to make directory: %v", err)
|
return "", fmt.Errorf("failed to make directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue