Skip to content

Commit c61f344

Browse files
committed
Move payload function into validate
1 parent 51c3d16 commit c61f344

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

plugin/impl.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Settings struct {
2929
Headers cli.StringSlice
3030
URLs cli.StringSlice
3131
ValidCodes cli.StringSlice
32+
payload []byte
3233
}
3334

3435
// Validate handles the settings validation of the plugin.
@@ -44,21 +45,32 @@ func (p *Plugin) Validate() error {
4445
}
4546
}
4647

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+
4765
return nil
4866
}
4967

5068
// Execute provides the implementation of the plugin.
5169
func (p *Plugin) Execute() error {
52-
b, err := p.payload()
53-
54-
if err != nil {
55-
return err
56-
}
57-
5870
for i, raw := range p.settings.URLs.Value() {
5971
uri, _ := url.Parse(raw)
6072

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))
6274
if err != nil {
6375
return fmt.Errorf("failed to create http request: %w", err)
6476
}
@@ -94,7 +106,7 @@ func (p *Plugin) Execute() error {
94106
Method: req.Method,
95107
Header: req.Header,
96108
Status: resp.Status,
97-
Request: string(b),
109+
Request: string(p.settings.payload),
98110
Response: string(body),
99111
})
100112
if err != nil {
@@ -112,22 +124,3 @@ func (p *Plugin) Execute() error {
112124

113125
return nil
114126
}
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

Comments
 (0)