- commit
- 9ccbc2e
- parent
- e3964cc
- author
- Eric Bower
- date
- 2024-10-04 19:46:15 +0000 UTC
feat(pico): logs ssh command
2 files changed,
+76,
-13
+74,
-11
1@@ -1,6 +1,8 @@
2 package pico
3
4 import (
5+ "bufio"
6+ "encoding/json"
7 "fmt"
8 "log/slog"
9 "strings"
10@@ -34,12 +36,13 @@ func getUser(s ssh.Session, dbpool db.DB) (*db.User, error) {
11 }
12
13 type Cmd struct {
14- User *db.User
15- Session shared.CmdSession
16- Log *slog.Logger
17- Dbpool db.DB
18- Write bool
19- Styles common.Styles
20+ User *db.User
21+ SshSession ssh.Session
22+ Session shared.CmdSession
23+ Log *slog.Logger
24+ Dbpool db.DB
25+ Write bool
26+ Styles common.Styles
27 }
28
29 func (c *Cmd) output(out string) {
30@@ -62,6 +65,59 @@ func (c *Cmd) notifications() error {
31 return nil
32 }
33
34+func (c *Cmd) logs() error {
35+ sshClient, err := shared.CreateSSHClient(
36+ shared.GetEnv("PICO_SENDLOG_ENDPOINT", "send.pico.sh:22"),
37+ shared.GetEnv("PICO_SENDLOG_KEY", "ssh_data/term_info_ed25519"),
38+ shared.GetEnv("PICO_SENDLOG_PASSPHRASE", ""),
39+ shared.GetEnv("PICO_SENDLOG_REMOTE_HOST", "send.pico.sh"),
40+ shared.GetEnv("PICO_SENDLOG_USER", "pico"),
41+ )
42+ if err != nil {
43+ return err
44+ }
45+ defer sshClient.Close()
46+
47+ session, err := sshClient.NewSession()
48+ defer func() {
49+ _ = session.Close()
50+ }()
51+ if err != nil {
52+ return err
53+ }
54+
55+ stdoutPipe, err := session.StdoutPipe()
56+ if err != nil {
57+ return err
58+ }
59+
60+ err = session.Start("sub log-drain -k")
61+ if err != nil {
62+ return err
63+ }
64+
65+ scanner := bufio.NewScanner(stdoutPipe)
66+ for scanner.Scan() {
67+ line := scanner.Text()
68+ parsedData := map[string]any{}
69+
70+ err := json.Unmarshal([]byte(line), &parsedData)
71+ if err != nil {
72+ c.Log.Error("json unmarshal", "err", err)
73+ continue
74+ }
75+
76+ if userName, ok := parsedData["user"]; ok {
77+ if userName, ok := userName.(string); ok {
78+ if userName == c.User.Name {
79+ wish.Println(c.SshSession, line)
80+ }
81+ }
82+ }
83+ }
84+ return scanner.Err()
85+}
86+
87 type CliHandler struct {
88 DBPool db.DB
89 Logger *slog.Logger
90@@ -113,11 +169,12 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
91 }
92
93 opts := Cmd{
94- Session: sesh,
95- User: user,
96- Log: log,
97- Dbpool: dbpool,
98- Write: false,
99+ Session: sesh,
100+ SshSession: sesh,
101+ User: user,
102+ Log: log,
103+ Dbpool: dbpool,
104+ Write: false,
105 }
106
107 cmd := strings.TrimSpace(args[0])
108@@ -125,6 +182,12 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
109 if cmd == "help" {
110 opts.help()
111 return
112+ } else if cmd == "logs" {
113+ err = opts.logs()
114+ if err != nil {
115+ wish.Fatalln(sesh, err)
116+ }
117+ return
118 } else if cmd == "pico+" {
119 opts.plus()
120 return
1@@ -138,7 +138,7 @@ func (c *SendLogWriter) Open() error {
2 c.Done = make(chan struct{})
3 c.Messages = make(chan []byte, c.BufferSize)
4
5- sshClient, err := createSSHClient(
6+ sshClient, err := CreateSSHClient(
7 GetEnv("PICO_SENDLOG_ENDPOINT", "send.pico.sh:22"),
8 GetEnv("PICO_SENDLOG_KEY", "ssh_data/term_info_ed25519"),
9 GetEnv("PICO_SENDLOG_PASSPHRASE", ""),
10@@ -248,7 +248,7 @@ func (c *SendLogWriter) Reconnect() {
11 }()
12 }
13
14-func createSSHClient(remoteHost string, keyLocation string, keyPassphrase string, remoteHostname string, remoteUser string) (*ssh.Client, error) {
15+func CreateSSHClient(remoteHost string, keyLocation string, keyPassphrase string, remoteHostname string, remoteUser string) (*ssh.Client, error) {
16 if !strings.Contains(remoteHost, ":") {
17 remoteHost += ":22"
18 }