chore: renamed messages to events

This commit is contained in:
Hayden Hargreaves 2026-04-14 22:40:14 -07:00
parent c1e06a9dfc
commit 017c177b69
10 changed files with 105 additions and 105 deletions

View File

@ -11,9 +11,9 @@ import (
"termtap.dev/internal/process"
)
func StartProcess(cmd model.Command, addr string, ch chan<- model.Message) (*model.Process, error) {
ch <- model.Message{
Type: model.MessageTypeProcessStarting,
func StartProcess(cmd model.Command, addr string, ch chan<- model.Event) (*model.Process, error) {
ch <- model.Event{
Type: model.EventTypeProcessStarting,
Body: fmt.Sprintf("spawning process '%s'", process.CommandString(cmd)),
}
@ -29,13 +29,13 @@ func StartProcess(cmd model.Command, addr string, ch chan<- model.Message) (*mod
return proc, nil
}
func StopProcess(proc *model.Process, ch chan<- model.Message, sig syscall.Signal) {
func StopProcess(proc *model.Process, ch chan<- model.Event, sig syscall.Signal) {
if proc == nil || proc.Exec == nil || proc.Exec.Process == nil {
return
}
ch <- model.Message{
Type: model.MessageTypeProcessSignaled,
ch <- model.Event{
Type: model.EventTypeProcessSignaled,
Body: fmt.Sprintf("process with pid '%d' is being killed", proc.Exec.Process.Pid),
PID: proc.Exec.Process.Pid,
}
@ -50,15 +50,15 @@ func StopProcess(proc *model.Process, ch chan<- model.Message, sig syscall.Signa
}()
}
func waitForProcessExit(proc *model.Process, ch chan<- model.Message) {
func waitForProcessExit(proc *model.Process, ch chan<- model.Event) {
if proc == nil || proc.Exec == nil {
return
}
if err := proc.Exec.Wait(); err != nil {
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
ch <- model.Message{
Type: model.MessageTypeProcessExited,
ch <- model.Event{
Type: model.EventTypeProcessExited,
Body: fmt.Sprintf("process pid '%d' exited", proc.Exec.Process.Pid),
PID: proc.Exec.Process.Pid,
ExitCode: exitErr.ExitCode(),
@ -67,16 +67,16 @@ func waitForProcessExit(proc *model.Process, ch chan<- model.Message) {
return
}
ch <- model.Message{
Type: model.MessageTypeFatal,
ch <- model.Event{
Type: model.EventTypeFatal,
Body: fmt.Sprintf("%q", err),
}
process.UpdateStatus(proc, false, ch)
return
}
ch <- model.Message{
Type: model.MessageTypeProcessExited,
ch <- model.Event{
Type: model.EventTypeProcessExited,
Body: fmt.Sprintf("process pid '%d' exited", proc.Exec.Process.Pid),
PID: proc.Exec.Process.Pid,
ExitCode: 0,

View File

@ -8,13 +8,13 @@ import (
"termtap.dev/internal/model"
)
func StartProxy(ps *model.ProxyServer, ch chan<- model.Message) {
func StartProxy(ps *model.ProxyServer, ch chan<- model.Event) {
if ps == nil || ps.Server == nil || ps.Listener == nil {
return
}
ch <- model.Message{
Type: model.MessageTypeProxyStarting,
ch <- model.Event{
Type: model.EventTypeProxyStarting,
Body: fmt.Sprintf("proxy server started on %s", (*ps.Listener).Addr().String()),
}
@ -23,8 +23,8 @@ func StartProxy(ps *model.ProxyServer, ch chan<- model.Message) {
return
}
ch <- model.Message{
Type: model.MessageTypeFatal,
ch <- model.Event{
Type: model.EventTypeFatal,
Body: fmt.Sprintf("fatal error in proxy server: %q", err),
}
return

View File

@ -9,16 +9,16 @@ import (
)
type Session struct {
Messages <-chan model.Message
Events <-chan model.Event
msgCh chan model.Message
ch chan model.Event
proxy *model.ProxyServer
proc *model.Process
stopOnce sync.Once
once sync.Once
}
func StartSession(cmd model.Command, addr string) (*Session, error) {
msgs := make(chan model.Message, 256)
msgs := make(chan model.Event, 256)
ps, err := proxy.NewProxyServer(addr, msgs)
if err != nil {
@ -34,8 +34,8 @@ func StartSession(cmd model.Command, addr string) (*Session, error) {
}
return &Session{
Messages: msgs,
msgCh: msgs,
Events: msgs,
ch: msgs,
proxy: ps,
proc: proc,
}, nil
@ -46,8 +46,8 @@ func (s *Session) Stop() {
return
}
s.stopOnce.Do(func() {
StopProcess(s.proc, s.msgCh, syscall.SIGTERM)
proxy.Destroy(s.proxy, s.msgCh)
s.once.Do(func() {
StopProcess(s.proc, s.ch, syscall.SIGTERM)
proxy.Destroy(s.proxy, s.ch)
})
}

View File

@ -26,7 +26,7 @@ func Run(args []string) {
}
defer session.Stop()
if err := tui.Run(session.Messages); err != nil {
if err := tui.Run(session.Events); err != nil {
log.Fatalln(err)
}
}

View File

@ -1,32 +1,32 @@
package model
type MessageType string
type EventType string
const (
MessageTypeSessionStarted MessageType = "SessionStarted"
MessageTypeSessionStopped MessageType = "SessionStopped"
EventTypeSessionStarted EventType = "SessionStarted"
EventTypeSessionStopped EventType = "SessionStopped"
MessageTypeProxyStarting MessageType = "ProxyStarting"
MessageTypeProxyStarted MessageType = "ProxyStarted"
MessageTypeProxyStopped MessageType = "ProxyStopped"
EventTypeProxyStarting EventType = "ProxyStarting"
EventTypeProxyStarted EventType = "ProxyStarted"
EventTypeProxyStopped EventType = "ProxyStopped"
MessageTypeRequestStarted MessageType = "RequestStarted"
MessageTypeRequestFinished MessageType = "RequestFinished"
MessageTypeRequestFailed MessageType = "RequestFailed"
EventTypeRequestStarted EventType = "RequestStarted"
EventTypeRequestFinished EventType = "RequestFinished"
EventTypeRequestFailed EventType = "RequestFailed"
MessageTypeProcessStarting MessageType = "ProcessStarting"
MessageTypeProcessStarted MessageType = "ProcessStarted"
MessageTypeProcessExited MessageType = "ProcessExited"
MessageTypeProcessSignaled MessageType = "ProcessSignaled"
MessageTypeProcessStdout MessageType = "ProcessStdout"
MessageTypeProcessStderr MessageType = "ProcessStderr"
EventTypeProcessStarting EventType = "ProcessStarting"
EventTypeProcessStarted EventType = "ProcessStarted"
EventTypeProcessExited EventType = "ProcessExited"
EventTypeProcessSignaled EventType = "ProcessSignaled"
EventTypeProcessStdout EventType = "ProcessStdout"
EventTypeProcessStderr EventType = "ProcessStderr"
MessageTypeFatal MessageType = "Fatal"
MessageTypeWarn MessageType = "Warn"
EventTypeFatal EventType = "Fatal"
EventTypeWarn EventType = "Warn"
)
type Message struct {
Type MessageType
type Event struct {
Type EventType
Body string
PID int
ExitCode int

View File

@ -15,7 +15,7 @@ func CommandString(c model.Command) string {
return fmt.Sprintf("%s %s", c.Name, strings.Join(c.Args, " "))
}
func NewProcess(cmd model.Command, addr string, ch chan<- model.Message) *model.Process {
func NewProcess(cmd model.Command, addr string, ch chan<- model.Event) *model.Process {
proc := exec.Command(cmd.Name, cmd.Args...)
configureProcessForSignals(proc)
@ -23,24 +23,24 @@ func NewProcess(cmd model.Command, addr string, ch chan<- model.Message) *model.
stdout, err := proc.StdoutPipe()
if err != nil {
ch <- model.Message{
Type: model.MessageTypeWarn,
ch <- model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("could not open stdout pipe: %q", err),
PID: proc.Process.Pid,
}
} else {
go readPipe(stdout, model.MessageTypeProcessStdout, ch)
go readPipe(stdout, model.EventTypeProcessStdout, ch)
}
stderr, err := proc.StderrPipe()
if err != nil {
ch <- model.Message{
Type: model.MessageTypeWarn,
ch <- model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("could not open stderr pipe: %q", err),
PID: proc.Process.Pid,
}
} else {
go readPipe(stderr, model.MessageTypeProcessStderr, ch)
go readPipe(stderr, model.EventTypeProcessStderr, ch)
}
return &model.Process{
@ -66,17 +66,17 @@ func injectEnv(proc *exec.Cmd, addr string) {
proc.Env = append(os.Environ(), injected...)
}
func readPipe(pipe io.Reader, t model.MessageType, ch chan<- model.Message) {
func readPipe(pipe io.Reader, t model.EventType, ch chan<- model.Event) {
scanner := bufio.NewScanner(pipe)
for scanner.Scan() {
ch <- model.Message{
ch <- model.Event{
Type: t,
Body: scanner.Text(),
}
}
}
func UpdateStatus(proc *model.Process, running bool, ch chan<- model.Message) {
func UpdateStatus(proc *model.Process, running bool, ch chan<- model.Event) {
if proc == nil {
return
}
@ -88,18 +88,18 @@ func UpdateStatus(proc *model.Process, running bool, ch chan<- model.Message) {
proc.Running = running
var (
t model.MessageType
t model.EventType
status string
)
if running {
t = model.MessageTypeProcessStarted
t = model.EventTypeProcessStarted
status = "running"
} else {
t = model.MessageTypeProcessExited
t = model.EventTypeProcessExited
status = "stopped"
}
ch <- model.Message{
ch <- model.Event{
Type: t,
Body: fmt.Sprintf("Set process pid '%d' status to %s", proc.Exec.Process.Pid, status),
PID: proc.Exec.Process.Pid,

View File

@ -20,15 +20,15 @@ import (
const maxPreviewBytes = 1024
func proxyHandler(ch chan<- model.Message) http.Handler {
func proxyHandler(ch chan<- model.Event) http.Handler {
transport := http.DefaultTransport
// TODO: This should be wired into the main channel, but that will require a model package
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodConnect {
http.Error(w, "CONNECT is not supported yet", http.StatusNotImplemented)
ch <- model.Message{
Type: model.MessageTypeWarn,
ch <- model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("CONNECT is not supported: %s", req.Host),
}
return
@ -36,8 +36,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
if req.URL.Scheme == "" || req.URL.Host == "" {
http.Error(w, "request must use absolute-form URLs through the proxy", http.StatusBadRequest)
ch <- model.Message{
Type: model.MessageTypeWarn,
ch <- model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("rejected non-proxy request %s %s", req.Method, req.URL.String()),
}
return
@ -60,8 +60,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
requestPreview, err := readAndRestoreBody(&req.Body)
if err != nil {
ch <- model.Message{
Type: model.MessageTypeWarn,
ch <- model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("(%s) failed to read request body", request.ID),
Request: request,
}
@ -80,8 +80,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
request.RequestHeaders = outReq.Header
request.RawURL = outReq.URL.String()
ch <- model.Message{
Type: model.MessageTypeRequestStarted,
ch <- model.Event{
Type: model.EventTypeRequestStarted,
Body: fmt.Sprintf("-> %+v", request),
Request: request,
}
@ -96,8 +96,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
request.Duration = time.Since(start).Round(time.Microsecond)
request.Status = status
ch <- model.Message{
Type: model.MessageTypeRequestFailed,
ch <- model.Event{
Type: model.EventTypeRequestFailed,
Body: fmt.Sprintf("upstream error for %s %s: %v", outReq.Method, outReq.URL.String(), err),
Request: request,
}
@ -107,8 +107,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
responsePreview, err := readAndRestoreBody(&resp.Body)
if err != nil {
ch <- model.Message{
Type: model.MessageTypeWarn,
ch <- model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("(%s) failed to read response body", request.ID),
Request: request,
}
@ -124,8 +124,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
request.Duration = time.Since(start).Round(time.Microsecond)
request.Status = resp.StatusCode
ch <- model.Message{
Type: model.MessageTypeRequestFailed,
ch <- model.Event{
Type: model.EventTypeRequestFailed,
Body: fmt.Sprintf("write response body %s %s: %v", outReq.Method, outReq.URL.String(), err),
}
return
@ -136,8 +136,8 @@ func proxyHandler(ch chan<- model.Message) http.Handler {
request.ResponseHeaders = resp.Header
request.Pending = false
ch <- model.Message{
Type: model.MessageTypeRequestFinished,
ch <- model.Event{
Type: model.EventTypeRequestFinished,
Body: fmt.Sprintf("<- %+v %s", request, formatHeaders(resp.Request.Header)),
Request: request,
}

View File

@ -10,7 +10,7 @@ import (
"termtap.dev/internal/model"
)
func NewProxyServer(addr string, ch chan<- model.Message) (*model.ProxyServer, error) {
func NewProxyServer(addr string, ch chan<- model.Event) (*model.ProxyServer, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
@ -28,14 +28,14 @@ func NewProxyServer(addr string, ch chan<- model.Message) (*model.ProxyServer, e
}
// BUG: Not sure what all this does
func Destroy(ps *model.ProxyServer, ch chan<- model.Message) {
func Destroy(ps *model.ProxyServer, ch chan<- model.Event) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if ps != nil && ps.Server != nil {
_ = ps.Server.Shutdown(ctx)
ch <- model.Message{
Type: model.MessageTypeProxyStopped,
ch <- model.Event{
Type: model.EventTypeProxyStarted,
Body: "proxy server was destroyed",
}
}

View File

@ -13,18 +13,18 @@ const (
maxRequests = 200
)
type appMsg struct {
value model.Message
type EventMsg struct {
value model.Event
}
type modelErrMsg struct {
type ErrMsg struct {
err error
}
type Model struct {
msgCh <-chan model.Message
msgCh <-chan model.Event
events []model.Message
events []model.Event
requestOrder []uuid.UUID
requests map[uuid.UUID]model.Request
@ -32,10 +32,10 @@ type Model struct {
height int
}
func NewModel(msgCh <-chan model.Message) Model {
func NewModel(msgCh <-chan model.Event) Model {
return Model{
msgCh: msgCh,
events: make([]model.Message, 0, maxEvents),
events: make([]model.Event, 0, maxEvents),
requestOrder: make([]uuid.UUID, 0, maxRequests),
requests: map[uuid.UUID]model.Request{},
width: 100,
@ -43,7 +43,7 @@ func NewModel(msgCh <-chan model.Message) Model {
}
}
func Run(msgCh <-chan model.Message) error {
func Run(msgCh <-chan model.Event) error {
p := tea.NewProgram(NewModel(msgCh), tea.WithAltScreen())
_, err := p.Run()
return err
@ -53,13 +53,13 @@ func (m Model) Init() tea.Cmd {
return waitForAppMessage(m.msgCh)
}
func waitForAppMessage(msgCh <-chan model.Message) tea.Cmd {
func waitForAppMessage(msgCh <-chan model.Event) tea.Cmd {
return func() tea.Msg {
msg, ok := <-msgCh
if !ok {
return modelErrMsg{err: fmt.Errorf("event channel closed")}
return ErrMsg{err: fmt.Errorf("event channel closed")}
}
return appMsg{value: msg}
return EventMsg{value: msg}
}
}

View File

@ -22,14 +22,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, nil
case modelErrMsg:
m.events = append(m.events, model.Message{
Type: model.MessageTypeWarn,
case ErrMsg:
m.events = append(m.events, model.Event{
Type: model.EventTypeWarn,
Body: fmt.Sprintf("tui event stream closed: %v", msg.err),
})
return m, nil
case appMsg:
case EventMsg:
m.pushEvent(msg.value)
m.applyMessage(msg.value)
return m, waitForAppMessage(m.msgCh)
@ -38,18 +38,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
func (m *Model) pushEvent(msg model.Message) {
func (m *Model) pushEvent(msg model.Event) {
m.events = append(m.events, msg)
if len(m.events) > maxEvents {
m.events = m.events[len(m.events)-maxEvents:]
}
}
func (m *Model) applyMessage(msg model.Message) {
func (m *Model) applyMessage(msg model.Event) {
switch msg.Type {
case model.MessageTypeRequestStarted:
case model.EventTypeRequestStarted:
m.upsertRequest(msg.Request, true)
case model.MessageTypeRequestFinished, model.MessageTypeRequestFailed:
case model.EventTypeRequestFinished, model.EventTypeRequestFailed:
m.upsertRequest(msg.Request, false)
}
}