Spliting "complex" methods to please go linter !

This commit is contained in:
Philippe Caseiro 2018-09-20 17:34:34 +02:00
parent de0255fd03
commit ec26c74ae8
4 changed files with 59 additions and 42 deletions

View File

@ -32,9 +32,7 @@ func (n *Network) ListInterfaces() []net.Interface {
fmt.Print(fmt.Errorf("error listing network interfacess: %+v", err.Error())) fmt.Print(fmt.Errorf("error listing network interfacess: %+v", err.Error()))
return nil return nil
} }
for _, i := range ifaces { result = append(result, ifaces...)
result = append(result, i)
}
return result return result
} }

View File

@ -60,7 +60,7 @@ func (u *UCI) Commit() *Action {
func (u *UCI) Reload() *Action { func (u *UCI) Reload() *Action {
cmdResult := u.exec.Run("reload_config") cmdResult := u.exec.Run("reload_config")
time.Sleep(5) time.Sleep(5 * time.Second)
return &Action{cmdResult} return &Action{cmdResult}
} }

View File

@ -26,28 +26,12 @@ func NewWifiWithExecutor(exec Executor, wIface string) *Wifi {
return &Wifi{exec, wIface, nil} return &Wifi{exec, wIface, nil}
} }
// GetWifiCells retrieves all availabe wifi cells for a card ! func (w *Wifi) parseWifiCells(stdout string) int {
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
}
}
new := false new := false
mac := "" mac := ""
ssid := "" ssid := ""
enc := "" 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 { if strings.HasPrefix(line, "Cell") && new == false {
new = true new = true
mac = strings.Split(line, " ")[4] mac = strings.Split(line, " ")[4]
@ -77,6 +61,25 @@ func (w *Wifi) GetWifiCells() int {
return 0 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 // GetCell retreives an WifiCell by SSID provided in parameter
func (w *Wifi) GetCell(ssid string) *WifiCell { func (w *Wifi) GetCell(ssid string) *WifiCell {
for _, v := range w.Cells { for _, v := range w.Cells {

View File

@ -18,17 +18,7 @@ func NewWifiCell(ssid string, mac string, encrypt string) *WifiCell {
} }
} }
// Connect to wifi Cell func (cell *WifiCell) uciWifiConfigure(uci *UCI, secret string) *Action {
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 := uci.Set("wireless.@wifi-iface[1].network", "PyxisNetwork") setRes := uci.Set("wireless.@wifi-iface[1].network", "PyxisNetwork")
if setRes.ReturnCode != 0 { if setRes.ReturnCode != 0 {
return setRes return setRes
@ -58,15 +48,41 @@ func (cell *WifiCell) Connect(uci *UCI, secret string) *Action {
if setRes.ReturnCode != 0 { if setRes.ReturnCode != 0 {
return setRes return setRes
} }
setRes = uci.Commit()
if setRes.ReturnCode != 0 { return &Action{
return setRes &CommandResult{
} Stdout: "",
setRes = uci.Reload() Stderr: "",
if setRes.ReturnCode != 0 { ReturnCode: 0,
return setRes },
} }
time.Sleep(20) }
// 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{ return &Action{
&CommandResult{ &CommandResult{
Stdout: "", Stdout: "",