owrt/uci.go

177 lines
4.2 KiB
Go
Raw Normal View History

package owrt
import (
"fmt"
"time"
)
// Action is the result of an UCI action output and return code
type Action struct {
*CommandResult
Command string
}
// UCI "Object"
type UCI struct {
exec Executor
CustomFirewallFile string
Wireless *UCIWirelessConf
}
// NewUCI return an UCI instance to interact with UCI
func NewUCI() *UCI {
exec := &localExecutor{}
customFWFile := "/etc/"
wireless := &UCIWirelessConf{}
return &UCI{exec, customFWFile, wireless}
}
// NewUCIWithExecutor returns a UCI Instance an gives you the ability to provide
// a different command executor than the default one.
func NewUCIWithExecutor(exec Executor, customFWFile string) *UCI {
wireless := &UCIWirelessConf{}
return &UCI{exec, customFWFile, wireless}
}
// uciRun, private method to run the UCI command
func (u *UCI) uciRun(param ...string) *Action {
cmd := "/sbin/uci"
res := u.exec.Run(cmd, param...)
return &Action{res, fmt.Sprintf("%s %s", cmd, param)}
}
// Add add an entry to UCI configuration, specify the Module and the value
func (u *UCI) Add(module string, name string) *Action {
cmd := "uci add"
commandRes := u.exec.Run(cmd, module, name)
return &Action{commandRes, fmt.Sprintf("%s %s %s", cmd, module, name)}
}
// Delete delete an entry from UCI configuration specify the entry name
func (u *UCI) Delete(entry string) *Action {
return u.uciRun("delete", entry)
}
// Set set a value ton an UCI configuration entry
func (u *UCI) Set(entry string, value string) *Action {
return u.uciRun("set", fmt.Sprintf("%s=%s", entry, value))
}
// Commit the recent actions to UCI
func (u *UCI) Commit() *Action {
res := u.uciRun("commit")
return res
}
// Reload reload uci configuration
func (u *UCI) Reload() *Action {
cmdResult := u.exec.Run("reload_config")
2018-10-29 15:10:32 +01:00
time.Sleep(1 * time.Second)
return &Action{cmdResult, "reload_config"}
}
// Save commit and relaod configuration (writes it to files !)
func (u *UCI) Save() *Action {
commitRes := u.Commit()
if commitRes.ReturnCode != 0 {
return commitRes
}
reload := u.Reload()
return reload
}
// Show returns the output of uci show command
func (u *UCI) Show(target string) *Action {
cmdRes := u.uciRun("show", target)
return cmdRes
}
// AddWireless Create a new Wireless entry in UCI configuration
func (u *UCI) AddWireless() *Action {
res := u.uciRun("add", "wireless", "wifi-iface")
return res
}
// LoadWirelessConf scan UCI configuration and loads saved Wireless configuration
func (u *UCI) LoadWirelessConf() {
u.Wireless = NewUCIWirelessConf(u)
u.Wireless.Load()
}
// GetWifiIface returns the wifi Interface by Index
func (u *UCI) GetWifiIface(idx int) *UCIWirelessInterface {
ifaces := u.Wireless.Interfaces
if len(ifaces) < idx {
return nil
}
return ifaces[idx]
}
// GetWifiIfaceByName returns the wifi Interface by Index
func (u *UCI) GetWifiIfaceByName(name string) *UCIWirelessInterface {
ifaces := u.Wireless.Interfaces
if len(ifaces) <= 0 {
return nil
}
for _, ifa := range ifaces {
if ifa.Name == name {
return ifa
}
}
return nil
}
// GetWifiIfaceBySSID returns the wifi Interface by Index
func (u *UCI) GetWifiIfaceBySSID(ssid string) *UCIWirelessInterface {
ifaces := u.Wireless.Interfaces
if len(ifaces) <= 0 {
fmt.Println("ifaces are empty !!! fuck")
return nil
}
for _, ifa := range ifaces {
if ifa.Ssid == ssid {
return ifa
}
}
fmt.Println("iface not found !")
return nil
}
// GetWifiIfaces wifi interfaces in configuration
2018-10-29 15:10:32 +01:00
func (u *UCI) GetWifiIfaces() map[int]*UCIWirelessInterface {
return u.Wireless.Interfaces
}
// GetWifiDevices returns the wifi devices in configuration
func (u *UCI) GetWifiDevices() []map[string]string {
return u.Wireless.Devices
}
// GetWifiDeviceByName returns a wifi device by his name "radio0" "radio1"
func (u *UCI) GetWifiDeviceByName(name string) map[string]string {
for _, device := range u.Wireless.Devices {
if device["Device"] == name {
return device
}
}
return nil
}
// Service make restart services via the UCI client possible
func (u *UCI) Service(name string, action string) error {
sys := NewSystemWithExecutor(u.exec)
res := sys.Service(name, action)
if res.ReturnCode != 0 {
return fmt.Errorf("%d - %s - %s - %s",
res.ReturnCode,
res.Command,
res.Stdout,
res.Stderr)
}
return nil
}