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

123 lines
3.0 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-10-05 16:12:07 +02:00
Command string
}
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-10-05 16:12:07 +02:00
return &Action{res, fmt.Sprintf("%s %s", cmd, param)}
}
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 {
2018-10-05 16:12:07 +02:00
cmd := "uci add"
commandRes := u.exec.Run(cmd, module, name)
return &Action{commandRes, fmt.Sprintf("%s %s %s", cmd, module, name)}
}
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-10-05 16:12:07 +02:00
return &Action{cmdResult, "reload_config"}
}
// 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()
}
2018-10-05 16:12:07 +02:00
// 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]
}
2018-10-05 16:12:07 +02:00
// 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
}
// 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
}