Debian Codegraph @

examples/ssh-form/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "net"
8 "os"
9 "os/signal"
10 "syscall"
11 "time"
12
13 tea "github.com/charmbracelet/bubbletea"
14 "github.com/charmbracelet/huh"
15 "github.com/charmbracelet/lipgloss"
16 "github.com/charmbracelet/log"
17 "github.com/charmbracelet/ssh"
18 "github.com/charmbracelet/wish"
19 "github.com/charmbracelet/wish/activeterm"
20 "github.com/charmbracelet/wish/bubbletea"
21 )
22
23 const (
24 host = "localhost"
25 port = "2222"
26 )
27
28 func main() {
29 s, err := wish.NewServer(
30 wish.WithAddress(net.JoinHostPort(host, port)),
31 wish.WithHostKeyPath(".ssh/id_ed25519"),
32 wish.WithMiddleware(
33 bubbletea.Middleware(teaHandler),
34 activeterm.Middleware(),
35 ),
36 )
37 if err != nil {
38 log.Error("Could not start server", "error", err)
39 }
40
41 done := make(chan os.Signal, 1)
42 signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
43 log.SetReportTimestamp(false)
44 log.Infof("Running form over ssh, connect with:")
45 fmt.Printf("\n ssh %s -p %s\n\n", host, port)
46 go func() {
47 if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
48 log.Error("Could not start server", "error", err)
49 done <- nil
50 }
51 }()
52
53 <-done
54 log.Info("Stopping SSH server")
55 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
56 defer cancel()
57 if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
58 log.Error("Could not stop server", "error", err)
59 }
60 }
61
62 func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
63 form := huh.NewForm(
64 huh.NewGroup(
65 huh.NewInput().Title("Username").Key("username"),
66 huh.NewInput().Title("Password").EchoMode(huh.EchoModePassword),
67 ),
68 )
69 r := bubbletea.MakeRenderer(s)
70 style := r.NewStyle().
71 Border(lipgloss.NormalBorder()).
72 Padding(1, 2).
73 BorderForeground(lipgloss.Color("#444444")).
74 Foreground(lipgloss.Color("#7571F9"))
75
76 custom := huh.ThemeBase()
77 custom.Blurred.Title = r.NewStyle().
78 Foreground(lipgloss.Color("#444"))
79 custom.Blurred.TextInput.Prompt = r.NewStyle().
80 Foreground(lipgloss.Color("#444"))
81 custom.Blurred.TextInput.Text = r.NewStyle().
82 Foreground(lipgloss.Color("#444"))
83 custom.Focused.TextInput.Cursor = r.NewStyle().
84 Foreground(lipgloss.Color("#7571F9"))
85 custom.Focused.Base = r.NewStyle().
86 Padding(0, 1).
87 Border(lipgloss.ThickBorder(), false).
88 BorderLeft(true).
89 BorderForeground(lipgloss.Color("#7571F9"))
90
91 form.WithTheme(custom)
92
93 m := model{form: form, style: style}
94 return m, []tea.ProgramOption{tea.WithAltScreen()}
95 }
96
97 type model struct {
98 form *huh.Form
99 style lipgloss.Style
100 loggedIn bool
101 }
102
103 func (m model) Init() tea.Cmd {
104 if m.form == nil {
105 return nil
106 }
107 return m.form.Init()
108 }
109
110 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
111 var cmds []tea.Cmd
112
113 if m.form != nil {
114 f, cmd := m.form.Update(msg)
115 m.form = f.(*huh.Form)
116 cmds = append(cmds, cmd)
117 }
118
119 m.loggedIn = m.form.State == huh.StateCompleted
120 if m.form.State == huh.StateAborted {
121 return m, tea.Quit
122 }
123
124 switch msg := msg.(type) {
125 case tea.KeyMsg:
126 switch msg.String() {
127 case "ctrl+c":
128 return m, tea.Interrupt
129 case "q":
130 return m, tea.Quit
131 }
132 }
133
134 return m, tea.Batch(cmds...)
135 }
136
137 func (m model) View() string {
138 if m.form == nil {
139 return "Starting..."
140 }
141 if m.loggedIn {
142 return m.style.Render("Welcome, " + m.form.GetString("username") + "!")
143 }
144 return m.form.View()
145 }