Skip to content

Commit 1d94ac6

Browse files
authored
Initialize default reports directory only when needed (#1294)
`report` subcommand had some code to initialize its defaults for the output directory. This code was executed during the initialization of the subcommand, that is allways executed. When running any command out of a package directory, this initialization fails, as it depends on the package directory, and it produces an error message. Move this initialization to the code that gets the parameter in the `report` subcommand, so it is only executed there, and only if the user has not provided a value for the flag.
1 parent d6d0891 commit 1d94ac6

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

cmd/report.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import (
1818
"github.com/elastic/elastic-package/internal/reportgenerator/outputs"
1919
)
2020

21-
const reportLongDescription = `Use this command to generate various reports relative to the packages. Currently, the following types of reports are available:
21+
const (
22+
benchmarksFolder = "benchmark-report"
23+
reportLongDescription = `Use this command to generate various reports relative to the packages. Currently, the following types of reports are available:
2224
2325
#### Benchmark report for Github
2426
@@ -27,6 +29,7 @@ The report will show performance differences between both runs.
2729
2830
It is formatted as a Markdown Github comment to use as part of the CI results.
2931
`
32+
)
3033

3134
func setupReportsCommand() *cobraext.Command {
3235
var reportTypeCmdActions []cobraext.CommandAction
@@ -43,17 +46,13 @@ func setupReportsCommand() *cobraext.Command {
4346
}
4447

4548
return cobraext.ComposeCommandActions(cmd, args, reportTypeCmdActions...)
46-
}}
47-
48-
dest, err := resultsDir()
49-
if err != nil {
50-
cmd.PrintErrf("could not determine benchmark reports folder: %v", err)
49+
},
5150
}
5251

5352
cmd.PersistentFlags().BoolP(cobraext.FailOnMissingFlagName, "m", false, cobraext.FailOnMissingFlagDescription)
5453
cmd.PersistentFlags().BoolP(cobraext.ReportFullFlagName, "", false, cobraext.ReportFullFlagDescription)
5554
cmd.PersistentFlags().StringP(cobraext.ReportOutputFlagName, "", string(outputs.OutputFile), cobraext.ReportOutputFlagDescription)
56-
cmd.PersistentFlags().StringP(cobraext.ReportOutputPathFlagName, "", dest, cobraext.ReportOutputPathFlagDescription)
55+
cmd.PersistentFlags().StringP(cobraext.ReportOutputPathFlagName, "", "", fmt.Sprintf(cobraext.ReportOutputPathFlagDescription, benchmarksFolder))
5756

5857
// add benchmark report creation subcommand
5958
cmd.AddCommand(getBenchReportCommand())
@@ -83,6 +82,13 @@ func getBenchReportCommand() *cobra.Command {
8382
if err != nil {
8483
return cobraext.FlagParsingError(err, cobraext.ReportOutputPathFlagName)
8584
}
85+
if reportOutputPath == "" {
86+
dest, err := resultsDir()
87+
if err != nil {
88+
return fmt.Errorf("could not determine benchmark reports folder: %w", err)
89+
}
90+
reportOutputPath = dest
91+
}
8692

8793
isFull, err := cmd.Flags().GetBool(cobraext.ReportFullFlagName)
8894
if err != nil {
@@ -158,6 +164,5 @@ func resultsDir() (string, error) {
158164
if err != nil {
159165
return "", fmt.Errorf("locating build directory failed: %w", err)
160166
}
161-
const folder = "benchmark-report"
162-
return filepath.Join(buildDir, folder), nil
167+
return filepath.Join(buildDir, benchmarksFolder), nil
163168
}

internal/cobraext/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const (
130130
ReportOutputFlagDescription = "output type for test report, eg: stdout, file"
131131

132132
ReportOutputPathFlagName = "report-output-path"
133-
ReportOutputPathFlagDescription = "output path for test report"
133+
ReportOutputPathFlagDescription = "output path for test report (defaults to %q in build directory)"
134134

135135
ShowAllFlagName = "all"
136136
ShowAllFlagDescription = "show all deployed package revisions"

0 commit comments

Comments
 (0)