Skip to content

Commit

Permalink
Merge pull request #147 from replicatedhq/jeremy/fix-analyze
Browse files Browse the repository at this point in the history
Added spec support to `analyze` function in support-bundle
  • Loading branch information
moonlight16 committed Mar 6, 2020
2 parents 26d8211 + f6a56a9 commit 2158dee
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions cmd/troubleshoot/cli/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cli
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"

analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
"github.com/replicatedhq/troubleshoot/pkg/convert"
Expand All @@ -14,11 +17,12 @@ import (

func Analyze() *cobra.Command {
cmd := &cobra.Command{
Use: "analyze",
Use: "analyze [url]",
Args: cobra.MinimumNArgs(1),
Short: "analyze a support bundle",
Long: `...`,
Long: `Analyze a support bundle using the Analyzer definitions provided`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("url", cmd.Flags().Lookup("url"))
viper.BindPFlag("bundle", cmd.Flags().Lookup("bundle"))
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
viper.BindPFlag("quiet", cmd.Flags().Lookup("quiet"))
},
Expand All @@ -27,7 +31,13 @@ func Analyze() *cobra.Command {

logger.SetQuiet(v.GetBool("quiet"))

result, err := analyzer.DownloadAndAnalyze(v.GetString("url"), "")
specPath := args[0]
analyzerSpec, err := downloadAnalyzerSpec(specPath)
if err != nil {
return err
}

result, err := analyzer.DownloadAndAnalyze(v.GetString("bundle"), analyzerSpec)
if err != nil {
return err
}
Expand Down Expand Up @@ -59,8 +69,8 @@ func Analyze() *cobra.Command {
},
}

cmd.Flags().String("url", "", "URL of the support bundle to analyze")
cmd.MarkFlagRequired("url")
cmd.Flags().String("bundle", "", "filename of the support bundle to analyze")
cmd.MarkFlagRequired("bundle")
cmd.Flags().String("output", "", "output format: json, yaml")
cmd.Flags().String("compatibility", "", "output compatibility mode: support-bundle")
cmd.Flags().MarkHidden("compatibility")
Expand All @@ -70,3 +80,38 @@ func Analyze() *cobra.Command {

return cmd
}

func downloadAnalyzerSpec(specPath string) (string, error) {
specContent := ""
if !isURL(specPath) {
if _, err := os.Stat(specPath); os.IsNotExist(err) {
return "", fmt.Errorf("%s was not found", specPath)
}

b, err := ioutil.ReadFile(specPath)
if err != nil {
return "", err
}

specContent = string(b)
} else {
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)
}
return specContent, nil
}

0 comments on commit 2158dee

Please sign in to comment.