Skip to content

Commit

Permalink
cmd/record: fail gracefully when user tries to create a concurrent
Browse files Browse the repository at this point in the history
recording session
  • Loading branch information
joshi4 committed Jan 7, 2024
1 parent 3fc450d commit 8c47672
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
7 changes: 5 additions & 2 deletions cmd/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ func runRecordCmd(cmd *cobra.Command, args []string) {

commands, err := startRecording()
if err != nil {
panic(err)
savvy_errors.DisplayWithSupportCTA(err)
os.Exit(1)
}

gm := component.NewGenerateRunbookModel(commands, cl)
p := tea.NewProgram(gm)
if _, err := p.Run(); err != nil {
// TODO: fail gracefully. Provider users either a link to view the runbook or a list of their saved commands
fmt.Printf("could not run program: %s\n", err)
os.Exit(1)
}
Expand All @@ -55,6 +57,7 @@ func runRecordCmd(cmd *cobra.Command, args []string) {
m := newDisplayCommandsModel(runbook)
p = tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
// TODO: fail gracefully and provide users a link to view the runbook
fmt.Printf("could not run program: %s\n", err)
os.Exit(1)
}
Expand All @@ -65,7 +68,7 @@ func startRecording() ([]string, error) {
socketPath := "/tmp/savvy-socket"
ss, err := server.NewUnixSocketServer(socketPath)
if err != nil {
panic(err)
return nil, err
}
// TODO: kill this goroutine when the shell exits
go ss.ListenAndServe()
Expand Down
74 changes: 37 additions & 37 deletions server/unix_socket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"errors"
"fmt"
"io"
"net"
Expand All @@ -11,56 +12,55 @@ import (

const socketPath = "/tmp/savvy-socket"


type UnixSocketServer struct {
socketPath string
listener net.Listener
socketPath string
listener net.Listener

mu sync.Mutex
commands []string
mu sync.Mutex
commands []string

closed atomic.Bool
closed atomic.Bool
}

func NewUnixSocketServer(socketPath string) (*UnixSocketServer, error) {
if fileInfo, _ := os.Stat(socketPath); fileInfo != nil {
return nil, fmt.Errorf("Socket file already exists: %s", socketPath)
}
var ErrStartingRecordingSession = errors.New("failed to start recording session")

func NewUnixSocketServer(socketPath string) (*UnixSocketServer, error) {
if fileInfo, _ := os.Stat(socketPath); fileInfo != nil {
return nil, fmt.Errorf("%w: concurrent recording sessions are not supported yet.", ErrStartingRecordingSession)
}

return &UnixSocketServer{socketPath: socketPath}, nil
return &UnixSocketServer{socketPath: socketPath}, nil
}

func (s *UnixSocketServer) Commands() []string {
s.mu.Lock()
defer s.mu.Unlock()
return s.commands
s.mu.Lock()
defer s.mu.Unlock()
return s.commands
}

func (s *UnixSocketServer) Close() error {
if s.listener != nil {
s.closed.Store(true)
return s.listener.Close()
}
return nil
if s.listener != nil {
s.closed.Store(true)
return s.listener.Close()
}
return nil
}


func (s *UnixSocketServer) ListenAndServe() {
listener, err := net.Listen("unix", socketPath)
if err != nil {
fmt.Printf("Failed to listen on Unix socket: %s\n", err)
return
}
s.listener = listener
s.listener = listener

for {
// Accept new connections
conn, err := s.listener.Accept()
if err != nil{
if !s.closed.Load() {
fmt.Printf("Failed to accept connection: %s\n", err)
}
// Accept new connections
conn, err := s.listener.Accept()
if err != nil {
if !s.closed.Load() {
fmt.Printf("Failed to accept connection: %s\n", err)
}
continue
}

Expand All @@ -70,14 +70,14 @@ func (s *UnixSocketServer) ListenAndServe() {
}

func (s *UnixSocketServer) handleConnection(c net.Conn) {
defer c.Close()
bs, err := io.ReadAll(c)
if err != nil {
fmt.Printf("Failed to read from connection: %s\n", err)
return
}
command := string(bs)
s.mu.Lock()
s.commands = append(s.commands, command)
s.mu.Unlock()
defer c.Close()
bs, err := io.ReadAll(c)
if err != nil {
fmt.Printf("Failed to read from connection: %s\n", err)
return
}
command := string(bs)
s.mu.Lock()
s.commands = append(s.commands, command)
s.mu.Unlock()
}

0 comments on commit 8c47672

Please sign in to comment.