-
Notifications
You must be signed in to change notification settings - Fork 132
RSDK-14081: support Interactive shell on windows #6099
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
Merged
+30
−16
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import ( | |
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/creack/pty" | ||
| "github.com/charmbracelet/x/xpty" | ||
| "go.viam.com/utils" | ||
|
|
||
| "go.viam.com/rdk/logging" | ||
|
|
@@ -48,24 +48,37 @@ type builtIn struct { | |
| func (svc *builtIn) Shell(ctx context.Context, extra map[string]interface{}) ( | ||
| chan<- string, chan<- map[string]interface{}, <-chan shell.Output, error, | ||
| ) { | ||
| if runtime.GOOS == "windows" { | ||
| return nil, nil, nil, errors.New("shell not supported on windows yet; sorry") | ||
| } | ||
|
|
||
| defaultShellPath, ok := os.LookupEnv("SHELL") | ||
| if !ok { | ||
| defaultShellPath = "/bin/sh" | ||
| } | ||
| shellArgs := []string{"-i"} | ||
| shellEnv := []string{"TERM=xterm-256color"} | ||
| if runtime.GOOS == "windows" { | ||
| // powershell when available, else cmd.exe; inherit the parent environment (nil). | ||
| defaultShellPath = "cmd.exe" | ||
| if ps, lookErr := exec.LookPath("powershell.exe"); lookErr == nil { | ||
| defaultShellPath = ps | ||
| } | ||
| shellArgs = nil | ||
| shellEnv = nil | ||
| } | ||
|
|
||
| ctxCancel, cancel := context.WithCancel(ctx) | ||
| //nolint:gosec | ||
| cmd := exec.CommandContext(ctxCancel, defaultShellPath, "-i") | ||
| cmd.Env = []string{"TERM=xterm-256color"} | ||
| f, err := pty.Start(cmd) | ||
| cmd := exec.Command(defaultShellPath, shellArgs...) | ||
|
Member
Author
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. we pass the ctxCancel into xpty.WaitProcess on line 134 because exec can't launch a conPTY process see: golang/go#62708 |
||
| cmd.Env = shellEnv | ||
| // xpty gives a unix pty or a Windows ConPTY behind one interface. | ||
| f, err := xpty.NewPty(80, 24) | ||
| if err != nil { | ||
| cancel() | ||
| return nil, nil, nil, err | ||
| } | ||
| if err := f.Start(cmd); err != nil { | ||
| cancel() | ||
| utils.UncheckedError(f.Close()) | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| var sizeLock sync.Mutex | ||
| lastSet := time.Time{} | ||
|
|
@@ -92,10 +105,7 @@ func (svc *builtIn) Shell(ctx context.Context, extra map[string]interface{}) ( | |
| return | ||
| } | ||
| lastSet = time.Now() | ||
| if err := pty.Setsize(f, &pty.Winsize{ | ||
| Rows: uint16(rows), | ||
| Cols: uint16(cols), | ||
| }); err != nil { | ||
| if err := f.Resize(int(cols), int(rows)); err != nil { | ||
| svc.logger.CErrorw(ctx, "error setting pty window size", "error", err) | ||
| } | ||
| default: | ||
|
|
@@ -121,8 +131,8 @@ func (svc *builtIn) Shell(ctx context.Context, extra map[string]interface{}) ( | |
| utils.PanicCapturingGo(func() { | ||
| defer svc.activeBackgroundWorkers.Done() | ||
| defer cancel() | ||
| if err := cmd.Wait(); err != nil { | ||
| svc.logger.CDebugw(ctx, "error waiting for cmd", "error", err) | ||
| if err := xpty.WaitProcess(ctxCancel, cmd); err != nil { | ||
| svc.logger.CDebugw(ctx, "error waiting for shell process", "error", err) | ||
| } | ||
| if err := f.Close(); err != nil { | ||
| svc.logger.CDebugw(ctx, "error closing pty", "error", err) | ||
|
|
@@ -170,7 +180,7 @@ func (svc *builtIn) Shell(ctx context.Context, extra map[string]interface{}) ( | |
| select { | ||
| case inputData, ok := <-input: | ||
| if ok { | ||
| if _, err := f.WriteString(inputData); err != nil { | ||
| if _, err := f.Write([]byte(inputData)); err != nil { | ||
| svc.logger.CErrorw(ctx, "error writing data", "error", err) | ||
| return | ||
| } | ||
|
|
||
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.
windows doesn't take the -i (interactive shell) or term input using nil here still works