state: do not overwrite values.yaml if nothing has changed

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-05-21 16:27:19 -07:00
parent 155c90e3e0
commit 322ca54e58

View File

@ -1,6 +1,7 @@
package state package state
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -167,6 +168,17 @@ func (w *Workspace) Save(ctx context.Context, st *State) error {
manifestPath := path.Join(st.Path, manifestFile) manifestPath := path.Join(st.Path, manifestFile)
currentEncrypted, err := os.ReadFile(manifestPath)
if err != nil {
return err
}
currentPlain, err := keychain.Decrypt(ctx, currentEncrypted)
if err != nil {
return fmt.Errorf("unable to decrypt state: %w", err)
}
// Only update the encrypted file if there were changes
if bytes.Compare(data, currentPlain) != 0 {
encrypted, err := keychain.Reencrypt(ctx, manifestPath, data) encrypted, err := keychain.Reencrypt(ctx, manifestPath, data)
if err != nil { if err != nil {
return err return err
@ -174,6 +186,7 @@ func (w *Workspace) Save(ctx context.Context, st *State) error {
if err := os.WriteFile(manifestPath, encrypted, 0600); err != nil { if err := os.WriteFile(manifestPath, encrypted, 0600); err != nil {
return err return err
} }
}
if st.Computed != "" { if st.Computed != "" {
state := path.Join(st.Path, stateDir) state := path.Join(st.Path, stateDir)