Skip to content

Commit

Permalink
feat: response processing
Browse files Browse the repository at this point in the history
  • Loading branch information
selengalp committed Nov 13, 2023
1 parent 0760aa3 commit a7f011c
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package atcom

import (
"errors"
"strings"
)

func GetMeaningfulPart(response []string, command string, prefix string) (data []string, len int, err error) {

var echoRow int = 0
var lastRow int = 0

for index, line := range response {
line = strings.TrimSpace(line)

if strings.HasPrefix(line, command) {
echoRow = index
continue
}

if line == "OK" {
lastRow = index
break
}
}

if lastRow == 0 {
return nil, 0, errors.New("no ok response")
}

if prefix == "" {
data = response[echoRow+1 : lastRow]
len = lastRow - echoRow - 1
} else {
for _, line := range response[echoRow+1 : lastRow] {
if strings.HasPrefix(line, prefix) {
line = strings.Trim(line, prefix)
data = append(data, line)
len++
}
}
}

return data, len, nil
}

0 comments on commit a7f011c

Please sign in to comment.