Skip to content

Commit de03710

Browse files
author
Nathan Sullivan
authored
preflight: ensure --output produces an output file of the desired format (#951)
1 parent 2b4bcd2 commit de03710

File tree

5 files changed

+222
-192
lines changed

5 files changed

+222
-192
lines changed

pkg/preflight/interactive_results.go

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package preflight
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
"os"
75
"time"
86

97
"github.com/pkg/errors"
108
ui "github.com/replicatedhq/termui/v3"
119
"github.com/replicatedhq/termui/v3/widgets"
1210
"github.com/replicatedhq/troubleshoot/cmd/util"
1311
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
14-
"github.com/replicatedhq/troubleshoot/pkg/convert"
1512
)
1613

1714
var (
@@ -44,7 +41,7 @@ func showInteractiveResults(preflightName string, outputPath string, analyzeResu
4441
return nil
4542
}
4643
case "s":
47-
filename, err := save(preflightName, outputPath, analyzeResults)
44+
filename, err := outputToFile(preflightName, outputPath, analyzeResults)
4845
if err != nil {
4946
// show
5047
} else {
@@ -220,60 +217,6 @@ func estimateNumberOfLines(text string, width int) int {
220217
return lines
221218
}
222219

223-
func save(preflightName string, outputPath string, analyzeResults []*analyzerunner.AnalyzeResult) (string, error) {
224-
filename := ""
225-
if outputPath != "" {
226-
// use override output path
227-
overridePath, err := convert.ValidateOutputPath(outputPath)
228-
if err != nil {
229-
return "", errors.Wrap(err, "override output file path")
230-
}
231-
filename = overridePath
232-
} else {
233-
// use default output path
234-
filename = fmt.Sprintf("%s-results-%s.txt", preflightName, time.Now().Format("2006-01-02T15_04_05"))
235-
}
236-
237-
_, err := os.Stat(filename)
238-
if err == nil {
239-
os.Remove(filename)
240-
}
241-
242-
results := fmt.Sprintf("%s Preflight Checks\n\n", util.AppName(preflightName))
243-
for _, analyzeResult := range analyzeResults {
244-
result := ""
245-
246-
if analyzeResult.IsPass {
247-
result = "Check PASS\n"
248-
} else if analyzeResult.IsWarn {
249-
result = "Check WARN\n"
250-
} else if analyzeResult.IsFail {
251-
result = "Check FAIL\n"
252-
}
253-
254-
result = result + fmt.Sprintf("Title: %s\n", analyzeResult.Title)
255-
result = result + fmt.Sprintf("Message: %s\n", analyzeResult.Message)
256-
257-
if analyzeResult.URI != "" {
258-
result = result + fmt.Sprintf("URI: %s\n", analyzeResult.URI)
259-
}
260-
261-
if analyzeResult.Strict {
262-
result = result + fmt.Sprintf("Strict: %t\n", analyzeResult.Strict)
263-
}
264-
265-
result = result + "\n------------\n"
266-
267-
results = results + result
268-
}
269-
270-
if err := ioutil.WriteFile(filename, []byte(results), 0644); err != nil {
271-
return "", errors.Wrap(err, "failed to save preflight results")
272-
}
273-
274-
return filename, nil
275-
}
276-
277220
func showSaved(filename string) {
278221
termWidth, termHeight := ui.TerminalDimensions()
279222

pkg/preflight/output_to_file.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package preflight
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"time"
8+
9+
"github.com/pkg/errors"
10+
"github.com/replicatedhq/troubleshoot/cmd/util"
11+
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
12+
"github.com/replicatedhq/troubleshoot/pkg/convert"
13+
)
14+
15+
func outputToFile(preflightName string, outputPath string, analyzeResults []*analyzerunner.AnalyzeResult) (string, error) {
16+
filename := ""
17+
if outputPath != "" {
18+
// use override output path
19+
overridePath, err := convert.ValidateOutputPath(outputPath)
20+
if err != nil {
21+
return "", errors.Wrap(err, "override output file path")
22+
}
23+
filename = overridePath
24+
} else {
25+
// use default output path
26+
filename = fmt.Sprintf("%s-results-%s.txt", preflightName, time.Now().Format("2006-01-02T15_04_05"))
27+
}
28+
29+
_, err := os.Stat(filename)
30+
if err == nil {
31+
os.Remove(filename)
32+
}
33+
34+
results := fmt.Sprintf("%s Preflight Checks\n\n", util.AppName(preflightName))
35+
for _, analyzeResult := range analyzeResults {
36+
result := ""
37+
38+
if analyzeResult.IsPass {
39+
result = "Check PASS\n"
40+
} else if analyzeResult.IsWarn {
41+
result = "Check WARN\n"
42+
} else if analyzeResult.IsFail {
43+
result = "Check FAIL\n"
44+
}
45+
46+
result = result + fmt.Sprintf("Title: %s\n", analyzeResult.Title)
47+
result = result + fmt.Sprintf("Message: %s\n", analyzeResult.Message)
48+
49+
if analyzeResult.URI != "" {
50+
result = result + fmt.Sprintf("URI: %s\n", analyzeResult.URI)
51+
}
52+
53+
if analyzeResult.Strict {
54+
result = result + fmt.Sprintf("Strict: %t\n", analyzeResult.Strict)
55+
}
56+
57+
result = result + "\n------------\n"
58+
59+
results = results + result
60+
}
61+
62+
if err := ioutil.WriteFile(filename, []byte(results), 0644); err != nil {
63+
return "", errors.Wrap(err, "failed to save preflight results")
64+
}
65+
66+
return filename, nil
67+
}

pkg/preflight/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func RunPreflights(interactive bool, output string, format string, args []string
236236
return showInteractiveResults(preflightSpecName, output, analyzeResults)
237237
}
238238

239-
return showStdoutResults(format, preflightSpecName, analyzeResults)
239+
return showTextResults(format, preflightSpecName, output, analyzeResults)
240240
}
241241

242242
func collectInteractiveProgress(ctx context.Context, progressCh <-chan interface{}) func() error {

pkg/preflight/stdout_results.go

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)