2018-09-19 14:39:00 +02:00
|
|
|
package openwrt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Action is the result of an UCI action output and return code
|
|
|
|
type Action struct {
|
2018-09-19 15:22:51 +02:00
|
|
|
*CommandResult
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// UCI "Object"
|
|
|
|
type UCI struct {
|
2018-09-24 17:38:17 +02:00
|
|
|
exec Executor
|
|
|
|
Wireless *UCIWirelessConf
|
2018-09-20 10:59:11 +02:00
|
|
|
}
|
2018-09-19 14:39:00 +02:00
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// NewUCI return an UCI instance to interact with UCI
|
|
|
|
func NewUCI() *UCI {
|
|
|
|
exec := &localExecutor{}
|
2018-09-24 17:38:17 +02:00
|
|
|
wireless := &UCIWirelessConf{}
|
|
|
|
return &UCI{exec, wireless}
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// 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 {
|
2018-09-24 17:38:17 +02:00
|
|
|
wireless := &UCIWirelessConf{}
|
|
|
|
return &UCI{exec, wireless}
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// uciRun, private method to run the UCI command
|
2018-09-24 17:38:17 +02:00
|
|
|
func (u *UCI) uciRun(param ...string) *Action {
|
|
|
|
cmd := "/sbin/uci"
|
|
|
|
res := u.exec.Run(cmd, param...)
|
2018-09-20 10:59:11 +02:00
|
|
|
return &Action{res}
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// 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}
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// Delete delete an entry from UCI configuration specify the entry name
|
|
|
|
func (u *UCI) Delete(entry string) *Action {
|
|
|
|
return u.uciRun("delete", entry)
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// 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))
|
|
|
|
}
|
2018-09-19 14:39:00 +02:00
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// Commit the recent actions to UCI
|
|
|
|
func (u *UCI) Commit() *Action {
|
2018-09-24 17:38:17 +02:00
|
|
|
res := u.uciRun("commit")
|
|
|
|
return res
|
2018-09-20 10:59:11 +02:00
|
|
|
}
|
2018-09-19 14:39:00 +02:00
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// Reload reload uci configuration
|
|
|
|
func (u *UCI) Reload() *Action {
|
|
|
|
cmdResult := u.exec.Run("reload_config")
|
2018-09-19 14:39:00 +02:00
|
|
|
|
2018-09-20 17:34:34 +02:00
|
|
|
time.Sleep(5 * time.Second)
|
2018-09-19 14:39:00 +02:00
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
return &Action{cmdResult}
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// Show returns the output of uci show command
|
|
|
|
func (u *UCI) Show(target string) *Action {
|
|
|
|
cmdRes := u.uciRun("show", target)
|
|
|
|
return cmdRes
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// AddWireless Create a new Wireless entry in UCI configuration
|
2018-09-24 17:38:17 +02:00
|
|
|
func (u *UCI) AddWireless() *Action {
|
|
|
|
res := u.uciRun("add", "wireless", "wifi-iface")
|
2018-09-20 10:59:11 +02:00
|
|
|
return res
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
2018-09-24 17:38:17 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|