Skip to content

Commit

Permalink
Updated to allow an analyze spec from URL
Browse files Browse the repository at this point in the history
This commit includes the following changes:
1. Updated to allow a analyze spec from URL
2. Removed spec flag
The analyzer spec is a positional argument consistent with preflight.
  • Loading branch information
Jeremy Cohn committed Mar 6, 2020
1 parent 1d35229 commit 63a8131
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/preflight/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (

func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "preflight [url]",
Use: "preflight [url-or-file]",
Args: cobra.MinimumNArgs(1),
Short: "Run and retrieve preflight checks in a cluster",
Long: `A preflight check is a set of validations that can and should be run to ensure
Expand Down
59 changes: 45 additions & 14 deletions cmd/troubleshoot/cli/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"

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

func Analyze() *cobra.Command {
cmd := &cobra.Command{
Use: "analyze",
Use: "analyze [url-or-file]",
Args: cobra.MinimumNArgs(1),
Short: "analyze a support bundle",
Long: `...`,
Long: `Used to analyze an already downloaded support-bundle`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("bundle", cmd.Flags().Lookup("bundle"))
viper.BindPFlag("spec", cmd.Flags().Lookup("spec"))
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
viper.BindPFlag("quiet", cmd.Flags().Lookup("quiet"))
},
Expand All @@ -29,17 +31,13 @@ func Analyze() *cobra.Command {

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

filename := v.GetString("spec")
var analyzersSpec string
if len(filename) > 0 {
out, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
analyzersSpec = string(out)
specPath := args[0]
analyzerSpec, err := downloadAnalyzerSpec(specPath)
if err != nil {
return err
}

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

cmd.Flags().String("bundle", "", "Filename of the support bundle to analyze")
cmd.MarkFlagRequired("bundle")

cmd.Flags().String("spec", "", "Filename of the analyze yaml spec")
cmd.Flags().String("output", "", "output format: json, yaml")
cmd.Flags().String("compatibility", "", "output compatibility mode: support-bundle")
cmd.Flags().MarkHidden("compatibility")
Expand All @@ -84,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
}
2 changes: 1 addition & 1 deletion cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (

func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "troubleshoot [url]",
Use: "troubleshoot [url-or-file]",
Args: cobra.MinimumNArgs(1),
Short: "Generate and manage support bundles",
Long: `A support bundle is an archive of files, output, metrics and state
Expand Down

0 comments on commit 63a8131

Please sign in to comment.