Object oriented UCI + Executor interface

This commit is contained in:
wpetit 2018-09-19 15:22:51 +02:00 committed by Philippe Caseiro
parent b8f0a554c1
commit e1f0c68630
8 changed files with 115 additions and 54 deletions

37
openwrt/executor.go Normal file
View File

@ -0,0 +1,37 @@
package openwrt
import (
"bytes"
"fmt"
"log"
"os/exec"
)
type Executor interface {
Run(command string, params ...string) *CommandResult
}
type localExecutor struct{}
func (e *localExecutor) 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,
}
}

29
openwrt/test.go Normal file
View File

@ -0,0 +1,29 @@
package openwrt
import (
"log"
"strings"
)
func createMockExecutor(stdout string, stderr string, returnCode int) Executor {
return &mockExecutor{
stdout: stdout,
stderr: stderr,
returnCode: returnCode,
}
}
type mockExecutor struct {
stdout string
stderr string
returnCode int
}
func (e *mockExecutor) Run(command string, params ...string) *CommandResult {
log.Printf("executing '%s %s'", command, strings.Join(params, " "))
return &CommandResult{
Stderr: e.stderr,
Stdout: e.stdout,
ReturnCode: e.returnCode,
}
}

View File

@ -10,20 +10,16 @@ import (
// Action is the result of an UCI action output and return code
type Action struct {
output string
errors string
returnCode int
*CommandResult
}
// uciRun, private method to run the UCI command
func uciRun(uciAction string, param string) *Action {
cmd := "uci"
res := Run(cmd, uciAction, param)
res := run(cmd, uciAction, param)
return &Action{
output: res.stdout,
errors: res.stderr,
returnCode: res.returnCode,
res,
}
}
@ -66,13 +62,15 @@ func UciReload() *Action {
time.Sleep(5)
return &Action{
output: out.String(),
returnCode: 0,
&CommandResult{
Stdout: 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
return res.ReturnCode
}

19
openwrt/uci_struct.go Normal file
View File

@ -0,0 +1,19 @@
package openwrt
type UCI struct {
exec Executor
}
func NewUCI() *UCI {
exec := &localExecutor{}
return &UCI{exec}
}
func NewUCIWithExecutor(exec Executor) *UCI {
return &UCI{exec}
}
func (u *UCI) Add(module string, name string) *Action {
commandRes := u.exec.Run("uci add", module, name)
return &Action{commandRes}
}

View File

@ -0,0 +1,11 @@
package openwrt
import (
"testing"
)
func TestUCIStruct(t *testing.T) {
exec := createMockExecutor("", "", 1)
uci := NewUCIWithExecutor(exec)
uci.Add("wireless", "test")
}

View File

@ -1,48 +1,15 @@
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)
if res.ReturnCode != 0 {
t.Error(res.Stderr)
}
}

View File

@ -9,13 +9,13 @@ import (
// CommandResult contain all information about a command execution, stdout, stderr
type CommandResult struct {
stdout string
stderr string
returnCode int
Stdout string
Stderr string
ReturnCode int
}
// Run executes a system command and returns
func Run(command string, params ...string) *CommandResult {
func run(command string, params ...string) *CommandResult {
var out bytes.Buffer
var stderr bytes.Buffer
@ -30,9 +30,9 @@ func Run(command string, params ...string) *CommandResult {
}
return &CommandResult{
stdout: out.String(),
stderr: stderr.String(),
Stdout: out.String(),
Stderr: stderr.String(),
// FIXME
returnCode: 0,
ReturnCode: 0,
}
}

View File

@ -23,8 +23,8 @@ func NewWifiCell(ssid string, mac string) *WifiCell {
// 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") {
res := run("iwinfo", iface, "scan")
for _, line := range strings.Split(strings.TrimSuffix(res.Stdout, "\n"), "\n") {
fmt.Println(line)
}