Adding begin of openwrt manipulation module

This commit is contained in:
Philippe Caseiro 2018-09-19 14:39:00 +02:00
parent 1aa77c90ab
commit b8f0a554c1
4 changed files with 195 additions and 0 deletions

78
openwrt/uci.go Normal file
View File

@ -0,0 +1,78 @@
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
}

48
openwrt/uci_test.go Normal file
View File

@ -0,0 +1,48 @@
package openwrt
import (
"log"
"strings"
"testing"
)
var retCode int
var retStdout string
var retStderr string
type UCI struct {
exec Executor
}
func (u *UCI) Run(command string, args []string) (stdout string, stderr string, exit int, err error) {
return u.exec.Run(command, args)
}
func NewUCI(exec Executor) *UCI {
return &UCI{exec}
}
type Executor interface {
Run(command string, args []string) (stdout string, stderr string, exit int, err error)
}
type fakeExecutor struct{}
func (e *fakeExecutor) Run(command string, args []string) (stdout string, stderr string, exit int, err error) {
log.Printf("executing '%s %s'", command, strings.Join(args, " "))
return "", "", 0, nil
}
func TestUciAdd(t *testing.T) {
retCode = 0
retStdout = "OK"
retStderr = ""
// Return 0 run !
res := UciAdd("wireless", "test")
if res.returnCode != 0 {
t.Error(res.errors)
}
}

38
openwrt/utils.go Normal file
View File

@ -0,0 +1,38 @@
package openwrt
import (
"bytes"
"fmt"
"log"
"os/exec"
)
// CommandResult contain all information about a command execution, stdout, stderr
type CommandResult struct {
stdout string
stderr string
returnCode int
}
// Run executes a system command and returns
func Run(command string, params ...string) *CommandResult {
var out bytes.Buffer
var stderr bytes.Buffer
exe := exec.Command(command, params...)
exe.Stdout = &out
exe.Stderr = &stderr
err := exe.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
log.Fatal(err)
}
return &CommandResult{
stdout: out.String(),
stderr: stderr.String(),
// FIXME
returnCode: 0,
}
}

31
openwrt/wifi_cell.go Normal file
View File

@ -0,0 +1,31 @@
package openwrt
import (
"fmt"
"strings"
)
// WifiCell reprensents wifi network Cell
type WifiCell struct {
// The SSID
Ssid string
// The cell mac adress
MacAdress string
}
// NewWifiCell returns a new WifiCell object
func NewWifiCell(ssid string, mac string) *WifiCell {
return &WifiCell{
Ssid: ssid,
MacAdress: mac,
}
}
// GetWifiCells retrieves all availabe wifi cells for a card !
func GetWifiCells(iface string) {
res := Run("iwinfo", iface, "scan")
for _, line := range strings.Split(strings.TrimSuffix(res.stdout, "\n"), "\n") {
fmt.Println(line)
}
}