@@ -3,6 +3,7 @@ package dotenv //import "github.com/getsops/sops/v3/stores/dotenv"
33import (
44 "bytes"
55 "fmt"
6+ "slices"
67 "sort"
78 "strings"
89
@@ -76,7 +77,32 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
7677 var branches sops.TreeBranches
7778 var branch sops.TreeBranch
7879
79- for _ , line := range bytes .Split (in , []byte ("\n " )) {
80+ var lines [][]byte
81+ var inQuotes = false
82+ var currentQuoteChar byte = '"'
83+ var lastSplit = - 1
84+
85+ for pos , char := range in {
86+ switch char {
87+ case '"' , '\'' , '`' :
88+ if inQuotes {
89+ if currentQuoteChar == char {
90+ inQuotes = false
91+ }
92+ } else {
93+ inQuotes = true
94+ currentQuoteChar = char
95+ }
96+ case '\n' :
97+ if ! inQuotes {
98+ var line = in [lastSplit + 1 : pos ]
99+ lines = append (lines , line )
100+ lastSplit = pos
101+ }
102+ }
103+ }
104+
105+ for _ , line := range lines {
80106 if len (line ) == 0 {
81107 continue
82108 }
@@ -141,8 +167,15 @@ func (store *Store) EmitPlainFile(in sops.TreeBranches) ([]byte, error) {
141167 if comment , ok := item .Key .(sops.Comment ); ok {
142168 line = fmt .Sprintf ("#%s\n " , comment .Value )
143169 } else {
144- value := strings .Replace (item .Value .(string ), "\n " , "\\ n" , - 1 )
170+ stringValue := item .Value .(string )
171+ var value string
172+ if len (stringValue ) == 0 || ! slices .Contains ([]byte {'"' , '\'' , '`' }, stringValue [0 ]) {
173+ value = strings .Replace (stringValue , "\n " , "\\ n" , - 1 )
174+ } else {
175+ value = item .Value .(string )
176+ }
145177 line = fmt .Sprintf ("%s=%s\n " , item .Key , value )
178+
146179 }
147180 buffer .WriteString (line )
148181 }
0 commit comments