Skip to content
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: add wish.Command and wish.Cmd #229

Merged
merged 13 commits into from
Jan 30, 2024
38 changes: 26 additions & 12 deletions cmd.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package wish

import (
"context"
"fmt"
"io"
"os/exec"
"runtime"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/ssh"
)

// Command sets stdin, stdout, and stderr to the current session's PTY slave.
// CommandContext is like Command but includes a context.
//
// If the current session does not have a PTY, it sets them to the session
// itself.
func Command(s ssh.Session, name string, args ...string) *Cmd {
c := exec.Command(name, args...)
func CommandContext(ctx context.Context, s ssh.Session, name string, args ...string) *Cmd {
c := exec.CommandContext(ctx, name, args...)
pty, _, ok := s.Pty()
if !ok {
c.Stdin, c.Stdout, c.Stderr = s, s, s
Expand All @@ -25,6 +27,16 @@ func Command(s ssh.Session, name string, args ...string) *Cmd {
return &Cmd{c, &pty}
}

// Command sets stdin, stdout, and stderr to the current session's PTY slave.
//
// If the current session does not have a PTY, it sets them to the session
// itself.
//
// This will call CommandContext using the session's Context.
func Command(s ssh.Session, name string, args ...string) *Cmd {
return CommandContext(s.Context(), s, name, args...)
}

caarlos0 marked this conversation as resolved.
Show resolved Hide resolved
// Cmd wraps a *exec.Cmd and a ssh.Pty so a command can be properly run.
type Cmd struct {
cmd *exec.Cmd
Expand All @@ -46,15 +58,6 @@ func (c *Cmd) SetDir(dir string) {
c.cmd.Dir = dir
}

// SetStderr conforms with tea.ExecCommand.
func (*Cmd) SetStderr(io.Writer) {}

// SetStdin conforms with tea.ExecCommand.
func (*Cmd) SetStdin(io.Reader) {}

// SetStdout conforms with tea.ExecCommand.
func (*Cmd) SetStdout(io.Writer) {}

// Run runs the program and waits for it to finish.
func (c *Cmd) Run() error {
if c.pty == nil {
Expand Down Expand Up @@ -83,3 +86,14 @@ func (c *Cmd) Run() error {
}
return nil
}

var _ tea.ExecCommand = &Cmd{}

// SetStderr conforms with tea.ExecCommand.
func (*Cmd) SetStderr(io.Writer) {}

// SetStdin conforms with tea.ExecCommand.
func (*Cmd) SetStdin(io.Reader) {}

// SetStdout conforms with tea.ExecCommand.
func (*Cmd) SetStdout(io.Writer) {}
Loading