-
Notifications
You must be signed in to change notification settings - Fork 67
/
resource_run_cmd.go
136 lines (113 loc) 路 3.86 KB
/
resource_run_cmd.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package cmd
import (
"context"
"fmt"
"strings"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/runner"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/spf13/cobra"
)
var (
runParams = &runParameters{}
runCmd *cobra.Command
)
func init() {
runCmd = &cobra.Command{
GroupID: cmdGroupResources.ID,
Use: "run " + runnableResourceList(),
Short: "run resources",
Long: "run resources",
PreRun: setupCommand(),
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) {
resourceType := resourceParams.ResourceName
ctx := context.Background()
r, err := runnerRegistry.Get(resourceType)
if err != nil {
return "", fmt.Errorf("resource type '%s' cannot be run", resourceType)
}
orchestrator := runner.Orchestrator(
cliLogger,
utils.GetAPIClient(cliConfig),
variableSetClient,
)
if runParams.EnvID != "" {
runParams.VarsID = runParams.EnvID
}
runParams := runner.RunOptions{
ID: runParams.ID,
DefinitionFile: runParams.DefinitionFile,
VarsID: runParams.VarsID,
SkipResultWait: runParams.SkipResultWait,
JUnitOuptutFile: runParams.JUnitOuptutFile,
RequiredGates: runParams.RequriedGates,
}
exitCode, err := orchestrator.Run(ctx, r, runParams, output)
if err != nil {
return "", err
}
ExitCLI(exitCode)
// ExitCLI will exit the process, so this return is just to satisfy the compiler
return "", nil
}, runParams),
PostRun: teardownCommand,
}
runCmd.Flags().StringVarP(&runParams.DefinitionFile, "file", "f", "", "path to the definition file")
runCmd.Flags().StringVar(&runParams.ID, "id", "", "id of the resource to run")
runCmd.Flags().StringVarP(&runParams.VarsID, "vars", "", "", "variable set file or ID to be used")
runCmd.Flags().BoolVarP(&runParams.SkipResultWait, "skip-result-wait", "W", false, "do not wait for results. exit immediately after test run started")
runCmd.Flags().StringVarP(&runParams.JUnitOuptutFile, "junit", "j", "", "file path to save test results in junit format")
runCmd.Flags().StringSliceVar(&runParams.RequriedGates, "required-gates", []string{}, "override default required gate. "+validRequiredGatesMsg())
//deprecated
runCmd.Flags().StringVarP(&runParams.EnvID, "env", "e", "", "environment file or ID to be used")
runCmd.Flags().MarkDeprecated("env", "use --vars instead")
runCmd.Flags().MarkShorthandDeprecated("e", "use --vars instead")
rootCmd.AddCommand(runCmd)
}
func validRequiredGatesMsg() string {
opts := make([]string, 0, len(openapi.AllowedSupportedGatesEnumValues))
for _, v := range openapi.AllowedSupportedGatesEnumValues {
opts = append(opts, string(v))
}
return "valid options: " + strings.Join(opts, ", ")
}
type runParameters struct {
ID string
DefinitionFile string
VarsID string
EnvID string
SkipResultWait bool
JUnitOuptutFile string
RequriedGates []string
}
func (p runParameters) Validate(cmd *cobra.Command, args []string) []error {
errs := []error{}
if p.DefinitionFile == "" && p.ID == "" {
errs = append(errs, paramError{
Parameter: "resource",
Message: "you must specify a definition file or resource ID",
})
}
if p.DefinitionFile != "" && p.ID != "" {
errs = append(errs, paramError{
Parameter: "resource",
Message: "you cannot specify both a definition file and resource ID",
})
}
if p.JUnitOuptutFile != "" && p.SkipResultWait {
errs = append(errs, paramError{
Parameter: "junit",
Message: "--junit option is incompatible with --skip-result-wait option",
})
}
for _, rg := range p.RequriedGates {
_, err := openapi.NewSupportedGatesFromValue(rg)
if err != nil {
errs = append(errs, paramError{
Parameter: "required-gates",
Message: fmt.Sprintf("invalid option '%s'. "+validRequiredGatesMsg(), rg),
})
}
}
return errs
}