1package server
 2
 3import (
 4	"fmt"
 5
 6	"github.com/aymanbagabas/go-osc52"
 7	tea "github.com/charmbracelet/bubbletea"
 8	"github.com/charmbracelet/soft-serve/server/backend"
 9	cm "github.com/charmbracelet/soft-serve/server/cmd"
10	"github.com/charmbracelet/soft-serve/server/config"
11	"github.com/charmbracelet/soft-serve/ui"
12	"github.com/charmbracelet/soft-serve/ui/common"
13	"github.com/charmbracelet/ssh"
14	"github.com/charmbracelet/wish"
15	bm "github.com/charmbracelet/wish/bubbletea"
16)
17
18// SessionHandler is the soft-serve bubbletea ssh session handler.
19func SessionHandler(cfg *config.Config) bm.ProgramHandler {
20	return func(s ssh.Session) *tea.Program {
21		pty, _, active := s.Pty()
22		if !active {
23			return nil
24		}
25		cmd := s.Command()
26		initialRepo := ""
27		if len(cmd) == 1 {
28			initialRepo = cmd[0]
29			auth := cfg.Backend.AccessLevel(initialRepo, s.PublicKey())
30			if auth < backend.ReadOnlyAccess {
31				wish.Fatalln(s, cm.ErrUnauthorized)
32				return nil
33			}
34		}
35		envs := s.Environ()
36		envs = append(envs, fmt.Sprintf("TERM=%s", pty.Term))
37		output := osc52.NewOutput(s, envs)
38		c := common.NewCommon(s.Context(), output, pty.Window.Width, pty.Window.Height)
39		c.SetValue(common.ConfigKey, cfg)
40		m := ui.New(c, initialRepo)
41		p := tea.NewProgram(m,
42			tea.WithInput(s),
43			tea.WithOutput(s),
44			tea.WithAltScreen(),
45			tea.WithoutCatchPanics(),
46			tea.WithMouseCellMotion(),
47		)
48		return p
49	}
50}