File tree Expand file tree Collapse file tree 7 files changed +43
-62
lines changed Expand file tree Collapse file tree 7 files changed +43
-62
lines changed Original file line number Diff line number Diff line change @@ -4,10 +4,10 @@ import (
44 "fmt"
55 "io/ioutil"
66 "net/http"
7- "net/url"
87 "os"
98
109 "github.com/pkg/errors"
10+ "github.com/replicatedhq/troubleshoot/cmd/util"
1111 analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1212 "github.com/spf13/viper"
1313)
@@ -16,18 +16,19 @@ func runAnalyzers(v *viper.Viper, bundlePath string) error {
1616 specPath := v .GetString ("analyzers" )
1717
1818 specContent := ""
19- if ! isURL (specPath ) {
20- if _ , err := os .Stat (specPath ); os .IsNotExist (err ) {
21- return fmt .Errorf ("%s was not found" , specPath )
22- }
23-
19+ var err error
20+ if _ , err = os .Stat (specPath ); err == nil {
2421 b , err := ioutil .ReadFile (specPath )
2522 if err != nil {
2623 return err
2724 }
2825
2926 specContent = string (b )
3027 } else {
28+ if ! util .IsURL (specPath ) {
29+ return fmt .Errorf ("%s is not a URL and was not found (err %s)" , specPath , err )
30+ }
31+
3132 req , err := http .NewRequest ("GET" , specPath , nil )
3233 if err != nil {
3334 return err
@@ -64,12 +65,3 @@ func runAnalyzers(v *viper.Viper, bundlePath string) error {
6465
6566 return nil
6667}
67-
68- func isURL (str string ) bool {
69- parsed , err := url .ParseRequestURI (str )
70- if err != nil {
71- return false
72- }
73-
74- return parsed .Scheme != ""
75- }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
1111 ui "github.com/gizak/termui/v3"
1212 "github.com/gizak/termui/v3/widgets"
1313 "github.com/pkg/errors"
14+ "github.com/replicatedhq/troubleshoot/cmd/util"
1415 analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
1516)
1617
@@ -214,7 +215,7 @@ func estimateNumberOfLines(text string, width int) int {
214215}
215216
216217func save (preflightName string , analyzeResults []* analyzerunner.AnalyzeResult ) (string , error ) {
217- filename := path .Join (homeDir (), fmt .Sprintf ("%s-results.txt" , preflightName ))
218+ filename := path .Join (util . HomeDir (), fmt .Sprintf ("%s-results.txt" , preflightName ))
218219 _ , err := os .Stat (filename )
219220 if err == nil {
220221 os .Remove (filename )
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import (
1010 cursor "github.com/ahmetalpbalkan/go-cursor"
1111 "github.com/fatih/color"
1212 "github.com/pkg/errors"
13+ "github.com/replicatedhq/troubleshoot/cmd/util"
1314 troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
1415 troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
1516 "github.com/replicatedhq/troubleshoot/pkg/preflight"
@@ -23,18 +24,19 @@ func runPreflights(v *viper.Viper, arg string) error {
2324 defer fmt .Print (cursor .Show ())
2425
2526 preflightContent := ""
26- if ! isURL (arg ) {
27- if _ , err := os .Stat (arg ); os .IsNotExist (err ) {
28- return fmt .Errorf ("%s was not found" , arg )
29- }
30-
27+ var err error
28+ if _ , err = os .Stat (arg ); err == nil {
3129 b , err := ioutil .ReadFile (arg )
3230 if err != nil {
3331 return err
3432 }
3533
3634 preflightContent = string (b )
3735 } else {
36+ if ! util .IsURL (arg ) {
37+ return fmt .Errorf ("%s is not a URL and was not found (err %s)" , arg , err )
38+ }
39+
3840 req , err := http .NewRequest ("GET" , arg , nil )
3941 if err != nil {
4042 return err
Original file line number Diff line number Diff line change 11package cli
22
33import (
4- "net/url"
5- "os"
6-
74 "github.com/pkg/errors"
85 troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
96 "k8s.io/cli-runtime/pkg/genericclioptions"
107)
118
12- func homeDir () string {
13- if h := os .Getenv ("HOME" ); h != "" {
14- return h
15- }
16- return os .Getenv ("USERPROFILE" ) // windows
17- }
18-
19- func isURL (str string ) bool {
20- parsed , err := url .ParseRequestURI (str )
21- if err != nil {
22- return false
23- }
24-
25- return parsed .Scheme != ""
26- }
27-
289func createTroubleshootK8sClient (configFlags * genericclioptions.ConfigFlags ) (* troubleshootclientv1beta1.TroubleshootV1beta1Client , error ) {
2910 config , err := configFlags .ToRESTConfig ()
3011 if err != nil {
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import (
1919 "github.com/mattn/go-isatty"
2020 "github.com/mholt/archiver"
2121 "github.com/pkg/errors"
22+ "github.com/replicatedhq/troubleshoot/cmd/util"
2223 troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
2324 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
2425 troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
@@ -137,17 +138,16 @@ the %s Admin Console to begin analysis.`
137138}
138139
139140func loadSpec (v * viper.Viper , arg string ) ([]byte , error ) {
140- if ! isURL (arg ) {
141- if _ , err := os .Stat (arg ); os .IsNotExist (err ) {
142- return nil , fmt .Errorf ("%s was not found" , arg )
143- }
144-
141+ var err error
142+ if _ , err = os .Stat (arg ); err == nil {
145143 b , err := ioutil .ReadFile (arg )
146144 if err != nil {
147145 return nil , errors .Wrap (err , "read spec file" )
148146 }
149147
150148 return b , nil
149+ } else if ! util .IsURL (arg ) {
150+ return nil , fmt .Errorf ("%s is not a URL and was not found (err %s)" , arg , err )
151151 }
152152
153153 for {
Original file line number Diff line number Diff line change @@ -2,30 +2,13 @@ package cli
22
33import (
44 "fmt"
5- "net/url"
65 "os"
76
87 "github.com/pkg/errors"
98 troubleshootclientv1beta1 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/typed/troubleshoot/v1beta1"
109 "k8s.io/cli-runtime/pkg/genericclioptions"
1110)
1211
13- func homeDir () string {
14- if h := os .Getenv ("HOME" ); h != "" {
15- return h
16- }
17- return os .Getenv ("USERPROFILE" ) // windows
18- }
19-
20- func isURL (str string ) bool {
21- parsed , err := url .ParseRequestURI (str )
22- if err != nil {
23- return false
24- }
25-
26- return parsed .Scheme != ""
27- }
28-
2912func createTroubleshootK8sClient (configFlags * genericclioptions.ConfigFlags ) (* troubleshootclientv1beta1.TroubleshootV1beta1Client , error ) {
3013 config , err := configFlags .ToRESTConfig ()
3114 if err != nil {
Original file line number Diff line number Diff line change 1+ package util
2+
3+ import (
4+ "net/url"
5+ "os"
6+ )
7+
8+ func HomeDir () string {
9+ if h := os .Getenv ("HOME" ); h != "" {
10+ return h
11+ }
12+ return os .Getenv ("USERPROFILE" ) // windows
13+ }
14+
15+ func IsURL (str string ) bool {
16+ parsed , err := url .ParseRequestURI (str )
17+ if err != nil {
18+ return false
19+ }
20+
21+ return parsed .Scheme != ""
22+ }
You can’t perform that action at this time.
0 commit comments