@@ -2,6 +2,7 @@ package client
22
33import (
44 "encoding/json"
5+ "errors"
56 "fmt"
67 "io"
78 "log"
@@ -15,20 +16,20 @@ import (
1516 "golang.org/x/term"
1617)
1718
18- func StartClient (command []string , socketPath string ) {
19+ func StartClient (command []string , socketPath string ) error {
1920 // Connect to the server
2021 conn , err := net .Dial ("unix" , socketPath )
2122 if err != nil {
2223 log .Println ("Error connecting to the host:" , err )
23- return
24+ return errors . New ( "failed to connect to the server" )
2425 }
2526 defer conn .Close ()
2627
2728 // Get the initial terminal size
2829 initialWidth , initialHeight , err := term .GetSize (int (os .Stdin .Fd ()))
2930 if err != nil {
3031 log .Println ("Error getting initial terminal size:" , err )
31- return
32+ return errors . New ( "failed to get terminal size" )
3233 }
3334
3435 // Send the command to the server
@@ -40,13 +41,13 @@ func StartClient(command []string, socketPath string) {
4041 cmdBytes , err := json .Marshal (cmd )
4142 if err != nil {
4243 log .Println ("Error encoding command:" , err )
43- return
44+ return errors . New ( "failed to encode command" )
4445 }
4546
4647 _ , err = conn .Write (append (cmdBytes , '\n' ))
4748 if err != nil {
4849 log .Println ("Error sending command to the server:" , err )
49- return
50+ return errors . New ( "failed to send command to the server" )
5051 }
5152
5253 // Set up handling for SIGWINCH (window change) signal to detect terminal resize events
@@ -76,7 +77,7 @@ func StartClient(command []string, socketPath string) {
7677 oldState , err := term .MakeRaw (int (os .Stdin .Fd ()))
7778 if err != nil {
7879 log .Println ("Error setting terminal to raw mode:" , err )
79- return
80+ return errors . New ( "failed to set terminal to raw mode" )
8081 }
8182 defer func () { _ = term .Restore (int (os .Stdin .Fd ()), oldState ) }()
8283
@@ -98,4 +99,12 @@ func StartClient(command []string, socketPath string) {
9899 }()
99100
100101 <- doneCh
102+
103+ // Clean up
104+ signal .Stop (sigwinchChan )
105+ close (sigwinchChan )
106+ _ = term .Restore (int (os .Stdin .Fd ()), oldState )
107+ _ = conn .Close ()
108+ log .Println ("Client connection closed" )
109+ return nil
101110}
0 commit comments