| 1 | package main |
| 2 | |
| 3 | import "github.com/ThomasRooney/gexpect" |
| 4 | import "fmt" |
| 5 | import "strings" |
| 6 | |
| 7 | func main() { |
| 8 | waitChan := make(chan string) |
| 9 | |
| 10 | fmt.Printf("Starting screen.. \n") |
| 11 | |
| 12 | child, err := gexpect.Spawn("screen") |
| 13 | if err != nil { |
| 14 | panic(err) |
| 15 | } |
| 16 | |
| 17 | sender, reciever := child.AsyncInteractChannels() |
| 18 | go func() { |
| 19 | waitString := "" |
| 20 | count := 0 |
| 21 | for { |
| 22 | select { |
| 23 | case waitString = <-waitChan: |
| 24 | count++ |
| 25 | case msg, open := <-reciever: |
| 26 | if !open { |
| 27 | return |
| 28 | } |
| 29 | fmt.Printf("Recieved: %s\n", msg) |
| 30 | |
| 31 | if strings.Contains(msg, waitString) { |
| 32 | if count >= 1 { |
| 33 | waitChan <- msg |
| 34 | count -= 1 |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | }() |
| 40 | wait := func(str string) { |
| 41 | waitChan <- str |
| 42 | <-waitChan |
| 43 | } |
| 44 | fmt.Printf("Waiting until started.. \n") |
| 45 | wait(" ") |
| 46 | fmt.Printf("Sending Enter.. \n") |
| 47 | sender <- "\n" |
| 48 | wait("$") |
| 49 | fmt.Printf("Sending echo.. \n") |
| 50 | sender <- "echo Hello World\n" |
| 51 | wait("Hello World") |
| 52 | fmt.Printf("Received echo. \n") |
| 53 | } |