-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
57 lines (45 loc) · 982 Bytes
/
main.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 (
"fmt"
"log"
"os"
"time"
"golang.org/x/net/websocket"
)
const (
WS_BASE_URL = "ws://localhost:4243"
ORIGIN = "http://localhost"
)
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage : <program> <contain id> <cmd>")
return
}
id := os.Args[1]
cmd := os.Args[2]
wsURL := fmt.Sprintf("%s/v1.17/containers/%s/attach/ws?logs=0&stream=1&stdin=1&stdout=1&stderr=1", WS_BASE_URL, id)
useXNet(wsURL, cmd)
}
func useXNet(url string, cmd string) {
fmt.Printf("URL : %s\n", url)
ws, err := websocket.Dial(url, "", ORIGIN)
if err != nil {
log.Fatal(err)
}
defer ws.Close()
message := []byte(cmd + "\n")
fmt.Printf("Send: %s\n", message)
if _, err := ws.Write(message); err != nil {
log.Fatal(err)
}
ws.SetReadDeadline(time.Now().Add(1 * time.Second))
for {
var msg = make([]byte, 512)
var n int
if n, err = ws.Read(msg); err != nil {
break // usually is time out
}
fmt.Printf("%s", msg[:n])
msg = nil
}
}