Skip to content

Commit 0a2ed01

Browse files
authored
improvement: added --output flag for preflight and support bundle (#538)
* improvement: added --output flag for preflight and support bundle * improvement: added datetime to preflight default file name
1 parent a818417 commit 0a2ed01

File tree

7 files changed

+56
-9
lines changed

7 files changed

+56
-9
lines changed

cmd/preflight/cli/interactive_results.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7-
"path"
87
"time"
98

109
"github.com/pkg/errors"
1110
ui "github.com/replicatedhq/termui/v3"
1211
"github.com/replicatedhq/termui/v3/widgets"
1312
"github.com/replicatedhq/troubleshoot/cmd/util"
1413
analyzerunner "github.com/replicatedhq/troubleshoot/pkg/analyze"
14+
"github.com/replicatedhq/troubleshoot/pkg/convert"
1515
)
1616

1717
var (
@@ -20,7 +20,7 @@ var (
2020
isShowingSaved = false
2121
)
2222

23-
func showInteractiveResults(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) error {
23+
func showInteractiveResults(preflightName string, outputPath string, analyzeResults []*analyzerunner.AnalyzeResult) error {
2424
if err := ui.Init(); err != nil {
2525
return errors.Wrap(err, "failed to create terminal ui")
2626
}
@@ -44,7 +44,7 @@ func showInteractiveResults(preflightName string, analyzeResults []*analyzerunne
4444
return nil
4545
}
4646
case "s":
47-
filename, err := save(preflightName, analyzeResults)
47+
filename, err := save(preflightName, outputPath, analyzeResults)
4848
if err != nil {
4949
// show
5050
} else {
@@ -217,8 +217,20 @@ func estimateNumberOfLines(text string, width int) int {
217217
return lines
218218
}
219219

220-
func save(preflightName string, analyzeResults []*analyzerunner.AnalyzeResult) (string, error) {
221-
filename := path.Join(util.HomeDir(), fmt.Sprintf("%s-results.txt", preflightName))
220+
func save(preflightName string, outputPath string, analyzeResults []*analyzerunner.AnalyzeResult) (string, error) {
221+
filename := ""
222+
if outputPath != "" {
223+
// use override output path
224+
overridePath, err := convert.ValidateOutputPath(outputPath)
225+
if err != nil {
226+
return "", errors.Wrap(err, "override output file path")
227+
}
228+
filename = overridePath
229+
} else {
230+
// use default output path
231+
filename = fmt.Sprintf("%s-results-%s.txt", preflightName, time.Now().Format("2006-01-02T15_04_05"))
232+
}
233+
222234
_, err := os.Stat(filename)
223235
if err == nil {
224236
os.Remove(filename)

cmd/preflight/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ that a cluster meets the requirements to run an application.`,
3838
cmd.Flags().String("selector", "", "selector (label query) to filter remote collection nodes on.")
3939
cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
4040
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
41+
cmd.Flags().StringP("output", "o", "", "specify the output file path for the preflight checks")
4142

4243
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
4344

cmd/preflight/cli/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func runPreflights(v *viper.Viper, arg string) error {
167167
if len(analyzeResults) == 0 {
168168
return errors.New("no data has been collected")
169169
}
170-
return showInteractiveResults(preflightSpecName, analyzeResults)
170+
return showInteractiveResults(preflightSpecName, v.GetString("output"), analyzeResults)
171171
}
172172

173173
return showStdoutResults(v.GetString("format"), preflightSpecName, analyzeResults)

cmd/troubleshoot/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
4242
cmd.Flags().Bool("collect-without-permissions", true, "always generate a support bundle, even if it some require additional permissions")
4343
cmd.Flags().String("since-time", "", "force pod logs collectors to return logs after a specific date (RFC3339)")
4444
cmd.Flags().String("since", "", "force pod logs collectors to return logs newer than a relative duration like 5s, 2m, or 3h.")
45+
cmd.Flags().StringP("output", "o", "", "specify the output file path for the support bundle")
4546

4647
// hidden in favor of the `insecure-skip-tls-verify` flag
4748
cmd.Flags().Bool("allow-insecure-connections", false, "when set, do not verify TLS certs when retrieving spec and reporting results")

cmd/troubleshoot/cli/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
181181
Namespace: v.GetString("namespace"),
182182
ProgressChan: progressChan,
183183
SinceTime: sinceTime,
184+
OutputPath: v.GetString("output"),
184185
Redact: v.GetBool("redact"),
185186
FromCLI: true,
186187
}

pkg/convert/output.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package convert
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
)
7+
8+
// ValidateOutputPath takes an output file path and returns it as an absolute path.
9+
// It returns an error if the absolute path cannot be determined or if the parent directory does not exist.
10+
func ValidateOutputPath(outputPath string) (string, error) {
11+
outputPath, err := filepath.Abs(outputPath)
12+
if err != nil {
13+
return "", err
14+
}
15+
if _, err := os.Stat(filepath.Dir(outputPath)); err != nil {
16+
return "", err
17+
}
18+
return outputPath, nil
19+
}

pkg/supportbundle/supportbundle.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
1616
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1717
"github.com/replicatedhq/troubleshoot/pkg/collect"
18+
"github.com/replicatedhq/troubleshoot/pkg/convert"
1819
"k8s.io/client-go/rest"
1920
)
2021

@@ -26,6 +27,7 @@ type SupportBundleCreateOpts struct {
2627
Namespace string
2728
ProgressChan chan interface{}
2829
SinceTime *time.Time
30+
OutputPath string
2931
Redact bool
3032
FromCLI bool
3133
}
@@ -57,9 +59,20 @@ func CollectSupportBundleFromSpec(spec *troubleshootv1beta2.SupportBundleSpec, a
5759
}
5860
defer os.RemoveAll(tmpDir)
5961

60-
basename := fmt.Sprintf("support-bundle-%s", time.Now().Format("2006-01-02T15_04_05"))
61-
if !opts.FromCLI {
62-
basename = filepath.Join(os.TempDir(), basename)
62+
basename := ""
63+
if opts.OutputPath != "" {
64+
// use override output path
65+
overridePath, err := convert.ValidateOutputPath(opts.OutputPath)
66+
if err != nil {
67+
return nil, errors.Wrap(err, "override output file path")
68+
}
69+
basename = strings.TrimSuffix(overridePath, ".tar.gz")
70+
} else {
71+
// use default output path
72+
basename = fmt.Sprintf("support-bundle-%s", time.Now().Format("2006-01-02T15_04_05"))
73+
if !opts.FromCLI {
74+
basename = filepath.Join(os.TempDir(), basename)
75+
}
6376
}
6477

6578
filename, err := findFileName(basename, "tar.gz")

0 commit comments

Comments
 (0)