Skip to content

Commit

Permalink
add collected_contents.go
Browse files Browse the repository at this point in the history
  • Loading branch information
diamonwiggins authored and nvanthao committed Oct 17, 2024
1 parent 6c5aa83 commit a72c7ef
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pkg/analyze/collected_contents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package analyzer

import (
"encoding/json"
"fmt"

"github.com/pkg/errors"
"github.com/replicatedhq/troubleshoot/pkg/constants"
)

type CollectedContent struct {
NodeName string
Data CollectorData
}

type CollectorData interface{}

type NodeNames struct {
Nodes []string `json:"nodes"`
}

func retrieveCollectedContents(
getCollectedFileContents func(string) ([]byte, error),
localPath string, remoteNodeBaseDir string, remoteFileName string,
) ([]CollectedContent, error) {
var collectedContents []CollectedContent

// Try to retrieve local data first
if contents, err := getCollectedFileContents(localPath); err == nil {
collectedContents = append(collectedContents, CollectedContent{NodeName: "", Data: contents})
// Return immediately if local content is available
return collectedContents, nil
}

// Local data not available, move to remote collection
nodeListContents, err := getCollectedFileContents(constants.NODE_LIST_FILE)
if err != nil {
return nil, errors.Wrap(err, "failed to get node list")
}

var nodeNames NodeNames
if err := json.Unmarshal(nodeListContents, &nodeNames); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal node names")
}

// Collect data for each node
for _, node := range nodeNames.Nodes {
nodeFilePath := fmt.Sprintf("%s/%s/%s", remoteNodeBaseDir, node, remoteFileName)
nodeContents, err := getCollectedFileContents(nodeFilePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve content for node %s", node)
}

collectedContents = append(collectedContents, CollectedContent{NodeName: node, Data: nodeContents})
}

return collectedContents, nil
}

0 comments on commit a72c7ef

Please sign in to comment.