This repository has been archived on 2024-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
orion/openwrt/uci.go

107 lines
2.6 KiB
Go
Raw Normal View History

package openwrt
import (
"fmt"
"time"
)
// Action is the result of an UCI action output and return code
type Action struct {
*CommandResult
}
2018-09-20 10:59:11 +02:00
// UCI "Object"
type UCI struct {
exec Executor
Wireless *UCIWirelessConf
2018-09-20 10:59:11 +02:00
}
2018-09-20 10:59:11 +02:00
// NewUCI return an UCI instance to interact with UCI
func NewUCI() *UCI {
exec := &localExecutor{}
wireless := &UCIWirelessConf{}
return &UCI{exec, wireless}
}
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 {
wireless := &UCIWirelessConf{}
return &UCI{exec, wireless}
}
2018-09-20 10:59:11 +02:00
// uciRun, private method to run the UCI command
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-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-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-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-20 10:59:11 +02:00
// Commit the recent actions to UCI
func (u *UCI) Commit() *Action {
res := u.uciRun("commit")
return res
2018-09-20 10:59:11 +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")
time.Sleep(5 * time.Second)
2018-09-20 10:59:11 +02:00
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
}
2018-09-20 10:59:11 +02:00
// AddWireless Create a new Wireless entry in UCI configuration
func (u *UCI) AddWireless() *Action {
res := u.uciRun("add", "wireless", "wifi-iface")
2018-09-20 10:59:11 +02:00
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
}