-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feat/wish #33
Merged
Feat/wish #33
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
904d1ab
add support for stdin
9e82096
update languages to allow lowercases names
05e2ad9
Merge branch 'main' into feat/stdin-input
puria 6da2bb0
Merge branch 'main' into feat/stdin-input
FilippoTrotter 96b7de4
fix merging error
0c8cee3
fix test
4b17ad5
refactor with cobra
c941a61
update build action
f5b4c6f
solve merge conflicts
dc02115
add some comments
af4da50
better error handling
577ba06
add ssh connection to remote folder
1b36af2
add possibility to specify port
476d03c
update first wish version
e234c18
add tui remote support
7d3dd52
fix label view
6e8e889
better pty handling
6d5027f
remove debug print
8ce0910
add a better error handling
5e1e26d
add labels case
077e840
fix applyChanges
d729dc3
remove log.fatal for better error handling
22baa5f
feat: support zenroom and slangroom contracts
puria 0bba042
add tui flag
7be875d
fix: error handling
d7cf4e4
add no selected file check
1144458
fix: label request view
8ece73a
remove possibility to add non character keys as input
371c869
fix: remove wrong exit key
f0e3fdf
fix: c language
f5f10c7
feat: add .cs extension support
1162807
feat: add go back possibility
d6ddccf
fix: view functions
2421789
add unit tests for option and label models
957b226
add unit tests for file selector model
db293bd
add some extra test
3e17b26
fix: remove mode selection for single file
9666295
add main model unit tests
fe7167e
refactor
0d44323
refactor
13c1c4d
Resolve merge conflict
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 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/creack/pty" | ||
"github.com/dyne/tgcom/utils/server" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/term" | ||
) | ||
|
||
// serverCmd represents the server command | ||
var serverCmd = &cobra.Command{ | ||
Use: "server", | ||
Short: "Start the SSH server", | ||
Long: `Start the SSH server that allows remote interactions with tgcom.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
server.StartServer() | ||
}, | ||
} | ||
|
||
func init() { | ||
// Register the server command | ||
rootCmd.AddCommand(serverCmd) | ||
} | ||
|
||
func executeRemoteCommand(remotePath string) { | ||
parts := strings.SplitN(remotePath, "@", 2) | ||
if len(parts) != 2 { | ||
fmt.Println("Invalid format. Usage: tgcom -w user@remote:/path/folder") | ||
os.Exit(1) | ||
} | ||
|
||
userHost := parts[0] | ||
pathParts := strings.SplitN(parts[1], ":", 2) | ||
if len(pathParts) != 2 { | ||
fmt.Println("Invalid format. Usage: tgcom -w user@remote:/path/folder") | ||
os.Exit(1) | ||
} | ||
|
||
host := pathParts[0] | ||
dir := pathParts[1] | ||
|
||
sshCmd := "ssh" | ||
sshArgs := []string{"-t", "-p", "2222", userHost + "@" + host, "tgcom", dir} | ||
|
||
// Start SSH command with PTY | ||
if err := startSSHWithPTY(sshCmd, sshArgs); err != nil { | ||
log.Fatalf("Error starting SSH with PTY: %v", err) | ||
} | ||
} | ||
|
||
func startSSHWithPTY(cmd string, args []string) error { | ||
// Create SSH command | ||
sshCommand := exec.Command(cmd, args...) | ||
|
||
// Start PTY | ||
ptmx, err := pty.Start(sshCommand) | ||
if err != nil { | ||
return fmt.Errorf("failed to start PTY: %w", err) | ||
} | ||
defer ptmx.Close() | ||
|
||
// Set terminal attributes | ||
oldState, err := term.MakeRaw(int(os.Stdin.Fd())) | ||
if err != nil { | ||
return fmt.Errorf("failed to make terminal raw: %w", err) | ||
} | ||
defer term.Restore(int(os.Stdin.Fd()), oldState) | ||
|
||
// Resize PTY to current terminal size | ||
if err := resizePTY(ptmx); err != nil { | ||
return fmt.Errorf("failed to resize PTY: %w", err) | ||
} | ||
|
||
// Forward input to PTY | ||
go func() { | ||
_, _ = io.Copy(ptmx, os.Stdin) | ||
}() | ||
|
||
// Forward output from PTY | ||
go func() { | ||
_, _ = io.Copy(os.Stdout, ptmx) | ||
}() | ||
|
||
// Handle PTY signals and resize | ||
go func() { | ||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, syscall.SIGWINCH) | ||
for range ch { | ||
if err := resizePTY(ptmx); err != nil { | ||
log.Printf("Error resizing PTY: %v", err) | ||
} | ||
} | ||
}() | ||
|
||
// Wait for SSH command to finish | ||
if err := sshCommand.Wait(); err != nil { | ||
return fmt.Errorf("SSH command failed: %w", err) | ||
} | ||
|
||
// Wait a bit before exiting to ensure all output is processed | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
return nil | ||
} | ||
Comment on lines
+35
to
+114
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. Review of SSH command execution functions: Enhance error handling and resource management.
Refactor the error handling to provide more detailed error messages and ensure resources are cleaned up even when errors occur. |
||
|
||
func resizePTY(ptmx *os.File) error { | ||
size, err := pty.GetsizeFull(os.Stdin) | ||
if err != nil { | ||
return fmt.Errorf("failed to get terminal size: %w", err) | ||
} | ||
if err := pty.Setsize(ptmx, size); err != nil { | ||
return fmt.Errorf("failed to set terminal size: %w", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
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.
Add a comment for the
clearScreen
function.The
clearScreen
function clears the terminal screen.+// clearScreen clears the terminal screen. func clearScreen() {
Committable suggestion