Skip to content

Commit

Permalink
fix(builder): is not possible to use ioutil.ReadAll more than once wi…
Browse files Browse the repository at this point in the history
…th the same reader.
  • Loading branch information
aledbf committed Mar 23, 2015
1 parent dafca34 commit 41053f5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
5 changes: 3 additions & 2 deletions builder/bin/get-app-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ func main() {

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

config, err := builder.ParseConfig(res)
config, err := builder.ParseConfig(body)
if err != nil {
fmt.Println("failed parsing config from controller")
fmt.Printf("%v\n", err)
os.Exit(1)
}
toString, err := json.Marshal(config)
Expand Down
12 changes: 2 additions & 10 deletions builder/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package builder
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

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

// ParseConfig takes a response body from the controller and returns a Config object.
func ParseConfig(res *http.Response) (*Config, error) {
body, err := ioutil.ReadAll(res.Body)

if err != nil {
return nil, err
}

func ParseConfig(body []byte) (*Config, error) {
var config Config
err = json.Unmarshal(body, &config)
err := json.Unmarshal(body, &config)
return &config, err
}

Expand Down
10 changes: 3 additions & 7 deletions builder/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package builder
import (
"bytes"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -58,20 +57,17 @@ worker: while true; do echo hello; sleep 1; done`),

func TestParseConfigGood(t *testing.T) {
// mock the controller response
resp := &http.Response{
Body: &ClosingBuffer{bytes.NewBufferString(`{"owner": "test",
resp := bytes.NewBufferString(`{"owner": "test",
"app": "example-go",
"values": {"FOO": "bar", "CAR": 1234},
"memory": {},
"cpu": {},
"tags": {},
"created": "2014-01-01T00:00:00UTC",
"updated": "2014-01-01T00:00:00UTC",
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"}`),
},
}
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"}`)

config, err := ParseConfig(resp)
config, err := ParseConfig(resp.Bytes())

if err != nil {
t.Error(err)
Expand Down

0 comments on commit 41053f5

Please sign in to comment.