-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate.go
45 lines (37 loc) · 921 Bytes
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package wpaclient
import (
"fmt"
"strings"
)
// validate can evolve to more complex function,
// since we are covering very limited return values
func validate(cmd string, buf []byte) error {
sb := strings.TrimSuffix(string(buf), "\n")
switch sb {
case "UNKNOWN COMMAND":
return ErrUnknownCmd
case "FAIL":
return ErrCmdFailed
}
ic := fmt.Sprintf("Invalid %s command", cmd)
if strings.HasPrefix(sb, ic) {
errs := strings.Replace(sb, ic, "", -1)
for i := 0; i < len(errs); i++ {
r := rune(errs[i])
if 96 < r && r < 123 {
errs = errs[i:]
break
}
}
errs = strings.Replace(errs, "\n", " ", -1)
return &InvalidCmdError{Cmd: cmd, Err: errs}
}
// usage message returned
if strings.HasPrefix(sb, strings.ToLower(cmd)) {
return &InvalidCmdError{Cmd: cmd}
}
if cmd == CmdPing && sb != "PONG" {
return fmt.Errorf("expected PONG got %s: %w", sb, ErrCmdFailed)
}
return nil
}