Skip to content

Commit

Permalink
Fix leaky logs
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomika committed Oct 5, 2024
1 parent e051891 commit 967f81f
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pico/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pico

import (
"bufio"
"context"
"encoding/json"
"fmt"
"log/slog"
Expand Down Expand Up @@ -65,7 +66,7 @@ func (c *Cmd) notifications() error {
return nil
}

func (c *Cmd) logs() error {
func (c *Cmd) logs(ctx context.Context) error {
sshClient, err := shared.CreateSSHClient(
shared.GetEnv("PICO_SENDLOG_ENDPOINT", "send.pico.sh:22"),
shared.GetEnv("PICO_SENDLOG_KEY", "ssh_data/term_info_ed25519"),
Expand All @@ -76,16 +77,16 @@ func (c *Cmd) logs() error {
if err != nil {
return err
}

defer sshClient.Close()

session, err := sshClient.NewSession()
defer func() {
_ = session.Close()
}()
if err != nil {
return err
}

defer session.Close()

stdoutPipe, err := session.StdoutPipe()
if err != nil {
return err
Expand All @@ -96,6 +97,12 @@ func (c *Cmd) logs() error {
return err
}

go func() {
<-ctx.Done()
session.Close()
sshClient.Close()
}()

scanner := bufio.NewScanner(stdoutPipe)
for scanner.Scan() {
line := scanner.Text()
Expand Down Expand Up @@ -183,7 +190,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
opts.help()
return
} else if cmd == "logs" {
err = opts.logs()
err = opts.logs(sesh.Context())
if err != nil {
wish.Fatalln(sesh, err)
}
Expand Down

0 comments on commit 967f81f

Please sign in to comment.