2018-09-19 14:39:00 +02:00
|
|
|
package openwrt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
// uciRun, private method to run the UCI command
|
|
|
|
func uciRun(uciAction string, param string) *Action {
|
|
|
|
cmd := "uci"
|
|
|
|
|
2018-09-19 15:22:51 +02:00
|
|
|
res := run(cmd, uciAction, param)
|
2018-09-19 14:39:00 +02:00
|
|
|
return &Action{
|
2018-09-19 15:22:51 +02:00
|
|
|
res,
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2018-09-19 15:22:51 +02:00
|
|
|
&CommandResult{
|
|
|
|
Stdout: out.String(),
|
|
|
|
ReturnCode: 0,
|
|
|
|
},
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UciAddWireless Create a new Wireless entry in UCI configuration
|
|
|
|
func UciAddWireless(name string) int {
|
|
|
|
res := UciAdd("wireless", name)
|
2018-09-19 15:22:51 +02:00
|
|
|
return res.ReturnCode
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|