@@ -29,6 +29,7 @@ type Settings struct {
29
29
Headers cli.StringSlice
30
30
URLs cli.StringSlice
31
31
ValidCodes cli.StringSlice
32
+ payload []byte
32
33
}
33
34
34
35
// Validate handles the settings validation of the plugin.
@@ -44,21 +45,32 @@ func (p *Plugin) Validate() error {
44
45
}
45
46
}
46
47
48
+ if p .settings .Template == "" {
49
+ res , err := json .Marshal (& p .pipeline )
50
+
51
+ if err != nil {
52
+ return fmt .Errorf ("failed to generate json response: %w" , err )
53
+ }
54
+
55
+ p .settings .payload = res
56
+ } else {
57
+ res , err := template .RenderTrim (p .settings .Template , p )
58
+ if err != nil {
59
+ return fmt .Errorf ("failed to parse response template: %w" , err )
60
+ }
61
+
62
+ p .settings .payload = []byte (res )
63
+ }
64
+
47
65
return nil
48
66
}
49
67
50
68
// Execute provides the implementation of the plugin.
51
69
func (p * Plugin ) Execute () error {
52
- b , err := p .payload ()
53
-
54
- if err != nil {
55
- return err
56
- }
57
-
58
70
for i , raw := range p .settings .URLs .Value () {
59
71
uri , _ := url .Parse (raw )
60
72
61
- req , err := http .NewRequest (p .settings .Method , uri .String (), bytes .NewReader (b ))
73
+ req , err := http .NewRequest (p .settings .Method , uri .String (), bytes .NewReader (p . settings . payload ))
62
74
if err != nil {
63
75
return fmt .Errorf ("failed to create http request: %w" , err )
64
76
}
@@ -94,7 +106,7 @@ func (p *Plugin) Execute() error {
94
106
Method : req .Method ,
95
107
Header : req .Header ,
96
108
Status : resp .Status ,
97
- Request : string (b ),
109
+ Request : string (p . settings . payload ),
98
110
Response : string (body ),
99
111
})
100
112
if err != nil {
@@ -112,22 +124,3 @@ func (p *Plugin) Execute() error {
112
124
113
125
return nil
114
126
}
115
-
116
- func (p * Plugin ) payload () ([]byte , error ) {
117
- if p .settings .Template == "" {
118
- res , err := json .Marshal (& p .pipeline )
119
-
120
- if err != nil {
121
- return []byte {}, fmt .Errorf ("failed to generate json response: %w" , err )
122
- }
123
-
124
- return res , nil
125
- }
126
-
127
- res , err := template .RenderTrim (p .settings .Template , p )
128
- if err != nil {
129
- return []byte {}, fmt .Errorf ("failed to parse response template: %w" , err )
130
- }
131
-
132
- return []byte (res ), nil
133
- }
0 commit comments