From ec26c74ae895b08b6013c8981eb7d9cd16e9afd5 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Thu, 20 Sep 2018 17:34:34 +0200 Subject: [PATCH] Spliting "complex" methods to please go linter ! --- openwrt/network.go | 4 +--- openwrt/uci.go | 2 +- openwrt/wifi.go | 39 ++++++++++++++++-------------- openwrt/wifi_cell.go | 56 ++++++++++++++++++++++++++++---------------- 4 files changed, 59 insertions(+), 42 deletions(-) diff --git a/openwrt/network.go b/openwrt/network.go index 6505d46..2384829 100644 --- a/openwrt/network.go +++ b/openwrt/network.go @@ -32,9 +32,7 @@ func (n *Network) ListInterfaces() []net.Interface { fmt.Print(fmt.Errorf("error listing network interfacess: %+v", err.Error())) return nil } - for _, i := range ifaces { - result = append(result, i) - } + result = append(result, ifaces...) return result } diff --git a/openwrt/uci.go b/openwrt/uci.go index c5d7429..0613643 100644 --- a/openwrt/uci.go +++ b/openwrt/uci.go @@ -60,7 +60,7 @@ func (u *UCI) Commit() *Action { func (u *UCI) Reload() *Action { cmdResult := u.exec.Run("reload_config") - time.Sleep(5) + time.Sleep(5 * time.Second) return &Action{cmdResult} } diff --git a/openwrt/wifi.go b/openwrt/wifi.go index 9507b9f..d13f5d7 100644 --- a/openwrt/wifi.go +++ b/openwrt/wifi.go @@ -26,28 +26,12 @@ func NewWifiWithExecutor(exec Executor, wIface string) *Wifi { return &Wifi{exec, wIface, nil} } -// GetWifiCells retrieves all availabe wifi cells for a card ! -func (w *Wifi) GetWifiCells() int { - res := w.exec.Run("iwinfo", w.iface, "scan") - if res.ReturnCode != 0 { - log.Fatal(res.Stderr) - return res.ReturnCode - } - - for res.Stdout == "Scanning not possible" { - time.Sleep(1) - res = w.exec.Run("iwinfo", w.iface, "scan") - if res.ReturnCode != 0 { - log.Fatal(res.Stderr) - return res.ReturnCode - } - } - +func (w *Wifi) parseWifiCells(stdout string) int { new := false mac := "" ssid := "" enc := "" - for _, line := range strings.Split(strings.TrimSuffix(res.Stdout, "\n"), "\n") { + for _, line := range strings.Split(strings.TrimSuffix(stdout, "\n"), "\n") { if strings.HasPrefix(line, "Cell") && new == false { new = true mac = strings.Split(line, " ")[4] @@ -77,6 +61,25 @@ func (w *Wifi) GetWifiCells() int { return 0 } +// GetWifiCells retrieves all availabe wifi cells for a card ! +func (w *Wifi) GetWifiCells() int { + res := w.exec.Run("iwinfo", w.iface, "scan") + if res.ReturnCode != 0 { + log.Fatal(res.Stderr) + return res.ReturnCode + } + + for res.Stdout == "Scanning not possible" { + time.Sleep(time.Second) + res = w.exec.Run("iwinfo", w.iface, "scan") + if res.ReturnCode != 0 { + log.Fatal(res.Stderr) + return res.ReturnCode + } + } + return w.parseWifiCells(res.Stdout) +} + // GetCell retreives an WifiCell by SSID provided in parameter func (w *Wifi) GetCell(ssid string) *WifiCell { for _, v := range w.Cells { diff --git a/openwrt/wifi_cell.go b/openwrt/wifi_cell.go index ccdfdc3..3ce823e 100644 --- a/openwrt/wifi_cell.go +++ b/openwrt/wifi_cell.go @@ -18,17 +18,7 @@ func NewWifiCell(ssid string, mac string, encrypt string) *WifiCell { } } -// Connect to wifi Cell -func (cell *WifiCell) Connect(uci *UCI, secret string) *Action { - delRes := uci.Delete("wireless.@wifi-iface[1]") - if delRes.ReturnCode != 0 { - return delRes - } - addRes := uci.AddWireless("wifi-iface") - if addRes.ReturnCode != 0 { - return addRes - } - +func (cell *WifiCell) uciWifiConfigure(uci *UCI, secret string) *Action { setRes := uci.Set("wireless.@wifi-iface[1].network", "PyxisNetwork") if setRes.ReturnCode != 0 { return setRes @@ -58,15 +48,41 @@ func (cell *WifiCell) Connect(uci *UCI, secret string) *Action { if setRes.ReturnCode != 0 { return setRes } - setRes = uci.Commit() - if setRes.ReturnCode != 0 { - return setRes - } - setRes = uci.Reload() - if setRes.ReturnCode != 0 { - return setRes - } - time.Sleep(20) + + return &Action{ + &CommandResult{ + Stdout: "", + Stderr: "", + ReturnCode: 0, + }, + } +} + +// Connect to wifi Cell +func (cell *WifiCell) Connect(uci *UCI, secret string) *Action { + delRes := uci.Delete("wireless.@wifi-iface[1]") + if delRes.ReturnCode != 0 { + return delRes + } + addRes := uci.AddWireless("wifi-iface") + if addRes.ReturnCode != 0 { + return addRes + } + + setRes := cell.uciWifiConfigure(uci, secret) + if setRes.ReturnCode != 0 { + return setRes + } + + setRes = uci.Commit() + if setRes.ReturnCode != 0 { + return setRes + } + setRes = uci.Reload() + if setRes.ReturnCode != 0 { + return setRes + } + time.Sleep(20 * time.Second) return &Action{ &CommandResult{ Stdout: "",