-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.go
More file actions
361 lines (331 loc) · 9.23 KB
/
Copy pathsetup.go
File metadata and controls
361 lines (331 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
accent = lipgloss.AdaptiveColor{Light: "#7c3aed", Dark: "#a78bfa"}
dim = lipgloss.AdaptiveColor{Light: "#6b7280", Dark: "#9ca3af"}
titleStyle = lipgloss.NewStyle().Bold(true)
accentStyle = lipgloss.NewStyle().Foreground(accent).Bold(true)
dimStyle = lipgloss.NewStyle().Foreground(dim)
)
type choice struct {
label string
desc string
}
var notifyChoices = []choice{
{"Off", "no desktop pop-ups on this machine"},
{"On", "pop up a desktop notification when an agent needs you"},
}
// exposeChoices lists how pulse can be reached: the two fixed choices, plus
// one per domain/IP "pulse add-domain" has already issued a certificate for
// — recommended for a VPS. Selecting one of those picks it directly; there's
// no separate step, since a domain either has a certificate ready or it
// isn't offered here yet.
func exposeChoices(doms []registeredDomain) []choice {
choices := []choice{
{"Local network", "reachable by other devices on your Wi-Fi / LAN"},
{"Public tunnel", "a public https link, reachable from anywhere"},
}
for _, d := range doms {
status := "valid until " + d.NotAfter.Format("2006-01-02")
if d.Expired {
status = "expired " + d.NotAfter.Format("2006-01-02") + ", renew: pulse add-domain " + d.Target
}
choices = append(choices, choice{d.Target, "Let's Encrypt: " + status})
}
return choices
}
// runWizard interactively confirms saved setup or fills choices not fixed by flags.
// Non-interactive runs reuse saved setup and otherwise keep flag values/defaults.
func runWizard(o opts) opts {
if fi, err := os.Stdin.Stat(); err != nil || fi.Mode()&os.ModeCharDevice == 0 {
return applySavedSetup(o)
}
if saved, err := readSetup(); err == nil {
if hasSetupOverrides(o) {
return applySetup(o, saved)
}
return runSavedWizard(o, saved)
}
return runSetupWizard(o)
}
func runSavedWizard(o opts, saved *setupRecord) opts {
m := wizModel{steps: []string{"saved"}, o: o, input: newWizardInput(), saved: saved}
res, err := tea.NewProgram(m, tea.WithAltScreen()).Run()
if err != nil {
return o
}
fm := res.(wizModel)
if fm.quit {
fmt.Println("pulse: setup cancelled")
os.Exit(0)
}
return fm.o
}
func runSetupWizard(o opts) opts {
steps := setupSteps(o)
if len(steps) == 0 {
return o
}
m := wizModel{steps: steps, o: o, input: newWizardInput()}
m.focusStep()
res, err := tea.NewProgram(m, tea.WithAltScreen()).Run()
if err != nil {
return o
}
fm := res.(wizModel)
if fm.quit {
fmt.Println("pulse: setup cancelled")
os.Exit(0)
}
return fm.o
}
func newWizardInput() textinput.Model {
ti := textinput.New()
ti.CharLimit = 64
ti.Width = 34
return ti
}
func setupSteps(o opts) []string {
var steps []string
if !o.local && !o.tunnelSet && !o.acmeSet {
steps = append(steps, "expose")
}
if !o.passwordSet {
steps = append(steps, "password")
}
if !o.notifySet {
steps = append(steps, "notify")
}
return steps
}
func hasSetupOverrides(o opts) bool {
return o.local || o.tunnelSet || o.acmeSet || o.notifySet || o.passwordSet || o.portSet
}
func applySavedSetup(o opts) opts {
saved, err := readSetup()
if err != nil {
return o
}
return applySetup(o, saved)
}
func applySetup(o opts, saved *setupRecord) opts {
if !o.local && !o.tunnelSet && !o.acmeSet {
o.tunnel = saved.Tunnel
o.acme = saved.Acme
o.domain = saved.Domain
}
if !o.notifySet {
o.localNotify = saved.Notify
}
if !o.passwordSet {
o.passwordHash = saved.PasswordHash
}
return o
}
type wizModel struct {
steps []string
i int
cursor int
o opts
input textinput.Model
quit bool
saved *setupRecord
error string
}
func (m wizModel) Init() tea.Cmd { return textinput.Blink }
func (m *wizModel) focusStep() {
m.input.Reset()
m.cursor = 0
switch m.step() {
case "password":
m.input.Placeholder = "required"
m.input.Focus()
case "expose":
// Auto-fill: land on the previously used choice, not the top of the list.
if m.saved == nil {
break
}
if m.saved.Acme {
for i, d := range registeredDomains() {
if d.Target == m.saved.Domain {
m.cursor = i + 2
break
}
}
} else if m.saved.Tunnel {
m.cursor = 1
}
default:
m.input.Blur()
}
}
func (m wizModel) step() string { return m.steps[m.i] }
// isTextStep reports whether the current step is a free-text input rather
// than a list of choices — cursor movement and up/down navigation don't apply.
func (m wizModel) isTextStep() bool {
return m.step() == "password"
}
// maxCursor is the highest selectable choice index for the current step.
func (m wizModel) maxCursor() int {
if m.step() == "expose" {
return len(exposeChoices(registeredDomains())) - 1
}
return 1
}
func (m wizModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
key, ok := msg.(tea.KeyMsg)
if !ok {
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}
switch key.String() {
case "ctrl+c", "esc":
m.quit = true
return m, tea.Quit
case "up", "k":
if !m.isTextStep() && m.cursor > 0 {
m.cursor--
}
case "down", "j":
if !m.isTextStep() && m.cursor < m.maxCursor() {
m.cursor++
}
case "enter":
return m.commit()
}
if m.isTextStep() {
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}
return m, nil
}
// commit records the current step's answer and advances (or quits on the last).
func (m wizModel) commit() (tea.Model, tea.Cmd) {
m.error = ""
switch m.step() {
case "saved":
if m.cursor == 0 {
m.o = applySetup(m.o, m.saved)
return m, tea.Quit
}
m.steps = setupSteps(m.o)
m.i = 0
m.focusStep()
return m, textinput.Blink
case "expose":
doms := registeredDomains()
switch {
case m.cursor == 1:
m.o.tunnel, m.o.acme = true, false
case m.cursor >= 2:
m.o.tunnel, m.o.acme = false, true
m.o.domain = doms[m.cursor-2].Target
default:
m.o.tunnel, m.o.acme = false, false
}
case "password":
m.o.password = strings.TrimSpace(m.input.Value())
if m.o.password == "" {
m.error = "Enter a login password to continue."
return m, nil
}
case "notify":
m.o.localNotify = m.cursor == 1
}
if m.i == len(m.steps)-1 {
return m, tea.Quit
}
m.i++
m.focusStep()
return m, textinput.Blink
}
func (m wizModel) View() string {
var b strings.Builder
fmt.Fprintf(&b, "%s\n%s\n\n", pulseWordmark(), dimStyle.Render(fmt.Sprintf("setup · %d of %d", m.i+1, len(m.steps))))
switch m.step() {
case "saved":
b.WriteString(titleStyle.Render("Use saved setup?"))
b.WriteString("\n\n")
b.WriteString(m.renderChoices([]choice{{"Start Pulse", "continue with saved setup"}, {"Redo setup", "choose everything again"}}))
case "expose":
b.WriteString(titleStyle.Render("How should pulse be reachable?"))
b.WriteString("\n\n")
b.WriteString(m.renderChoices(exposeChoices(registeredDomains())))
case "password":
b.WriteString(titleStyle.Render("Set a login password"))
b.WriteString("\n")
b.WriteString(dimStyle.Render("Required for the login page; scanning the QR skips it."))
b.WriteString("\n\n ")
b.WriteString(m.input.View())
b.WriteString("\n")
case "notify":
b.WriteString(titleStyle.Render("Desktop notifications on this machine?"))
b.WriteString("\n\n")
b.WriteString(m.renderChoices(notifyChoices))
}
if m.error != "" {
b.WriteString("\n")
b.WriteString(m.error)
b.WriteString("\n")
}
b.WriteString("\n")
b.WriteString(dimStyle.Render("↑/↓ move · enter confirm · esc cancel"))
return lipgloss.NewStyle().Padding(1, 2).Render(b.String())
}
func (m wizModel) renderChoices(choices []choice) string {
var b strings.Builder
for i, c := range choices {
if i == m.cursor {
b.WriteString(accentStyle.Render(" › " + c.label))
b.WriteString(" ")
b.WriteString(dimStyle.Render(c.desc))
b.WriteString("\n")
} else {
b.WriteString(dimStyle.Render(" " + c.label + " " + c.desc))
b.WriteString("\n")
}
}
return b.String()
}
// renderSummary keeps the connection details beside the QR so both scan and
// command options are visible without scrolling. footer is the closing dim
// line — "Ctrl-C quits" while attached, something else when just reprinting
// status for an already-running daemon.
func renderSummary(urls []string, qr, footer string) string {
left := strings.Builder{}
left.WriteString(pulseWordmark())
left.WriteString("\n\n")
left.WriteString(dimStyle.Render("URLs:"))
left.WriteString("\n")
for _, url := range urls {
left.WriteString("- ")
left.WriteString(accentStyle.Render(url))
left.WriteString("\n")
}
left.WriteString("\n")
left.WriteString(dimStyle.Render("Commands:"))
left.WriteString("\n")
left.WriteString("- pulse claude\n- pulse opencode\n- pulse codex\n- pulse ls\n- pulse attach <id>\n- pulse update\n- pulse version\n\n")
left.WriteString(dimStyle.Render(footer))
if qr == "" {
return "\n" + left.String() + "\n"
}
return "\n" + lipgloss.JoinHorizontal(lipgloss.Top, left.String(), " ", "\n"+strings.TrimRight(qr, "\n")) + "\n"
}
// pulseWordmark is a terminal-safe rendering of the Pulse name.
func pulseWordmark() string {
return accentStyle.Render(` ____ _ _ _ ____ _____
| _ \| | | | | / ___|| ____|
| |_) | | | | | \___ \| _|
| __/| |_| | |___ ___) | |___
|_| \___/|_____|____/|_____|`)
}