Skip to content

Commit 5257c0e

Browse files
author
mirkobrombin
committed
feat: make hrun usable as a lib
1 parent 6b2b813 commit 5257c0e

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

pkg/client/client.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
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
}

pkg/server/server.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package server
33
import (
44
"bufio"
55
"encoding/json"
6+
"errors"
67
"io"
78
"log"
89
"net"
@@ -17,11 +18,11 @@ import (
1718
"github.com/distrobox/hrun/pkg/structs"
1819
)
1920

20-
func StartServer(allowedCmds []string, socketPath string) {
21+
func StartServer(allowedCmds []string, socketPath string) error {
2122
// Create a listener for the server
2223
listener, err := net.Listen("unix", socketPath)
2324
if err != nil {
24-
panic(err)
25+
return err
2526
}
2627
defer listener.Close()
2728
log.Printf("Server is running on %s\n", listener.Addr())
@@ -42,13 +43,13 @@ func StartServer(allowedCmds []string, socketPath string) {
4243
select {
4344
case <-doneCh:
4445
log.Println("Shutting down server...")
45-
return
46+
return errors.New("server shutdown")
4647
case conn, ok := <-acceptConn(listener):
4748
if !ok {
4849
log.Println("Listener closed, shutting down server...")
49-
return
50+
return errors.New("listener closed")
5051
}
51-
go handleConnection(conn, allowedCmds)
52+
go HandleConnection(conn, allowedCmds)
5253
}
5354
}
5455
}
@@ -67,7 +68,7 @@ func acceptConn(listener net.Listener) <-chan net.Conn {
6768
return ch
6869
}
6970

70-
func handleConnection(conn net.Conn, allowedCmds []string) {
71+
func HandleConnection(conn net.Conn, allowedCmds []string) {
7172
defer conn.Close()
7273

7374
// Read the command from the client

0 commit comments

Comments
 (0)