mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
* fix(support-bundle): default in-cluster collectors in host support bundle Ensure cluster-resources and cluster-info collectors are present only when a support bundle spec contains in-cluster collectors. * Various improvements * Improve error messages * Util function appending elements to a nil slice that allows adding specs to an empty slice of collectors/analysers/redactors * Fix failing test
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/replicatedhq/troubleshoot/internal/util"
|
|
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func runAnalyzers(v *viper.Viper, bundlePath string) error {
|
|
specPath := v.GetString("analyzers")
|
|
|
|
specContent := ""
|
|
var err error
|
|
if _, err = os.Stat(specPath); err == nil {
|
|
b, err := os.ReadFile(specPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
specContent = string(b)
|
|
} else {
|
|
if !util.IsURL(specPath) {
|
|
// TODO: Better error message when we do not have a file/url etc
|
|
return fmt.Errorf("%s is not a URL and was not found", specPath)
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", specPath, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("User-Agent", "Replicated_Analyzer/v1beta1")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
specContent = string(body)
|
|
}
|
|
|
|
analyzeResults, err := analyzer.DownloadAndAnalyze(bundlePath, specContent)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to download and analyze bundle")
|
|
}
|
|
|
|
for _, analyzeResult := range analyzeResults {
|
|
if analyzeResult.IsPass {
|
|
fmt.Printf("Pass: %s\n %s\n", analyzeResult.Title, analyzeResult.Message)
|
|
} else if analyzeResult.IsWarn {
|
|
fmt.Printf("Warn: %s\n %s\n", analyzeResult.Title, analyzeResult.Message)
|
|
} else if analyzeResult.IsFail {
|
|
fmt.Printf("Fail: %s\n %s\n", analyzeResult.Title, analyzeResult.Message)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|