mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
chore: more refactoring and cleanup
This commit is contained in:
parent
aecb56971d
commit
844ce3c3e0
3 changed files with 29 additions and 33 deletions
|
|
@ -3,10 +3,10 @@ package cmd
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os/user"
|
||||
|
||||
"github.com/OpenCHAMI/magellan/internal/cache/sqlite"
|
||||
urlx "github.com/OpenCHAMI/magellan/internal/url"
|
||||
"github.com/OpenCHAMI/magellan/internal/util"
|
||||
magellan "github.com/OpenCHAMI/magellan/pkg"
|
||||
"github.com/OpenCHAMI/magellan/pkg/auth"
|
||||
"github.com/OpenCHAMI/magellan/pkg/bmc"
|
||||
|
|
@ -141,14 +141,13 @@ var CollectCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
func init() {
|
||||
currentUser, _ = user.Current()
|
||||
CollectCmd.Flags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint")
|
||||
CollectCmd.Flags().StringVarP(&username, "username", "u", "", "Set the master BMC username")
|
||||
CollectCmd.Flags().StringVarP(&password, "password", "p", "", "Set the master BMC password")
|
||||
CollectCmd.Flags().StringVar(&secretsFile, "secrets-file", "", "Set path to the node secrets file")
|
||||
CollectCmd.Flags().StringVar(&scheme, "scheme", "https", "Set the default scheme used to query when not included in URI")
|
||||
CollectCmd.Flags().StringVar(&protocol, "protocol", "tcp", "Set the protocol used to query")
|
||||
CollectCmd.Flags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", currentUser.Username+"/"), "Set the path to store collection data")
|
||||
CollectCmd.Flags().StringVarP(&outputPath, "output", "o", fmt.Sprintf("/tmp/%smagellan/inventory/", util.GetCurrentUsername()+"/"), "Set the path to store collection data")
|
||||
CollectCmd.Flags().BoolVar(&forceUpdate, "force-update", false, "Set flag to force update data sent to SMD")
|
||||
CollectCmd.Flags().StringVar(&cacertPath, "cacert", "", "Set the path to CA cert file. (defaults to system CAs when blank)")
|
||||
CollectCmd.Flags().StringVarP(&format, "format", "F", "hive", "Set the output format (json|yaml)")
|
||||
|
|
|
|||
|
|
@ -36,7 +36,21 @@ func NewClient[T Client](opts ...func(T)) T {
|
|||
return *client
|
||||
}
|
||||
|
||||
func WithCertPool(client Client, certPool *x509.CertPool) error {
|
||||
func LoadCertificateFromPath(client Client, path string) error {
|
||||
cacert, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read certificate at path: %s", path)
|
||||
}
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AppendCertsFromPEM(cacert)
|
||||
err = LoadCertificateFromPool(client, certPool)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not initialize certificate from pool: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadCertificateFromPool(client Client, certPool *x509.CertPool) error {
|
||||
// make sure we have a valid cert pool
|
||||
if certPool == nil {
|
||||
return fmt.Errorf("invalid cert pool")
|
||||
|
|
@ -63,16 +77,6 @@ func WithCertPool(client Client, certPool *x509.CertPool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func WithSecureTLS(client Client, certPath string) error {
|
||||
cacert, err := os.ReadFile(certPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read certificate from path '%s': %v", certPath, err)
|
||||
}
|
||||
certPool := x509.NewCertPool()
|
||||
certPool.AppendCertsFromPEM(cacert)
|
||||
return WithCertPool(client, certPool)
|
||||
}
|
||||
|
||||
// Post() is a simplified wrapper function that packages all of the
|
||||
// that marshals a mapper into a JSON-formatted byte array, and then performs
|
||||
// a request to the specified URL.
|
||||
|
|
|
|||
|
|
@ -205,26 +205,19 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[strin
|
|||
|
||||
// write data to file if output path is set using set format
|
||||
if outputPath != "" {
|
||||
switch params.Format {
|
||||
case "hive":
|
||||
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).Msgf("failed to write collect output to file")
|
||||
}
|
||||
} else { // error is set
|
||||
log.Error().Err(err).Msg("failed to make directory for collect output")
|
||||
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).Msgf("failed to write collect output to file")
|
||||
}
|
||||
case "json":
|
||||
case "yaml":
|
||||
|
||||
default:
|
||||
} else { // error is set
|
||||
log.Error().Err(err).Msg("failed to make directory for collect output")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue