@@ -17,8 +17,11 @@ package configuration
1717
1818import (
1919 "io/ioutil"
20+ "strings"
2021
22+ "github.com/haproxytech/dataplaneapi/log"
2123 "github.com/hashicorp/hcl"
24+ "github.com/jinzhu/copier"
2225 "github.com/rodaine/hclencoder"
2326)
2427
@@ -57,7 +60,48 @@ func (s *StorageHCL) Set(cfg *StorageDataplaneAPIConfiguration) {
5760}
5861
5962func (s * StorageHCL ) SaveAs (filename string ) error {
60- hcl , err := hclencoder .Encode (s .cfg )
63+ var err error
64+ var hcl []byte
65+ var localCopy StorageDataplaneAPIConfiguration
66+ err = copier .Copy (& localCopy , & s .cfg )
67+ if err != nil {
68+ return err
69+ }
70+ // check if we have cluster log targets in config file
71+ if s .cfg .Cluster != nil && len (s .cfg .Cluster .ClusterLogTargets ) > 0 {
72+ // since this can contain " character, escape it
73+ for index , value := range localCopy .Cluster .ClusterLogTargets {
74+ localCopy .Cluster .ClusterLogTargets [index ].LogFormat = strings .Replace (value .LogFormat , `"` , `\"` , - 1 )
75+ }
76+ }
77+ // check if we have cluster log targets in config file
78+ if localCopy .Cluster != nil && len (localCopy .Cluster .ClusterLogTargets ) > 0 {
79+ // since this can contain " character, escape it
80+ for index , value := range localCopy .Cluster .ClusterLogTargets {
81+ localCopy .Cluster .ClusterLogTargets [index ].LogFormat = strings .Replace (value .LogFormat , `"` , `\"` , - 1 )
82+ }
83+ }
84+ if localCopy .LogTargets != nil && len (* localCopy .LogTargets ) > 0 {
85+ var logTargets []log.Target
86+ for _ , value := range * localCopy .LogTargets {
87+ value .LogFormat = strings .Replace (value .LogFormat , `"` , `\"` , - 1 )
88+ value .ACLFormat = strings .Replace (value .ACLFormat , `"` , `\"` , - 1 )
89+ logTargets = append (logTargets , value )
90+ }
91+ localCopy .LogTargets = (* log .Targets )(& logTargets )
92+ }
93+ if localCopy .Log != nil {
94+ if localCopy .Log .ACLFormat != nil {
95+ aclF := strings .Replace (* localCopy .Log .ACLFormat , `"` , `\"` , - 1 )
96+ localCopy .Log .ACLFormat = & aclF
97+ }
98+ if localCopy .Log .LogFormat != nil {
99+ logF := strings .Replace (* localCopy .Log .LogFormat , `"` , `\"` , - 1 )
100+ localCopy .Log .LogFormat = & logF
101+ }
102+ }
103+
104+ hcl , err = hclencoder .Encode (localCopy )
61105 if err != nil {
62106 return err
63107 }
0 commit comments