forked from hashicorp/vault-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview.go
118 lines (96 loc) · 2.52 KB
/
review.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package command
import (
"fmt"
"os"
"strings"
"github.com/hashicorp/vault-benchmark/benchmarktests"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
var (
_ cli.Command = (*ReviewCommand)(nil)
_ cli.CommandAutocomplete = (*ReviewCommand)(nil)
)
type ReviewCommand struct {
*BaseCommand
flagReviewResultsFile string
flagReportMode string
}
func (r *ReviewCommand) Synopsis() string {
return "Review previous test results"
}
func (r *ReviewCommand) Help() string {
helpText := `
Usage: vault-benchmark review [options]
This command prints previous JSON test results for review.
$ vault-benchmark review -results_file=/etc/vault-benchmark/results.json
For a full list of examples, please see the documentation.
` + r.Flags().Help()
return strings.TrimSpace(helpText)
}
func (r *ReviewCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (r *ReviewCommand) AutocompleteFlags() complete.Flags {
return r.Flags().Completions()
}
func (r *ReviewCommand) Flags() *FlagSets {
set := r.flagSet()
f := set.NewFlagSet("Command Options")
f.StringVar(&StringVar{
Name: "results_file",
Target: &r.flagReviewResultsFile,
Completion: complete.PredictOr(
complete.PredictFiles("*.json"),
),
Usage: "Path to a vault-benchmark test configuration file.",
})
f.StringVar(&StringVar{
Name: "report_mode",
Target: &r.flagReportMode,
Default: "terse",
Usage: "Reporting Mode. Options are: terse, verbose, json.",
})
return set
}
func (r *ReviewCommand) Run(args []string) int {
f := r.Flags()
if err := f.Parse(args); err != nil {
r.UI.Error(err.Error())
return 1
}
// File Validity checking
fStat, err := os.Stat(r.flagReviewResultsFile)
if err != nil {
r.UI.Error(fmt.Sprintf("error opening file: %v", err))
return 1
}
if fStat.IsDir() {
r.UI.Error("location is a directory, not a file")
return 1
}
fReader, err := os.Open(r.flagReviewResultsFile)
if err != nil {
r.UI.Error(fmt.Sprintf("error opening file: %v", err))
return 1
}
rpt, err := benchmarktests.FromReader(fReader)
if err != nil {
r.UI.Error(fmt.Sprintf("error reading report: %v", err))
}
switch r.flagReportMode {
case "json":
err = fmt.Errorf("asked to report JSON on JSON input")
case "terse":
err = rpt.ReportTerse(os.Stdout)
case "verbose":
err = rpt.ReportVerbose(os.Stdout)
}
if err != nil {
r.UI.Error(fmt.Sprintf("error writing report: %v", err))
return 1
}
return 0
}