diff --git a/openwrt/executor.go b/openwrt/executor.go new file mode 100644 index 0000000..2c19fb7 --- /dev/null +++ b/openwrt/executor.go @@ -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, + } +} diff --git a/openwrt/test.go b/openwrt/test.go new file mode 100644 index 0000000..77b3e3a --- /dev/null +++ b/openwrt/test.go @@ -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, + } +} diff --git a/openwrt/uci.go b/openwrt/uci.go index 65f3c0c..d02872f 100644 --- a/openwrt/uci.go +++ b/openwrt/uci.go @@ -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 } diff --git a/openwrt/uci_struct.go b/openwrt/uci_struct.go new file mode 100644 index 0000000..d440ae6 --- /dev/null +++ b/openwrt/uci_struct.go @@ -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} +} diff --git a/openwrt/uci_struct_test.go b/openwrt/uci_struct_test.go new file mode 100644 index 0000000..af25884 --- /dev/null +++ b/openwrt/uci_struct_test.go @@ -0,0 +1,11 @@ +package openwrt + +import ( + "testing" +) + +func TestUCIStruct(t *testing.T) { + exec := createMockExecutor("", "", 1) + uci := NewUCIWithExecutor(exec) + uci.Add("wireless", "test") +} diff --git a/openwrt/uci_test.go b/openwrt/uci_test.go index 14ba08f..927884d 100644 --- a/openwrt/uci_test.go +++ b/openwrt/uci_test.go @@ -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) } } diff --git a/openwrt/utils.go b/openwrt/utils.go index e2e5a84..6e0dbed 100644 --- a/openwrt/utils.go +++ b/openwrt/utils.go @@ -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, } } diff --git a/openwrt/wifi_cell.go b/openwrt/wifi_cell.go index 15e0ffe..a6ac91f 100644 --- a/openwrt/wifi_cell.go +++ b/openwrt/wifi_cell.go @@ -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) }