Skip to content

Commit c4f841d

Browse files
authored
Merge pull request #161 from replicatedhq/laverya/check-filepath-before-attempting-url
attempt to read the file at the provided path before trying url
2 parents 2775a63 + 75ab7bb commit c4f841d

File tree

8 files changed

+50
-63
lines changed

8 files changed

+50
-63
lines changed

cmd/analyze/cli/run.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff 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-
}

cmd/preflight/cli/interactive_results.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

216217
func 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)

cmd/preflight/cli/run.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff 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

cmd/preflight/cli/util.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
package cli
22

33
import (
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-
289
func createTroubleshootK8sClient(configFlags *genericclioptions.ConfigFlags) (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
2910
config, err := configFlags.ToRESTConfig()
3011
if err != nil {

cmd/troubleshoot/cli/analyze.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"os"
99

10+
"github.com/replicatedhq/troubleshoot/cmd/util"
1011
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1112
"github.com/replicatedhq/troubleshoot/pkg/convert"
1213
"github.com/replicatedhq/troubleshoot/pkg/logger"
@@ -83,7 +84,8 @@ func Analyze() *cobra.Command {
8384

8485
func downloadAnalyzerSpec(specPath string) (string, error) {
8586
specContent := ""
86-
if !isURL(specPath) {
87+
var err error
88+
if _, err = os.Stat(specPath); err == nil {
8789
if _, err := os.Stat(specPath); os.IsNotExist(err) {
8890
return "", fmt.Errorf("%s was not found", specPath)
8991
}
@@ -95,6 +97,10 @@ func downloadAnalyzerSpec(specPath string) (string, error) {
9597

9698
specContent = string(b)
9799
} else {
100+
if !util.IsURL(specPath) {
101+
return "", fmt.Errorf("%s is not a URL and was not found (err %s)", specPath, err)
102+
}
103+
98104
req, err := http.NewRequest("GET", specPath, nil)
99105
if err != nil {
100106
return "", err

cmd/troubleshoot/cli/run.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff 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

139140
func 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 {

cmd/troubleshoot/cli/util.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@ package cli
22

33
import (
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-
2912
func createTroubleshootK8sClient(configFlags *genericclioptions.ConfigFlags) (*troubleshootclientv1beta1.TroubleshootV1beta1Client, error) {
3013
config, err := configFlags.ToRESTConfig()
3114
if err != nil {

cmd/util/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)