orion/openwrt/uci.go

79 lines
1.6 KiB
Go

package openwrt
import (
"bytes"
"fmt"
"log"
"os/exec"
"time"
)
// Action is the result of an UCI action output and return code
type Action struct {
output string
errors string
returnCode int
}
// uciRun, private method to run the UCI command
func uciRun(uciAction string, param string) *Action {
cmd := "uci"
res := Run(cmd, uciAction, param)
return &Action{
output: res.stdout,
errors: res.stderr,
returnCode: res.returnCode,
}
}
// UciAdd add an entry to UCI configuration, specify the Module and the value
func UciAdd(module string, name string) *Action {
actionRes := uciRun("add", fmt.Sprintf("%s %s", module, name))
return actionRes
}
// UciDelete delete an entry from UCI configuration specify the entry name
func UciDelete(entry string) *Action {
return uciRun("delete", entry)
}
// UciSet set a value ton an UCI configuration entry
func UciSet(entry string, value string) *Action {
return uciRun("set", fmt.Sprintf("%s=%s", entry, value))
}
// UciCommit the recent actions to UCI
func UciCommit() *Action {
return uciRun("commit", "")
}
// UciReload reload uci configuration
func UciReload() *Action {
var out bytes.Buffer
var stderr bytes.Buffer
exe := exec.Command("reload_config")
exe.Stdout = &out
exe.Stderr = &stderr
err := exe.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
log.Fatal(err)
}
time.Sleep(5)
return &Action{
output: out.String(),
returnCode: 0,
}
}
// UciAddWireless Create a new Wireless entry in UCI configuration
func UciAddWireless(name string) int {
res := UciAdd("wireless", name)
return res.returnCode
}