- commit
- 967f81f
- parent
- e051891
- author
- Antonio Mika
- date
- 2024-10-05 04:28:07 +0000 UTC
Fix leaky logs
1 files changed,
+12,
-5
+12,
-5
1@@ -2,6 +2,7 @@ package pico
2
3 import (
4 "bufio"
5+ "context"
6 "encoding/json"
7 "fmt"
8 "log/slog"
9@@ -65,7 +66,7 @@ func (c *Cmd) notifications() error {
10 return nil
11 }
12
13-func (c *Cmd) logs() error {
14+func (c *Cmd) logs(ctx context.Context) error {
15 sshClient, err := shared.CreateSSHClient(
16 shared.GetEnv("PICO_SENDLOG_ENDPOINT", "send.pico.sh:22"),
17 shared.GetEnv("PICO_SENDLOG_KEY", "ssh_data/term_info_ed25519"),
18@@ -76,16 +77,16 @@ func (c *Cmd) logs() error {
19 if err != nil {
20 return err
21 }
22+
23 defer sshClient.Close()
24
25 session, err := sshClient.NewSession()
26- defer func() {
27- _ = session.Close()
28- }()
29 if err != nil {
30 return err
31 }
32
33+ defer session.Close()
34+
35 stdoutPipe, err := session.StdoutPipe()
36 if err != nil {
37 return err
38@@ -96,6 +97,12 @@ func (c *Cmd) logs() error {
39 return err
40 }
41
42+ go func() {
43+ <-ctx.Done()
44+ session.Close()
45+ sshClient.Close()
46+ }()
47+
48 scanner := bufio.NewScanner(stdoutPipe)
49 for scanner.Scan() {
50 line := scanner.Text()
51@@ -183,7 +190,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
52 opts.help()
53 return
54 } else if cmd == "logs" {
55- err = opts.logs()
56+ err = opts.logs(sesh.Context())
57 if err != nil {
58 wish.Fatalln(sesh, err)
59 }