-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor pkg/trackclipboard to align with SOLID principles #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vldcreation
wants to merge
1
commit into
master
Choose a base branch
from
refactor/trackclipboard-solid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
| 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: | ||
|
|
@@ -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 { | ||
|
||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.