Spliting complex function parseWifiCells

This commit is contained in:
Philippe Caseiro 2018-09-21 15:42:48 +02:00
parent b34ccd3dee
commit 17487dd7a6
1 changed files with 13 additions and 13 deletions

View File

@ -26,11 +26,19 @@ func NewWifiWithExecutor(exec Executor, wIface string) *Wifi {
return &Wifi{exec, wIface, nil} return &Wifi{exec, wIface, nil}
} }
func (w *Wifi) getEncryption(line string) string {
enc := "unkn"
if strings.Contains(line, "WPA2 PSK") {
enc = "psk"
} else if strings.Contains(line, "none") {
enc = "none"
}
return enc
}
func (w *Wifi) parseWifiCells(stdout string) int { func (w *Wifi) parseWifiCells(stdout string) int {
new := false new := false
mac := "" mac, ssid, enc := "", "", ""
ssid := ""
enc := ""
for _, line := range strings.Split(strings.TrimSuffix(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
@ -41,20 +49,12 @@ func (w *Wifi) parseWifiCells(stdout string) int {
ssid = strings.Trim(ssid, "\"") ssid = strings.Trim(ssid, "\"")
} }
if strings.Contains(line, "Encryption:") { if strings.Contains(line, "Encryption:") {
if strings.Contains(line, "WPA2 PSK") { enc = w.getEncryption(line)
enc = "psk"
} else if strings.Contains(line, "none") {
enc = "none"
} else {
enc = "unkn"
}
} }
if len(mac) > 0 && len(ssid) > 0 && len(enc) > 0 { if len(mac) > 0 && len(ssid) > 0 && len(enc) > 0 {
cell := NewWifiCell(ssid, mac, enc) cell := NewWifiCell(ssid, mac, enc)
w.Cells = append(w.Cells, cell) w.Cells = append(w.Cells, cell)
ssid = "" ssid, mac, enc = "", "", ""
mac = ""
enc = ""
new = false new = false
} }
} }