1package ssh
2
3import (
4 "strings"
5
6 "github.com/charmbracelet/log"
7 "github.com/charmbracelet/soft-serve/server/backend"
8 "github.com/charmbracelet/soft-serve/server/config"
9 "github.com/charmbracelet/ssh"
10)
11
12// ContextMiddleware adds the config, backend, and logger to the session context.
13func ContextMiddleware(cfg *config.Config, be *backend.Backend, logger *log.Logger) func(ssh.Handler) ssh.Handler {
14 return func(sh ssh.Handler) ssh.Handler {
15 return func(s ssh.Session) {
16 s.Context().SetValue(config.ContextKey, cfg)
17 s.Context().SetValue(backend.ContextKey, be)
18 s.Context().SetValue(log.ContextKey, logger.WithPrefix("ssh"))
19 sh(s)
20 }
21 }
22}
23
24// CommandMiddleware handles git commands and CLI commands.
25// This middleware must be run after the ContextMiddleware.
26func CommandMiddleware(sh ssh.Handler) ssh.Handler {
27 return func(s ssh.Session) {
28 func() {
29 cmdLine := s.Command()
30 _, _, ptyReq := s.Pty()
31 if ptyReq {
32 return
33 }
34
35 switch {
36 case len(cmdLine) >= 2 && strings.HasPrefix(cmdLine[0], "git-"):
37 handleGit(s)
38 default:
39 handleCli(s)
40 }
41 }()
42 sh(s)
43 }
44}