-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_report.go
83 lines (75 loc) · 2.27 KB
/
step_report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package report
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// StepReportOptions contains the command line flags and other helper objects
type StepReportOptions struct {
step.StepOptions
OutputDir string
}
// NewCmdStepReport Creates a new Command object
func NewCmdStepReport(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepReportOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "report",
Short: "report [kind]",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.AddCommand(NewCmdStepReportChart(commonOpts))
cmd.AddCommand(NewCmdStepReportImageVersion(commonOpts))
cmd.AddCommand(NewCmdStepReportJUnit(commonOpts))
cmd.AddCommand(NewCmdStepReportVersion(commonOpts))
return cmd
}
// AddReportFlags adds common report flags
func (o *StepReportOptions) AddReportFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.OutputDir, "out-dir", "o", "", "The directory to store the resulting reports in")
}
// Run implements this command
func (o *StepReportOptions) Run() error {
return o.Cmd.Help()
}
// OutputReport outputs the report to the terminal or a file
func (o *StepReportOptions) OutputReport(report interface{}, fileName string, outputDir string) error {
data, err := yaml.Marshal(report)
if err != nil {
return errors.Wrap(err, "failed to marshal report to YAML")
}
if fileName == "" {
log.Logger().Infof(string(data))
return nil
}
if outputDir == "" {
outputDir = "."
}
err = os.MkdirAll(outputDir, util.DefaultWritePermissions)
if err != nil {
return errors.Wrap(err, "failed to create directories")
}
yamlFile := filepath.Join(outputDir, fileName)
err = ioutil.WriteFile(yamlFile, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to save report file %s", yamlFile)
}
log.Logger().Infof("generated report at %s", util.ColorInfo(yamlFile))
return nil
}