Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 49 additions & 25 deletions pkg/trackclipboard/local_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import (
"strings"
)

// ensureLogFile handles the creation and opening of the log file.
// It expands tilde in the path, creates the directory if necessary,
// and opens the file in append mode.
func ensureLogFile(path, name string) (*os.File, error) {
// Expand tilde to home directory if present
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("error getting home directory: %w", err)
}
path = filepath.Join(homeDir, path[2:])
}

// Ensure directory exists
if err := os.MkdirAll(path, 0755); err != nil {
return nil, fmt.Errorf("error creating directory: %w", err)
}

// Construct full file path and open file
fullPath := filepath.Join(path, name)
f, err := os.OpenFile(fullPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
return f, nil
}

type LocalChannel struct {
Path string
Name string
Expand Down Expand Up @@ -40,36 +67,18 @@ func (l *LocalChannel) Send(ctx context.Context, msg string) error {
}

func (l *LocalChannel) processMessages() {
// Expand tilde to home directory if present
path := l.Path
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("Error getting home directory: %v\n", err)
return
}
path = filepath.Join(homeDir, path[2:])
}

// Ensure directory exists
if err := os.MkdirAll(path, 0755); err != nil {
fmt.Printf("Error creating directory: %v\n", err)
return
}

// Construct full file path and open file
fullPath := filepath.Join(path, l.Name)
f, err := os.OpenFile(fullPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := ensureLogFile(l.Path, l.Name)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
fmt.Printf("Error ensuring log file: %v\n", err)
return
}
defer f.Close()
l.file = f // Store the file handle in the struct
Comment on lines +72 to +75
Copy link

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Rather than only printing the error, consider propagating it or implementing a more robust error handling strategy to flag file initialization failures louder.

Copilot uses AI. Check for mistakes.
defer l.file.Close()

for {
select {
case msg := <-l.msgChan:
if _, err := f.Write([]byte(msg + "\n")); err != nil {
if _, err := l.file.Write([]byte(msg + "\n")); err != nil {
fmt.Printf("Error writing to file: %v\n", err)
}
case <-l.doneChan:
Expand All @@ -80,10 +89,25 @@ func (l *LocalChannel) processMessages() {

func (l *LocalChannel) Close() error {
// Signal the processMessages goroutine to stop
close(l.doneChan)
if l.doneChan != nil {
Copy link

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider introducing synchronization (e.g., a WaitGroup) to ensure that the processMessages goroutine has fully terminated before Close() returns, preventing potential race conditions.

Copilot uses AI. Check for mistakes.
close(l.doneChan)
}

// Close message channel after ensuring processMessages has stopped
close(l.msgChan)
if l.msgChan != nil {
close(l.msgChan)
}

// The file will be closed by the defer statement in processMessages.
// If processMessages didn't run (e.g. due to error in ensureLogFile),
// l.file might be nil.
if l.file != nil {
// We might want to ensure a final flush or any other cleanup specific to the file here,
// but the primary closing is handled by defer in processMessages.
// For now, we'll rely on the defer in processMessages.
// If ensureLogFile fails, l.file is nil and processMessages returns early.
// If ensureLogFile succeeds, defer will close it when processMessages returns.
}

return nil
}
Loading