From 333ad153d07c1f082590a00a4b24efd7a19621b9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 18 Jun 2014 20:46:13 -0700 Subject: [PATCH] terraform: add a format byte to the diff file so we can iterate maybe --- terraform/diff.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/terraform/diff.go b/terraform/diff.go index 5d1b656e1..f184b3093 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -3,6 +3,7 @@ package terraform import ( "bytes" "encoding/gob" + "errors" "fmt" "io" "sort" @@ -10,6 +11,8 @@ import ( "sync" ) +const diffFormatByte byte = 1 + // Diff tracks the differences between resources to apply. type Diff struct { Resources map[string]*ResourceDiff @@ -21,6 +24,19 @@ type Diff struct { func ReadDiff(src io.Reader) (*Diff, error) { var result *Diff + var formatByte [1]byte + n, err := src.Read(formatByte[:]) + if err != nil { + return nil, err + } + if n != len(formatByte) { + return nil, errors.New("failed to read diff version byte") + } + + if formatByte[0] != diffFormatByte { + return nil, fmt.Errorf("unknown diff file version: %d", formatByte[0]) + } + dec := gob.NewDecoder(src) if err := dec.Decode(&result); err != nil { return nil, err @@ -31,6 +47,14 @@ func ReadDiff(src io.Reader) (*Diff, error) { // WriteDiff writes a diff somewhere in a binary format. func WriteDiff(d *Diff, dst io.Writer) error { + n, err := dst.Write([]byte{diffFormatByte}) + if err != nil { + return err + } + if n != 1 { + return errors.New("failed to write diff version byte") + } + return gob.NewEncoder(dst).Encode(d) }