Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pico): logs ssh command #144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 70 additions & 11 deletions pico/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pico

import (
"bufio"
"encoding/json"
"fmt"
"log/slog"
"strings"
Expand Down Expand Up @@ -34,12 +36,13 @@ func getUser(s ssh.Session, dbpool db.DB) (*db.User, error) {
}

type Cmd struct {
User *db.User
Session shared.CmdSession
Log *slog.Logger
Dbpool db.DB
Write bool
Styles common.Styles
User *db.User
SshSession ssh.Session
Session shared.CmdSession
Log *slog.Logger
Dbpool db.DB
Write bool
Styles common.Styles
}

func (c *Cmd) output(out string) {
Expand All @@ -62,6 +65,55 @@ func (c *Cmd) notifications() error {
return nil
}

func (c *Cmd) logs() error {
sshClient, err := shared.CreateSSHClient(
shared.GetEnv("PICO_SENDLOG_ENDPOINT", "send.pico.sh:22"),
shared.GetEnv("PICO_SENDLOG_KEY", "ssh_data/term_info_ed25519"),
shared.GetEnv("PICO_SENDLOG_PASSPHRASE", ""),
shared.GetEnv("PICO_SENDLOG_REMOTE_HOST", "send.pico.sh"),
shared.GetEnv("PICO_SENDLOG_USER", "pico"),
)
if err != nil {
return err
}

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

stdoutPipe, err := session.StdoutPipe()
if err != nil {
return err
}

err = session.Start("sub log-drain -k")
if err != nil {
return err
}

scanner := bufio.NewScanner(stdoutPipe)
for scanner.Scan() {
line := scanner.Text()
parsedData := map[string]any{}

err := json.Unmarshal([]byte(line), &parsedData)
if err != nil {
c.Log.Error("json unmarshal", "err", err)
continue
}

if userId, ok := parsedData["userId"]; ok {
if userId, ok := userId.(string); ok {
if userId == c.User.ID {
wish.Println(c.SshSession, line)
}
}
}
}
return scanner.Err()
}

type CliHandler struct {
DBPool db.DB
Logger *slog.Logger
Expand Down Expand Up @@ -113,18 +165,25 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
}

opts := Cmd{
Session: sesh,
User: user,
Log: log,
Dbpool: dbpool,
Write: false,
Session: sesh,
SshSession: sesh,
User: user,
Log: log,
Dbpool: dbpool,
Write: false,
}

cmd := strings.TrimSpace(args[0])
if len(args) == 1 {
if cmd == "help" {
opts.help()
return
} else if cmd == "logs" {
err = opts.logs()
if err != nil {
wish.Fatalln(sesh, err)
}
return
} else if cmd == "pico+" {
opts.plus()
return
Expand Down
4 changes: 2 additions & 2 deletions shared/sendlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *SendLogWriter) Open() error {
c.Done = make(chan struct{})
c.Messages = make(chan []byte, c.BufferSize)

sshClient, err := createSSHClient(
sshClient, err := CreateSSHClient(
GetEnv("PICO_SENDLOG_ENDPOINT", "send.pico.sh:22"),
GetEnv("PICO_SENDLOG_KEY", "ssh_data/term_info_ed25519"),
GetEnv("PICO_SENDLOG_PASSPHRASE", ""),
Expand Down Expand Up @@ -248,7 +248,7 @@ func (c *SendLogWriter) Reconnect() {
}()
}

func createSSHClient(remoteHost string, keyLocation string, keyPassphrase string, remoteHostname string, remoteUser string) (*ssh.Client, error) {
func CreateSSHClient(remoteHost string, keyLocation string, keyPassphrase string, remoteHostname string, remoteUser string) (*ssh.Client, error) {
if !strings.Contains(remoteHost, ":") {
remoteHost += ":22"
}
Expand Down
Loading