orion/openwrt/uci.go

73 lines
1.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
}
// NewUCI return an UCI instance to interact with UCI
func NewUCI() *UCI {
exec := &localExecutor{}
return &UCI{exec}
}
// 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 {
return &UCI{exec}
}
// uciRun, private method to run the UCI command
func (u *UCI) uciRun(uciAction string, param string) *Action {
cmd := "uci"
res := u.exec.Run(cmd, uciAction, 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 {
return u.uciRun("commit", "")
}
// Reload reload uci configuration
func (u *UCI) Reload() *Action {
cmdResult := u.exec.Run("reload_config")
time.Sleep(5 * time.Second)
return &Action{cmdResult}
}
// AddWireless Create a new Wireless entry in UCI configuration
func (u *UCI) AddWireless(name string) *Action {
res := u.Add("wireless", name)
return res
}