87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package owrt
|
|
|
|
import "fmt"
|
|
|
|
// UCINetworkInterface describes uci network inteface (uci show network)
|
|
type UCINetworkInterface struct {
|
|
uciClient *UCI
|
|
Name string
|
|
Proto string
|
|
IFName string
|
|
IPAddr string
|
|
Netmask string
|
|
DNS string
|
|
IFType string
|
|
Metric string
|
|
}
|
|
|
|
// NewUCINetworkInterface builds a new UCIWirelessConf instance
|
|
func NewUCINetworkInterface(uci *UCI) *UCINetworkInterface {
|
|
return &UCINetworkInterface{
|
|
uciClient: uci,
|
|
}
|
|
}
|
|
|
|
// Create add a new entry for wifi interface in UCI Configuration
|
|
func (ni *UCINetworkInterface) Create(uci *UCI) *Action {
|
|
conf := make(map[string]string)
|
|
|
|
conf["proto"] = ni.Proto
|
|
conf["ifname"] = ni.IFName
|
|
conf["ipaddr"] = ni.IPAddr
|
|
conf["netmask"] = ni.Netmask
|
|
conf["dns"] = ni.DNS
|
|
conf["type"] = ni.IFType
|
|
conf["metric"] = ni.Metric
|
|
|
|
result := uci.Set(fmt.Sprintf("network.%s", ni.Name), "interface")
|
|
if result.ReturnCode != 0 {
|
|
return result
|
|
}
|
|
for key, value := range conf {
|
|
result := uci.Set(fmt.Sprintf("network.%s.%s", ni.Name, key), value)
|
|
if result.ReturnCode != 0 {
|
|
return result
|
|
}
|
|
}
|
|
|
|
return &Action{
|
|
CommandResult: &CommandResult{
|
|
Stdout: "",
|
|
Stderr: "",
|
|
ReturnCode: 0,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Save commit and relaod configuration (writes it to files !)
|
|
func (ni *UCINetworkInterface) Save(uci *UCI) *Action {
|
|
commitRes := uci.Commit()
|
|
if commitRes.ReturnCode != 0 {
|
|
return commitRes
|
|
}
|
|
|
|
reload := uci.Reload()
|
|
return reload
|
|
}
|
|
|
|
// Delete remove wifi interface from UCI Configuration
|
|
func (ni *UCINetworkInterface) Delete(uci *UCI) *Action {
|
|
toDelete := fmt.Sprintf("network.%s", ni.Name)
|
|
del := uci.Delete(toDelete)
|
|
if del.ReturnCode != 0 {
|
|
return del
|
|
}
|
|
return uci.Commit()
|
|
}
|
|
|
|
// Update add a new entry for wifi interface in UCI Configuration
|
|
func (ni *UCINetworkInterface) Update(uci *UCI) *Action {
|
|
ni.Delete(uci)
|
|
create := ni.Create(uci)
|
|
if create.ReturnCode != 0 {
|
|
return create
|
|
}
|
|
return uci.Commit()
|
|
}
|