107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package openwrt
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Action is the result of an UCI action output and return code
|
|
type Action struct {
|
|
*CommandResult
|
|
}
|
|
|
|
// UCI "Object"
|
|
type UCI struct {
|
|
exec Executor
|
|
Wireless *UCIWirelessConf
|
|
}
|
|
|
|
// NewUCI return an UCI instance to interact with UCI
|
|
func NewUCI() *UCI {
|
|
exec := &localExecutor{}
|
|
wireless := &UCIWirelessConf{}
|
|
return &UCI{exec, 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) *UCI {
|
|
wireless := &UCIWirelessConf{}
|
|
return &UCI{exec, 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}
|
|
}
|
|
|
|
// Add add an entry to UCI configuration, specify the Module and the value
|
|
func (u *UCI) Add(module string, name string) *Action {
|
|
commandRes := u.exec.Run("uci add", module, name)
|
|
return &Action{commandRes}
|
|
}
|
|
|
|
// 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")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
return &Action{cmdResult}
|
|
}
|
|
|
|
// 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 system name of the Interface
|
|
func (u *UCI) GetWifiIface(idx int) *UCIWirelessInterface {
|
|
ifaces := u.Wireless.Interfaces
|
|
if len(ifaces) < idx {
|
|
return nil
|
|
}
|
|
return ifaces[idx]
|
|
}
|
|
|
|
// GetWifiIfaces wifi interfaces in configuration
|
|
func (u *UCI) GetWifiIfaces() []*UCIWirelessInterface {
|
|
return u.Wireless.Interfaces
|
|
}
|
|
|
|
// GetWifiDevices returns the wifi devices in configuration
|
|
func (u *UCI) GetWifiDevices() []map[string]string {
|
|
return u.Wireless.Devices
|
|
}
|