-
Notifications
You must be signed in to change notification settings - Fork 1
/
telnet.go
57 lines (46 loc) · 1.24 KB
/
telnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"bufio"
"fmt"
"log"
"net"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
func telnet(command string) ([]string, error) {
// connect to this socket
var response []string
liquidsoapHost := getEnv("LIQUIDSOAP_HOST")
liquidsoapPort := getEnv("LIQUIDSOAP_PORT")
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", liquidsoapHost, liquidsoapPort))
if err != nil {
log.Printf("Failed to connect to %s:%s, reason: %s", liquidsoapHost, liquidsoapPort, err)
liqConnectionFailure.With(prometheus.Labels{"host": liquidsoapHost, "port": liquidsoapPort}).Inc()
return response, err
}
defer conn.Close()
// read in input from stdin
// send to socket
fmt.Fprintf(conn, command+"\n")
// listen for reply
data := bufio.NewScanner(conn)
x := 0
for data.Scan() {
if x > 3 {
break
} else if strings.HasPrefix(data.Text(), "[playing]") {
log.Println("Skipping: " + data.Text())
} else if strings.HasPrefix(data.Text(), "[ready]") {
song := strings.Replace(data.Text(), "[ready] ", "", -1)
response = append(response, song)
} else if data.Text() == "END" {
break
} else if data.Text() == "" {
break
} else {
response = append(response, data.Text())
}
x = x + 1
}
return response, nil
}