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

@ -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.

View file

@ -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")
}
}