-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: Add Node File Exchange #8407
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,3 +96,94 @@ func makePrivateKeySigner(privateKey []byte, passPhrase []byte) (gossh.Signer, e | |
| } | ||
| return gossh.ParsePrivateKey(privateKey) | ||
| } | ||
|
|
||
| func (c *SSHClient) RunWithStreamOutput(command string, outputCallback func(string)) error { | ||
| session, err := c.Client.NewSession() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create SSH session: %w", err) | ||
| } | ||
| defer session.Close() | ||
|
|
||
| stdout, err := session.StdoutPipe() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to set up stdout pipe: %w", err) | ||
| } | ||
|
|
||
| stderr, err := session.StderrPipe() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to set up stderr pipe: %w", err) | ||
| } | ||
|
|
||
| if err := session.Start(command); err != nil { | ||
| return fmt.Errorf("failed to start command: %w", err) | ||
| } | ||
|
|
||
| stdoutCh := make(chan string, 100) | ||
| stderrCh := make(chan string, 100) | ||
| doneCh := make(chan struct{}) | ||
|
|
||
| go func() { | ||
| buffer := make([]byte, 1024) | ||
| for { | ||
| n, err := stdout.Read(buffer) | ||
| if err != nil { | ||
| close(stdoutCh) | ||
| return | ||
| } | ||
| if n > 0 { | ||
| stdoutCh <- string(buffer[:n]) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| go func() { | ||
| buffer := make([]byte, 1024) | ||
| for { | ||
| n, err := stderr.Read(buffer) | ||
| if err != nil { | ||
| close(stderrCh) | ||
| return | ||
| } | ||
| if n > 0 { | ||
| stderrCh <- string(buffer[:n]) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| go func() { | ||
| for { | ||
| select { | ||
| case stdoutOutput, ok := <-stdoutCh: | ||
| if !ok { | ||
| stdoutCh = nil | ||
| if stderrCh == nil { | ||
| close(doneCh) | ||
| return | ||
| } | ||
| continue | ||
| } | ||
| if outputCallback != nil { | ||
| outputCallback(stdoutOutput) | ||
| } | ||
|
|
||
| case stderrOutput, ok := <-stderrCh: | ||
| if !ok { | ||
| stderrCh = nil | ||
| if stdoutCh == nil { | ||
| close(doneCh) | ||
| return | ||
| } | ||
| continue | ||
| } | ||
| if outputCallback != nil { | ||
| outputCallback(stderrOutput) | ||
| } | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| err = session.Wait() | ||
| <-doneCh | ||
|
|
||
| return err | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code modifications are mostly clean and optimized. Here are some minor points:
Regarding the Overall, this method efficiently handles streaming output from an SSH command, making it suitable for applications that need to process or monitor command outputs. |
||
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.
The provided code snippet includes several improvements over its initial version:
Imports: The
importstatements were separated into sections for readability and clarity.Functionality Enhancement:
ExecWithStreamOutputfunction that takes a command string and an output callback function.bufio.Scanner.Error Handling:
Code Structure:
Here's a summary of the changes with minor formatting adjustments:
These enhancements improve the functionality, maintainability, and performance of the
cmdmodule while addressing potential issues related to command execution and output processing.