-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: structure and clear unit tests (#4)
* Add mocks * Remove old unittests * Update version to v0.4.3 * Improve atcom to ease using in scripts * Update version to v0.5.0
- Loading branch information
Showing
16 changed files
with
336 additions
and
943 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package atcom | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ATCommand struct { | ||
Command string | ||
Response []string | ||
Processed []string | ||
Error error | ||
|
||
Desired []string | ||
Fault []string | ||
Timeout int | ||
LineEnd bool | ||
} | ||
|
||
func NewATCommand(command string) *ATCommand { | ||
return &ATCommand{ | ||
Command: command, | ||
Desired: nil, | ||
Fault: nil, | ||
Timeout: 5, | ||
LineEnd: true, | ||
} | ||
} | ||
|
||
func (atc *ATCommand) GetMeaningfulPart(prefix string) error { | ||
|
||
if atc.Response == nil { | ||
return fmt.Errorf("no response") | ||
} | ||
|
||
if prefix == "" { | ||
atc.Processed = atc.Response | ||
return nil | ||
} | ||
|
||
var echoRow int = 0 | ||
var lastRow int = 0 | ||
var data []string | ||
|
||
for index, line := range atc.Response { | ||
line = strings.TrimSpace(line) | ||
|
||
if strings.HasPrefix(line, atc.Command) { | ||
echoRow = index | ||
continue | ||
} | ||
|
||
if line == "OK" { | ||
lastRow = index | ||
break | ||
} | ||
} | ||
|
||
if lastRow == 0 { | ||
return fmt.Errorf("no ok response") | ||
} | ||
|
||
if prefix == "" { | ||
data = atc.Response[echoRow+1 : lastRow] | ||
} else { | ||
for _, line := range atc.Response[echoRow+1 : lastRow] { | ||
if strings.HasPrefix(line, prefix) { | ||
line = strings.Trim(line, prefix) | ||
data = append(data, line) | ||
} | ||
} | ||
} | ||
|
||
atc.Processed = data | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.