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 // Action is the result of an UCI action output and return code
type Action struct { type Action struct {
output string *CommandResult
errors string
returnCode int
} }
// uciRun, private method to run the UCI command // uciRun, private method to run the UCI command
func uciRun(uciAction string, param string) *Action { func uciRun(uciAction string, param string) *Action {
cmd := "uci" cmd := "uci"
res := Run(cmd, uciAction, param) res := run(cmd, uciAction, param)
return &Action{ return &Action{
output: res.stdout, res,
errors: res.stderr,
returnCode: res.returnCode,
} }
} }
@ -66,13 +62,15 @@ func UciReload() *Action {
time.Sleep(5) time.Sleep(5)
return &Action{ return &Action{
output: out.String(), &CommandResult{
returnCode: 0, Stdout: out.String(),
ReturnCode: 0,
},
} }
} }
// UciAddWireless Create a new Wireless entry in UCI configuration // UciAddWireless Create a new Wireless entry in UCI configuration
func UciAddWireless(name string) int { func UciAddWireless(name string) int {
res := UciAdd("wireless", name) 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 package openwrt
import ( import (
"log"
"strings"
"testing" "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) { func TestUciAdd(t *testing.T) {
retCode = 0
retStdout = "OK"
retStderr = ""
// Return 0 run ! // Return 0 run !
res := UciAdd("wireless", "test") res := UciAdd("wireless", "test")
if res.returnCode != 0 { if res.ReturnCode != 0 {
t.Error(res.errors) t.Error(res.Stderr)
} }
} }

View File

@ -9,13 +9,13 @@ import (
// CommandResult contain all information about a command execution, stdout, stderr // CommandResult contain all information about a command execution, stdout, stderr
type CommandResult struct { type CommandResult struct {
stdout string Stdout string
stderr string Stderr string
returnCode int ReturnCode int
} }
// Run executes a system command and returns // 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 out bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer
@ -30,9 +30,9 @@ func Run(command string, params ...string) *CommandResult {
} }
return &CommandResult{ return &CommandResult{
stdout: out.String(), Stdout: out.String(),
stderr: stderr.String(), Stderr: stderr.String(),
// FIXME // 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 ! // GetWifiCells retrieves all availabe wifi cells for a card !
func GetWifiCells(iface string) { func GetWifiCells(iface string) {
res := Run("iwinfo", iface, "scan") res := run("iwinfo", iface, "scan")
for _, line := range strings.Split(strings.TrimSuffix(res.stdout, "\n"), "\n") { for _, line := range strings.Split(strings.TrimSuffix(res.Stdout, "\n"), "\n") {
fmt.Println(line) fmt.Println(line)
} }