chore: more refactoring and cleanup

This commit is contained in:
David Allen 2025-04-23 18:33:09 -06:00
parent aecb56971d
commit 844ce3c3e0
Signed by: towk
GPG key ID: 0430CDBE22619155
3 changed files with 29 additions and 33 deletions

View file

@ -3,10 +3,10 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os/user"
"github.com/OpenCHAMI/magellan/internal/cache/sqlite" "github.com/OpenCHAMI/magellan/internal/cache/sqlite"
urlx "github.com/OpenCHAMI/magellan/internal/url" urlx "github.com/OpenCHAMI/magellan/internal/url"
"github.com/OpenCHAMI/magellan/internal/util"
magellan "github.com/OpenCHAMI/magellan/pkg" magellan "github.com/OpenCHAMI/magellan/pkg"
"github.com/OpenCHAMI/magellan/pkg/auth" "github.com/OpenCHAMI/magellan/pkg/auth"
"github.com/OpenCHAMI/magellan/pkg/bmc" "github.com/OpenCHAMI/magellan/pkg/bmc"
@ -141,14 +141,13 @@ var CollectCmd = &cobra.Command{
} }
func init() { func init() {
currentUser, _ = user.Current()
CollectCmd.Flags().StringVar(&host, "host", "", "Set the URI to the SMD root endpoint") 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(&username, "username", "u", "", "Set the master BMC username")
CollectCmd.Flags().StringVarP(&password, "password", "p", "", "Set the master BMC password") 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(&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(&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().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().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().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)") CollectCmd.Flags().StringVarP(&format, "format", "F", "hive", "Set the output format (json|yaml)")

View file

@ -36,7 +36,21 @@ func NewClient[T Client](opts ...func(T)) T {
return *client 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 // make sure we have a valid cert pool
if certPool == nil { if certPool == nil {
return fmt.Errorf("invalid cert pool") return fmt.Errorf("invalid cert pool")
@ -63,16 +77,6 @@ func WithCertPool(client Client, certPool *x509.CertPool) error {
return nil 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 // Post() is a simplified wrapper function that packages all of the
// that marshals a mapper into a JSON-formatted byte array, and then performs // that marshals a mapper into a JSON-formatted byte array, and then performs
// a request to the specified URL. // a request to the specified URL.

View file

@ -205,8 +205,6 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[strin
// write data to file if output path is set using set format // write data to file if output path is set using set format
if outputPath != "" { if outputPath != "" {
switch params.Format {
case "hive":
var ( var (
finalPath = fmt.Sprintf("./%s/%s/%d.json", outputPath, data["ID"], time.Now().Unix()) finalPath = fmt.Sprintf("./%s/%s/%d.json", outputPath, data["ID"], time.Now().Unix())
finalDir = filepath.Dir(finalPath) finalDir = filepath.Dir(finalPath)
@ -221,11 +219,6 @@ func CollectInventory(assets *[]RemoteAsset, params *CollectParams) ([]map[strin
} else { // error is set } else { // error is set
log.Error().Err(err).Msg("failed to make directory for collect output") log.Error().Err(err).Msg("failed to make directory for collect output")
} }
case "json":
case "yaml":
default:
}
} }
// add all endpoints to SMD ONLY if a host is provided // add all endpoints to SMD ONLY if a host is provided