mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-11 08:32:19 -06:00
terraform: add a format byte to the diff file so we can iterate maybe
This commit is contained in:
parent
e5b82931ff
commit
333ad153d0
@ -3,6 +3,7 @@ package terraform
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
@ -10,6 +11,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const diffFormatByte byte = 1
|
||||||
|
|
||||||
// Diff tracks the differences between resources to apply.
|
// Diff tracks the differences between resources to apply.
|
||||||
type Diff struct {
|
type Diff struct {
|
||||||
Resources map[string]*ResourceDiff
|
Resources map[string]*ResourceDiff
|
||||||
@ -21,6 +24,19 @@ type Diff struct {
|
|||||||
func ReadDiff(src io.Reader) (*Diff, error) {
|
func ReadDiff(src io.Reader) (*Diff, error) {
|
||||||
var result *Diff
|
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)
|
dec := gob.NewDecoder(src)
|
||||||
if err := dec.Decode(&result); err != nil {
|
if err := dec.Decode(&result); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -31,6 +47,14 @@ func ReadDiff(src io.Reader) (*Diff, error) {
|
|||||||
|
|
||||||
// WriteDiff writes a diff somewhere in a binary format.
|
// WriteDiff writes a diff somewhere in a binary format.
|
||||||
func WriteDiff(d *Diff, dst io.Writer) error {
|
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)
|
return gob.NewEncoder(dst).Encode(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user