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/proto"
 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/wish"
14	bm "github.com/charmbracelet/wish/bubbletea"
15	"github.com/gliderlabs/ssh"
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.AuthRepo(initialRepo, s.PublicKey())
30			if auth < proto.ReadOnlyAccess {
31				wish.Fatalln(s, cm.ErrUnauthorized)
32				return nil
33			}
34		}
35		if cfg.Callbacks != nil {
36			cfg.Callbacks.Tui("new session")
37		}
38		envs := s.Environ()
39		envs = append(envs, fmt.Sprintf("TERM=%s", pty.Term))
40		output := osc52.NewOutput(s, envs)
41		c := common.NewCommon(s.Context(), output, pty.Window.Width, pty.Window.Height)
42		c.SetValue(common.ConfigKey, cfg)
43		m := ui.New(c, initialRepo)
44		p := tea.NewProgram(m,
45			tea.WithInput(s),
46			tea.WithOutput(s),
47			tea.WithAltScreen(),
48			tea.WithoutCatchPanics(),
49			tea.WithMouseCellMotion(),
50		)
51		return p
52	}
53}