mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-23 15:40:07 -06:00
terraform: read/write diff to binary format
This commit is contained in:
parent
965d403d3d
commit
e5b82931ff
@ -2,7 +2,9 @@ package terraform
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -14,6 +16,24 @@ type Diff struct {
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// ReadDiff reads a diff structure out of a reader in the format that
|
||||
// was written by WriteDiff.
|
||||
func ReadDiff(src io.Reader) (*Diff, error) {
|
||||
var result *Diff
|
||||
|
||||
dec := gob.NewDecoder(src)
|
||||
if err := dec.Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// WriteDiff writes a diff somewhere in a binary format.
|
||||
func WriteDiff(d *Diff, dst io.Writer) error {
|
||||
return gob.NewEncoder(dst).Encode(d)
|
||||
}
|
||||
|
||||
func (d *Diff) init() {
|
||||
d.once.Do(func() {
|
||||
if d.Resources == nil {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@ -61,6 +63,44 @@ func TestResourceDiff_RequiresNew_nil(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadWriteDiff(t *testing.T) {
|
||||
diff := &Diff{
|
||||
Resources: map[string]*ResourceDiff{
|
||||
"nodeA": &ResourceDiff{
|
||||
Attributes: map[string]*ResourceAttrDiff{
|
||||
"foo": &ResourceAttrDiff{
|
||||
Old: "foo",
|
||||
New: "bar",
|
||||
},
|
||||
"bar": &ResourceAttrDiff{
|
||||
Old: "foo",
|
||||
NewComputed: true,
|
||||
},
|
||||
"longfoo": &ResourceAttrDiff{
|
||||
Old: "foo",
|
||||
New: "bar",
|
||||
RequiresNew: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := WriteDiff(diff, buf); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual, err := ReadDiff(buf)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actual, diff) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
}
|
||||
|
||||
const diffStrBasic = `
|
||||
CREATE: nodeA
|
||||
bar: "foo" => "<computed>"
|
||||
|
Loading…
Reference in New Issue
Block a user