read & print test SIP messages
This commit is contained in:
61
main.go
61
main.go
@ -2,8 +2,67 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, I'm the SIP Parser!")
|
||||
content, errChan := readTestFiles("./sip_messages")
|
||||
printMessages(content, errChan)
|
||||
}
|
||||
|
||||
// readTestFiles reads all files in the given directory that start with "test" and sends their content to the output channel.
|
||||
// If an error occurs, it sends the error to the error channel.
|
||||
func readTestFiles(directory string) (<-chan string, <-chan error) {
|
||||
output := make(chan string)
|
||||
errChan := make(chan error)
|
||||
|
||||
go func(output chan<- string, errChan chan<- error) {
|
||||
defer close(output)
|
||||
defer close(errChan)
|
||||
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if !file.IsDir() && strings.HasPrefix(file.Name(), "test") {
|
||||
content, err := os.ReadFile(directory + "/" + file.Name())
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
output <- fmt.Sprintf("Content of %s:\n%s\n", file.Name(), content)
|
||||
}
|
||||
}
|
||||
}(output, errChan)
|
||||
|
||||
return output, errChan
|
||||
}
|
||||
|
||||
// printMessages reads from the content and error channels and prints the messages to the console.
|
||||
// It stops when both channels are closed.
|
||||
func printMessages(content <-chan string, errChan <-chan error) {
|
||||
for {
|
||||
select {
|
||||
case c, ok := <-content:
|
||||
if !ok {
|
||||
content = nil
|
||||
} else {
|
||||
fmt.Println(c)
|
||||
}
|
||||
case err, ok := <-errChan:
|
||||
if !ok {
|
||||
errChan = nil
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
if content == nil && errChan == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user