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
1618func 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+ }
0 commit comments