|
@@ -1,6 +1,7 @@
|
|
|
package netplan
|
|
package netplan
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "bytes"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"os"
|
|
"os"
|
|
|
"path/filepath"
|
|
"path/filepath"
|
|
@@ -81,7 +82,7 @@ func (s *Service) Write(path string, targetInterface string, input model.Interfa
|
|
|
delete(target, "gateway4")
|
|
delete(target, "gateway4")
|
|
|
delete(target, "routes")
|
|
delete(target, "routes")
|
|
|
delete(target, "nameservers")
|
|
delete(target, "nameservers")
|
|
|
- output, err := yaml.Marshal(&cfg)
|
|
|
|
|
|
|
+ output, err := marshalYAML(&cfg)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -103,13 +104,27 @@ func (s *Service) Write(path string, targetInterface string, input model.Interfa
|
|
|
delete(target, "nameservers")
|
|
delete(target, "nameservers")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- output, err := yaml.Marshal(&cfg)
|
|
|
|
|
|
|
+ output, err := marshalYAML(&cfg)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
return os.WriteFile(path, output, 0600)
|
|
return os.WriteFile(path, output, 0600)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func marshalYAML(value any) ([]byte, error) {
|
|
|
|
|
+ var output bytes.Buffer
|
|
|
|
|
+ encoder := yaml.NewEncoder(&output)
|
|
|
|
|
+ encoder.SetIndent(2)
|
|
|
|
|
+ if err := encoder.Encode(value); err != nil {
|
|
|
|
|
+ _ = encoder.Close()
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := encoder.Close(); err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ return output.Bytes(), nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func ensureMap(parent map[string]any, key string) map[string]any {
|
|
func ensureMap(parent map[string]any, key string) map[string]any {
|
|
|
if existing, ok := parent[key].(map[string]any); ok {
|
|
if existing, ok := parent[key].(map[string]any); ok {
|
|
|
return existing
|
|
return existing
|