-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
96 lines (67 loc) · 2.19 KB
/
runner.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
package concourse
import (
"context"
"encoding/json"
"fmt"
"io"
)
func CheckWithValidation[S any, V any, P any](ctx context.Context, resource Resource[S, V, P], stdin io.Reader, stdout, stderr io.Writer) error {
var request CheckRequest[S, V]
err := json.NewDecoder(stdin).Decode(&request)
if err != nil {
return fmt.Errorf("unable to decode request: %w", err)
}
err = request.Validate()
if err != nil {
return fmt.Errorf("validation of check request failed: %w", err)
}
response, err := resource.Check(ctx, request, stderr)
if err != nil {
return fmt.Errorf("check failed: %w", err)
}
err = response.Validate()
if err != nil {
return fmt.Errorf("validation of check response failed: %w", err)
}
return json.NewEncoder(stdout).Encode(response)
}
func GetWithValidation[S any, V any, P any](ctx context.Context, resource Resource[S, V, P], stdin io.Reader, stdout, stderr io.Writer, destination string) error {
var request GetRequest[S, V, P]
err := json.NewDecoder(stdin).Decode(&request)
if err != nil {
return fmt.Errorf("unable to decode request: %w", err)
}
err = request.Validate()
if err != nil {
return fmt.Errorf("validation of get request failed: %w", err)
}
response, err := resource.Get(ctx, request, stderr, destination)
if err != nil {
return fmt.Errorf("get failed: %w", err)
}
err = response.Validate()
if err != nil {
return fmt.Errorf("validation of get response failed: %w", err)
}
return json.NewEncoder(stdout).Encode(response)
}
func PutWithValidation[S any, V any, P any](ctx context.Context, resource Resource[S, V, P], stdin io.Reader, stdout, stderr io.Writer, source string) error {
var request PutRequest[S, P]
err := json.NewDecoder(stdin).Decode(&request)
if err != nil {
return fmt.Errorf("unable to decode request: %w", err)
}
err = request.Validate()
if err != nil {
return fmt.Errorf("validation of put request failed: %w", err)
}
response, err := resource.Put(ctx, request, stderr, source)
if err != nil {
return fmt.Errorf("put failed: %w", err)
}
err = response.Validate()
if err != nil {
return fmt.Errorf("validation of put response failed: %w", err)
}
return json.NewEncoder(stdout).Encode(response)
}