44 "bytes"
55 "fmt"
66 "sort"
7+ "strconv"
78 "strings"
89
910 "github.com/getsops/sops/v3"
@@ -55,7 +56,9 @@ func (store *Store) LoadEncryptedFile(in []byte) (sops.Tree, error) {
5556 return sops.Tree {}, sops .MetadataNotFound
5657 }
5758
58- stores .DecodeNewLines (mdMap )
59+ if ! store .config .Quote {
60+ stores .DecodeNewLines (mdMap )
61+ }
5962 err = stores .DecodeNonStrings (mdMap )
6063 if err != nil {
6164 return sops.Tree {}, err
@@ -97,9 +100,19 @@ func (store *Store) LoadPlainFile(in []byte) (sops.TreeBranches, error) {
97100 if pos == - 1 {
98101 return nil , fmt .Errorf ("invalid dotenv input line: %s" , line )
99102 }
103+ var value string
104+ if store .config .Quote {
105+ var err error
106+ value , err = strconv .Unquote (string (line [pos + 1 :]))
107+ if err != nil {
108+ return nil , fmt .Errorf ("invalid quoted dotenv value for key %q: %w" , line [:pos ], err )
109+ }
110+ } else {
111+ value = strings .Replace (string (line [pos + 1 :]), "\\ n" , "\n " , - 1 )
112+ }
100113 branch = append (branch , sops.TreeItem {
101114 Key : string (line [:pos ]),
102- Value : strings . Replace ( string ( line [ pos + 1 :]), " \\ n" , " \n " , - 1 ) ,
115+ Value : value ,
103116 })
104117 }
105118 }
@@ -118,7 +131,9 @@ func (store *Store) EmitEncryptedFile(in sops.Tree) ([]byte, error) {
118131 }
119132
120133 stores .EncodeNonStrings (mdItems )
121- stores .EncodeNewLines (mdItems )
134+ if ! store .config .Quote {
135+ stores .EncodeNewLines (mdItems )
136+ }
122137
123138 var keys []string
124139 for k := range mdItems {
@@ -151,7 +166,10 @@ func (store *Store) EmitPlainFile(in sops.TreeBranches) ([]byte, error) {
151166 value , ok := item .Value .(string )
152167 if ! ok {
153168 value = stores .ValToString (item .Value )
154- } else {
169+ }
170+ if store .config .Quote {
171+ value = strconv .Quote (value )
172+ } else if ok {
155173 value = strings .ReplaceAll (value , "\n " , "\\ n" )
156174 }
157175
0 commit comments