-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.go
121 lines (104 loc) · 3.74 KB
/
run.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
package cmd
import (
"encoding/json"
"github.com/ghodss/yaml"
"io/ioutil"
"os"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/thomaspoignant/api-scenario/pkg/context"
"github.com/thomaspoignant/api-scenario/pkg/controller"
"github.com/thomaspoignant/api-scenario/pkg/model"
"github.com/thomaspoignant/api-scenario/pkg/util"
)
var headers []string
var variables []string
var inputFile string
var token string
var outputFile string
var outputFormat string
// init setup the flags used by the run command.
func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().StringVarP(&inputFile, "scenario", "s", "", "Input file for the scenario.")
runCmd.Flags().StringArrayVarP(&headers, "header", "H", []string{}, "Header you want to override (format should be \"header_name:value\").")
runCmd.Flags().StringArrayVarP(&variables, "variable", "V", []string{}, "Value for a variable used in your scenario (format should be \"variable_name:value\").")
runCmd.Flags().StringVarP(&token, "authorization-token", "t", "", "Authorization token send in the Authorization headers.")
runCmd.Flags().StringVarP(&outputFile, "output-file", "f", "", "Output file where to save the result (use --output-format to specify if you want JSON or YAML output).")
runCmd.Flags().StringVar(&outputFormat, "output-format", "JSON", "Format of the output file, available values are JSON and YAML (ignored if --output-file is not set).")
if err := runCmd.MarkFlagRequired("scenario"); err != nil {
panic(err)
}
}
var runCmd = &cobra.Command{
Use: "run",
Short: "Execute your scenario",
Long: `Execute your scenario`,
Run: func(cmd *cobra.Command, args []string) {
// add variable to context
addVariableToContext(variables)
// format headers and add it to the config
viper.Set("headers", formatHeadersForConfig(headers, token))
// Parse the input file
scenario, err := model.InitScenarioFromFile(inputFile)
util.ExitIfErr(err)
// run the scenario
ctrl, err := controller.InitializeScenarioController()
util.ExitIfErr(err)
res := ctrl.Run(scenario)
saveResultInFile(res)
if !res.IsSuccess() {
os.Exit(1)
}
},
}
// addVariableToContext is adding a variable to the context to replace wildcard strings
func addVariableToContext(variables []string) {
const separator = ":"
for _, variable := range variables {
splitStr := strings.SplitN(variable, separator, 2)
if len(splitStr) <= 1 {
logrus.Errorf("Wrong format for parameter %s, it should be \"Key:value\", this parameter is ignored.", variable)
continue
}
context.GetContext().Add(strings.TrimSpace(splitStr[0]), strings.TrimSpace(splitStr[1]))
}
}
// formatHeadersForConfig is formatting headers to put them in the config
func formatHeadersForConfig(headers []string, token string) map[string]string {
const separator = ":"
var res = map[string]string{}
for _, header := range headers {
splitStr := strings.SplitN(header, separator, 2)
if len(splitStr) <= 1 {
logrus.Errorf("Wrong format for parameter %s, it should be \"Key:value\", this parameter is ignored.", header)
continue
}
res[strings.TrimSpace(splitStr[0])] = strings.TrimSpace(splitStr[1])
}
// Authentication token
if len(token) > 0 {
res["Authorization"] = util.AddBearerPrefix(token)
}
return res
}
// save result in a file
func saveResultInFile(result model.ScenarioResult) {
if len(outputFile) == 0 {
return
}
var s []byte
var err error
// Marshal in the choosing format
if strings.ToUpper(outputFormat) == "YAML" || strings.ToUpper(outputFormat) == "YML" {
s, err = yaml.Marshal(result)
} else {
s, err = json.Marshal(result)
}
util.ExitIfErr(err)
err = ioutil.WriteFile(outputFile, s, 0644)
util.ExitIfErr(err)
logrus.Infof("Output file %s saved", outputFile)
}