Skip to content

Commit c03021e

Browse files
bharath-srinivasNARKOZ
authored andcommitted
ported fucking-coffee script to golang (NARKOZ#163)
1 parent 4bf5fe9 commit c03021e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

go/fucking-coffee.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"regexp"
8+
"time"
9+
10+
"github.com/codeskyblue/go-sh"
11+
"github.com/google/goexpect"
12+
)
13+
14+
func main() {
15+
// exit early if no sessions with my username are found
16+
currentUser, _ := sh.Command("who").Command("grep", "my_username").Output()
17+
if currentUser == nil {
18+
os.Exit(1)
19+
}
20+
21+
// info about the coffee machine
22+
coffeeMachineIP := "10.10.42.42"
23+
password := "1234"
24+
passwordPrompt := "Password: "
25+
delayBeforeBrew := 17 * time.Second
26+
delay := 24 * time.Second
27+
28+
// timeout for the telnet prompts
29+
timeout := 10 * time.Minute
30+
31+
// sleep 17 seconds before brewing coffee
32+
time.Sleep(delayBeforeBrew)
33+
34+
// spawn a new telnet session with the coffee machine
35+
t, _, err := expect.Spawn(fmt.Sprintf("telnet %s", coffeeMachineIP), -1)
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
defer t.Close()
40+
41+
t.Expect(regexp.MustCompile(passwordPrompt), timeout)
42+
t.Send(password + "\n")
43+
t.Expect(regexp.MustCompile("telnet>"), timeout)
44+
t.Send("sys brew\n")
45+
time.Sleep(delay)
46+
t.Expect(regexp.MustCompile("telnet>"), timeout)
47+
t.Send("sys pour\n")
48+
t.Expect(regexp.MustCompile("telnet>"), timeout)
49+
t.Send("exit\n")
50+
}

0 commit comments

Comments
 (0)