forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
53 lines (44 loc) · 944 Bytes
/
rpc.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
package transform
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/drone/drone/yaml"
)
func convertTransform(c *yaml.Config, url string) error {
var buf bytes.Buffer
// encode yaml in json format
if err := json.NewEncoder(&buf).Encode(c); err != nil {
return err
}
resp, err := http.Post(url, "application/json", &buf)
if err != nil {
return err
}
defer resp.Body.Close()
// decode the updated yaml from the body
if resp.StatusCode == 200 {
err = json.NewDecoder(resp.Body).Decode(c)
return err
}
// handle error response
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf(string(out))
}
// RemoteTransform makes remote transform requests.
func RemoteTransform(c *yaml.Config, url []string) error {
if len(url) == 0 {
return nil
}
for _, u := range url {
if err := convertTransform(c, u); err != nil {
return err
}
}
return nil
}