Skip to content

Commit

Permalink
feat: implemented tail --no-follow
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Nov 3, 2021
1 parent 98eab14 commit 96ab844
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/vproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func tailLogs(c *cli.Context) error {

hostname := c.Args().First()
client := createClient(c)
client.Attach(hostname)
client.Tail(hostname, !c.Bool("no-follow"))

return nil
}
Expand Down
11 changes: 7 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func (c *Client) addBinding(bind string, detach bool) {
if detach {
c.wg.Done()
} else {
c.Attach(s[0])
c.Tail(s[0], true)
}
res.Body.Close()
}

func (c *Client) Attach(hostname string) {
func (c *Client) Tail(hostname string, follow bool) {
data := url.Values{}
data.Add("host", hostname)
res, err := http.DefaultClient.PostForm(c.uri("/clients/stream"), data)
Expand All @@ -98,14 +98,17 @@ func (c *Client) Attach(hostname string) {
log.Fatalf("error registering client: %s\n", err)
}
fmt.Printf("[*] streaming logs for %s\n", hostname)
streamLogs(res)
streamLogs(res, follow)
}

func streamLogs(res *http.Response) {
func streamLogs(res *http.Response, follow bool) {
defer res.Body.Close()
r := bufio.NewReader(res.Body)
for {
line, err := r.ReadString('\n')
if line == "---\n" && !follow {
os.Exit(0)
}
if err != nil {
if line != "" && strings.Contains(line, "error") {
fmt.Println(line)
Expand Down
1 change: 1 addition & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func (d *Daemon) relayLogsUntilClose(vhost *Vhost, w http.ResponseWriter, reqCtx
buff := vhost.BufferAsString()
if buff != "" {
fmt.Fprint(w, buff)
fmt.Fprintln(w, "---")
}

// Listen to connection close and un-register logChan
Expand Down

0 comments on commit 96ab844

Please sign in to comment.