@@ -17,8 +17,11 @@ package configuration
17
17
18
18
import (
19
19
"io/ioutil"
20
+ "strings"
20
21
22
+ "github.com/haproxytech/dataplaneapi/log"
21
23
"github.com/hashicorp/hcl"
24
+ "github.com/jinzhu/copier"
22
25
"github.com/rodaine/hclencoder"
23
26
)
24
27
@@ -57,7 +60,48 @@ func (s *StorageHCL) Set(cfg *StorageDataplaneAPIConfiguration) {
57
60
}
58
61
59
62
func (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 )
61
105
if err != nil {
62
106
return err
63
107
}
0 commit comments