62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
const (
|
|
defNetConfigFile = "/etc/netplan/01-netcfg.yaml"
|
|
// defNetConfigFile = "01-netcfg.yaml"
|
|
)
|
|
|
|
type NetConfig struct {
|
|
Network struct {
|
|
Version int `yaml:"version"`
|
|
Renderer string `yaml:"renderer"`
|
|
Ethernets struct {
|
|
Eth1 struct {
|
|
Dhcp4 string `yaml:"dhcp4"`
|
|
Addresses []string `yaml:"addresses"`
|
|
Routes []struct {
|
|
To string `yaml:"to"`
|
|
Via string `yaml:"via"`
|
|
} `yaml:"routes"`
|
|
Nameservers struct {
|
|
Addresses []string `yaml:"addresses"`
|
|
} `yaml:"nameservers"`
|
|
} `yaml:"eth1"`
|
|
} `yaml:"ethernets"`
|
|
} `yaml:"network"`
|
|
}
|
|
|
|
func (nc *NetConfig) Read() (err error) {
|
|
data, err := os.ReadFile(defNetConfigFile)
|
|
if err != nil {
|
|
err = fmt.Errorf("error reading netconfig file: %s", err)
|
|
return
|
|
}
|
|
|
|
if err = yaml.Unmarshal(data, nc); err != nil {
|
|
err = fmt.Errorf("error reading netconfig file: %s", err)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *NetConfig) Write() (err error) {
|
|
data, err := yaml.Marshal(c)
|
|
if err != nil {
|
|
err = fmt.Errorf("failed to marshal netconfig file: %s", err)
|
|
return
|
|
}
|
|
|
|
if err = os.WriteFile(defNetConfigFile, data, os.FileMode(0644)); err != nil {
|
|
err = fmt.Errorf("error writing netconfig file: %s", err)
|
|
return
|
|
}
|
|
return
|
|
}
|