-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TUI + server + CLI rewrite (#33)
## Summary - **New Features** - Introduced `tgcom`, a command-line tool for commenting and uncommenting code in various languages. - Added SSH server support for remote interactions with `tgcom`. - **Enhancements** - Improved terminal user interface (TUI) for file selection, mode selection, and action selection. - Functions for modifying files based on comments, with support for dry-run mode and terminal emulation. - **Bug Fixes** - Corrected directory navigation and file selection in the TUI. - Fixed validations for label inputs in the TUI. - **Tests** - Comprehensive tests added for file selection, label input, mode selection, and server functionality. --------- Co-authored-by: Filippo Trotter <[email protected]> Co-authored-by: Puria Nafisi Azizi <[email protected]>
- Loading branch information
1 parent
d5d46bf
commit 4088ab0
Showing
19 changed files
with
1,973 additions
and
435 deletions.
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 | ||
} | ||
|
||
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.