@@ -3,6 +3,7 @@ package dotenv //import "github.com/getsops/sops/v3/stores/dotenv"
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
+ "slices"
6
7
"sort"
7
8
"strings"
8
9
@@ -76,7 +77,32 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
76
77
var branches sops.TreeBranches
77
78
var branch sops.TreeBranch
78
79
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 {
80
106
if len (line ) == 0 {
81
107
continue
82
108
}
@@ -141,8 +167,15 @@ func (store *Store) EmitPlainFile(in sops.TreeBranches) ([]byte, error) {
141
167
if comment , ok := item .Key .(sops.Comment ); ok {
142
168
line = fmt .Sprintf ("#%s\n " , comment .Value )
143
169
} 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
+ }
145
177
line = fmt .Sprintf ("%s=%s\n " , item .Key , value )
178
+
146
179
}
147
180
buffer .WriteString (line )
148
181
}
0 commit comments