Skip to content

Commit 63a8131

Browse files
author
Jeremy Cohn
committed
Updated to allow an analyze spec from URL
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.
1 parent 1d35229 commit 63a8131

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

cmd/preflight/cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616

1717
func RootCmd() *cobra.Command {
1818
cmd := &cobra.Command{
19-
Use: "preflight [url]",
19+
Use: "preflight [url-or-file]",
2020
Args: cobra.MinimumNArgs(1),
2121
Short: "Run and retrieve preflight checks in a cluster",
2222
Long: `A preflight check is a set of validations that can and should be run to ensure

cmd/troubleshoot/cli/analyze.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"io/ioutil"
7+
"net/http"
8+
"os"
79

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

1618
func Analyze() *cobra.Command {
1719
cmd := &cobra.Command{
18-
Use: "analyze",
20+
Use: "analyze [url-or-file]",
21+
Args: cobra.MinimumNArgs(1),
1922
Short: "analyze a support bundle",
20-
Long: `...`,
23+
Long: `Used to analyze an already downloaded support-bundle`,
2124
PreRun: func(cmd *cobra.Command, args []string) {
2225
viper.BindPFlag("bundle", cmd.Flags().Lookup("bundle"))
23-
viper.BindPFlag("spec", cmd.Flags().Lookup("spec"))
2426
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
2527
viper.BindPFlag("quiet", cmd.Flags().Lookup("quiet"))
2628
},
@@ -29,17 +31,13 @@ func Analyze() *cobra.Command {
2931

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

32-
filename := v.GetString("spec")
33-
var analyzersSpec string
34-
if len(filename) > 0 {
35-
out, err := ioutil.ReadFile(filename)
36-
if err != nil {
37-
return err
38-
}
39-
analyzersSpec = string(out)
34+
specPath := args[0]
35+
analyzerSpec, err := downloadAnalyzerSpec(specPath)
36+
if err != nil {
37+
return err
4038
}
4139

42-
result, err := analyzer.DownloadAndAnalyze(v.GetString("bundle"), analyzersSpec)
40+
result, err := analyzer.DownloadAndAnalyze(v.GetString("bundle"), analyzerSpec)
4341
if err != nil {
4442
return err
4543
}
@@ -73,8 +71,6 @@ func Analyze() *cobra.Command {
7371

7472
cmd.Flags().String("bundle", "", "Filename of the support bundle to analyze")
7573
cmd.MarkFlagRequired("bundle")
76-
77-
cmd.Flags().String("spec", "", "Filename of the analyze yaml spec")
7874
cmd.Flags().String("output", "", "output format: json, yaml")
7975
cmd.Flags().String("compatibility", "", "output compatibility mode: support-bundle")
8076
cmd.Flags().MarkHidden("compatibility")
@@ -84,3 +80,38 @@ func Analyze() *cobra.Command {
8480

8581
return cmd
8682
}
83+
84+
func downloadAnalyzerSpec(specPath string) (string, error) {
85+
specContent := ""
86+
if !isURL(specPath) {
87+
if _, err := os.Stat(specPath); os.IsNotExist(err) {
88+
return "", fmt.Errorf("%s was not found", specPath)
89+
}
90+
91+
b, err := ioutil.ReadFile(specPath)
92+
if err != nil {
93+
return "", err
94+
}
95+
96+
specContent = string(b)
97+
} else {
98+
req, err := http.NewRequest("GET", specPath, nil)
99+
if err != nil {
100+
return "", err
101+
}
102+
req.Header.Set("User-Agent", "Replicated_Analyzer/v1beta1")
103+
resp, err := http.DefaultClient.Do(req)
104+
if err != nil {
105+
return "", err
106+
}
107+
defer resp.Body.Close()
108+
109+
body, err := ioutil.ReadAll(resp.Body)
110+
if err != nil {
111+
return "", err
112+
}
113+
114+
specContent = string(body)
115+
}
116+
return specContent, nil
117+
}

cmd/troubleshoot/cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717

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

0 commit comments

Comments
 (0)