Skip to content

Commit 41053f5

Browse files
committed
fix(builder): is not possible to use ioutil.ReadAll more than once with the same reader.
1 parent dafca34 commit 41053f5

3 files changed

Lines changed: 8 additions & 19 deletions

File tree

builder/bin/get-app-config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ func main() {
9494

9595
if err != nil || res.StatusCode != 200 {
9696
fmt.Println("failed retrieving config from controller")
97-
fmt.Println(body)
97+
fmt.Printf("%v\n", body)
9898
os.Exit(1)
9999
}
100100

101-
config, err := builder.ParseConfig(res)
101+
config, err := builder.ParseConfig(body)
102102
if err != nil {
103103
fmt.Println("failed parsing config from controller")
104+
fmt.Printf("%v\n", err)
104105
os.Exit(1)
105106
}
106107
toString, err := json.Marshal(config)

builder/utils.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package builder
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
7-
"net/http"
86

97
"gopkg.in/yaml.v2"
108
)
@@ -27,15 +25,9 @@ func YamlToJSON(bytes []byte) (string, error) {
2725
}
2826

2927
// ParseConfig takes a response body from the controller and returns a Config object.
30-
func ParseConfig(res *http.Response) (*Config, error) {
31-
body, err := ioutil.ReadAll(res.Body)
32-
33-
if err != nil {
34-
return nil, err
35-
}
36-
28+
func ParseConfig(body []byte) (*Config, error) {
3729
var config Config
38-
err = json.Unmarshal(body, &config)
30+
err := json.Unmarshal(body, &config)
3931
return &config, err
4032
}
4133

builder/utils_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package builder
33
import (
44
"bytes"
55
"encoding/json"
6-
"net/http"
76
"strings"
87
"testing"
98
"time"
@@ -58,20 +57,17 @@ worker: while true; do echo hello; sleep 1; done`),
5857

5958
func TestParseConfigGood(t *testing.T) {
6059
// mock the controller response
61-
resp := &http.Response{
62-
Body: &ClosingBuffer{bytes.NewBufferString(`{"owner": "test",
60+
resp := bytes.NewBufferString(`{"owner": "test",
6361
"app": "example-go",
6462
"values": {"FOO": "bar", "CAR": 1234},
6563
"memory": {},
6664
"cpu": {},
6765
"tags": {},
6866
"created": "2014-01-01T00:00:00UTC",
6967
"updated": "2014-01-01T00:00:00UTC",
70-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"}`),
71-
},
72-
}
68+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"}`)
7369

74-
config, err := ParseConfig(resp)
70+
config, err := ParseConfig(resp.Bytes())
7571

7672
if err != nil {
7773
t.Error(err)

0 commit comments

Comments
 (0)