@@ -185,35 +185,45 @@ func detectSubnet(ipAddrOutput string) (string, error) {
185
185
func editLXDBridgeFile (input string , subnet string ) string {
186
186
buffer := bytes.Buffer {}
187
187
188
+ newValues := map [string ]string {
189
+ "USE_LXD_BRIDGE" : "true" ,
190
+ "EXISTING_BRIDGE" : "" ,
191
+ "LXD_BRIDGE" : "lxdbr0" ,
192
+ "LXD_IPV4_ADDR" : fmt .Sprintf ("10.0.%s.1" , subnet ),
193
+ "LXD_IPV4_NETMASK" : "255.255.255.0" ,
194
+ "LXD_IPV4_NETWORK" : fmt .Sprintf ("10.0.%s.1/24" , subnet ),
195
+ "LXD_IPV4_DHCP_RANGE" : fmt .Sprintf ("10.0.%s.2,10.0.%s.254" , subnet , subnet ),
196
+ "LXD_IPV4_DHCP_MAX" : "253" ,
197
+ "LXD_IPV4_NAT" : "true" ,
198
+ "LXD_IPV6_PROXY" : "false" ,
199
+ }
200
+ found := map [string ]bool {}
201
+
188
202
for _ , line := range strings .Split (input , "\n " ) {
189
203
out := line
190
204
191
- if strings .HasPrefix (line , "USE_LXD_BRIDGE=" ) {
192
- out = `USE_LXD_BRIDGE="true"`
193
- } else if strings .HasPrefix (line , "EXISTING_BRIDGE=" ) {
194
- out = `EXISTING_BRIDGE=""`
195
- } else if strings .HasPrefix (line , "LXD_BRIDGE=" ) {
196
- out = `LXD_BRIDGE="lxdbr0"`
197
- } else if strings .HasPrefix (line , "LXD_IPV4_ADDR=" ) {
198
- out = fmt .Sprintf (`LXD_IPV4_ADDR="10.0.%s.1"` , subnet )
199
- } else if strings .HasPrefix (line , "LXD_IPV4_NETMASK=" ) {
200
- out = `LXD_IPV4_NETMASK="255.255.255.0"`
201
- } else if strings .HasPrefix (line , "LXD_IPV4_NETWORK=" ) {
202
- out = fmt .Sprintf (`LXD_IPV4_NETWORK="10.0.%s.1/24"` , subnet )
203
- } else if strings .HasPrefix (line , "LXD_IPV4_DHCP_RANGE=" ) {
204
- out = fmt .Sprintf (`LXD_IPV4_DHCP_RANGE="10.0.%s.2,10.0.%s.254"` , subnet , subnet )
205
- } else if strings .HasPrefix (line , "LXD_IPV4_DHCP_MAX=" ) {
206
- out = `LXD_IPV4_DHCP_MAX="253"`
207
- } else if strings .HasPrefix (line , "LXD_IPV4_NAT=" ) {
208
- out = `LXD_IPV4_NAT="true"`
209
- } else if strings .HasPrefix (line , "LXD_IPV6_PROXY=" ) {
210
- out = `LXD_IPV6_PROXY="false"`
205
+ for prefix , value := range newValues {
206
+ if strings .HasPrefix (line , prefix + "=" ) {
207
+ out = fmt .Sprintf (`%s="%s"` , prefix , value )
208
+ found [prefix ] = true
209
+ break
210
+ }
211
211
}
212
212
213
213
buffer .WriteString (out )
214
214
buffer .WriteString ("\n " )
215
215
}
216
216
217
+ for prefix , value := range newValues {
218
+ if ! found [prefix ] {
219
+ buffer .WriteString (prefix )
220
+ buffer .WriteString ("=" )
221
+ buffer .WriteString (value )
222
+ buffer .WriteString ("\n " )
223
+ found [prefix ] = true // not necessary but keeps "found" logically consistent
224
+ }
225
+ }
226
+
217
227
return buffer .String ()
218
228
}
219
229
0 commit comments