-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexample_test.go
45 lines (40 loc) · 1020 Bytes
/
example_test.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
package dlog_test
import (
"bufio"
"context"
"fmt"
"log"
"net"
"net/http"
"github.com/ahmetalpbalkan/dlog"
)
func ExampleNewReader() {
client := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := net.Dial("unix", "/var/run/docker.sock")
if err != nil {
return nil, fmt.Errorf("cannot connect docker socket: %v", err)
}
return conn, nil
}}}
url := "http://-/containers/CONTAINER_NAME/logs?stdout=1&stderr=1&follow=1"
resp, err := client.Get(url)
if err != nil {
log.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalf("unexpected status code: %s", resp.Status)
}
// At this point we have a logs stream, here is how to read each log line
// from container:
r := dlog.NewReader(resp.Body)
s := bufio.NewScanner(r)
for s.Scan() {
log.Println(s)
}
if err := s.Err(); err != nil {
log.Fatalf("read error: %v", err)
}
}