forked from coder/wsep
-
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.
- Loading branch information
Showing
8 changed files
with
145 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ jobs: | |
- name: test | ||
uses: ./ci/image | ||
with: | ||
args: go test ./... | ||
args: go test ./... |
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
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
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,97 @@ | ||
// +build !windows | ||
|
||
package wsep | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/creack/pty" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
type localProcess struct { | ||
// tty may be nil | ||
tty *os.File | ||
cmd *exec.Cmd | ||
|
||
stdin io.WriteCloser | ||
stdout io.Reader | ||
stderr io.Reader | ||
} | ||
|
||
func (l *localProcess) Resize(_ context.Context, rows, cols uint16) error { | ||
if l.tty == nil { | ||
return nil | ||
} | ||
return pty.Setsize(l.tty, &pty.Winsize{ | ||
Rows: rows, | ||
Cols: cols, | ||
}) | ||
} | ||
|
||
// Start executes the given command locally | ||
func (l LocalExecer) Start(ctx context.Context, c Command) (Process, error) { | ||
var ( | ||
process localProcess | ||
err error | ||
) | ||
process.cmd = exec.CommandContext(ctx, c.Command, c.Args...) | ||
process.cmd.Env = append(os.Environ(), c.Env...) | ||
process.cmd.Dir = c.WorkingDir | ||
|
||
if c.GID != 0 || c.UID != 0 { | ||
process.cmd.SysProcAttr = &syscall.SysProcAttr{ | ||
Credential: &syscall.Credential{}, | ||
} | ||
} | ||
if c.GID != 0 { | ||
process.cmd.SysProcAttr.Credential.Gid = c.GID | ||
} | ||
if c.UID != 0 { | ||
process.cmd.SysProcAttr.Credential.Uid = c.UID | ||
} | ||
|
||
if c.TTY { | ||
// This special WSEP_TTY variable helps debug unexpected TTYs. | ||
process.cmd.Env = append(process.cmd.Env, "WSEP_TTY=true") | ||
process.tty, err = pty.Start(process.cmd) | ||
if err != nil { | ||
return nil, xerrors.Errorf("start command with pty: %w", err) | ||
} | ||
process.stdout = process.tty | ||
process.stderr = ioutil.NopCloser(bytes.NewReader(nil)) | ||
process.stdin = process.tty | ||
} else { | ||
if c.Stdin { | ||
process.stdin, err = process.cmd.StdinPipe() | ||
if err != nil { | ||
return nil, xerrors.Errorf("create pipe: %w", err) | ||
} | ||
} else { | ||
process.stdin = disabledStdinWriter{} | ||
} | ||
|
||
process.stdout, err = process.cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, xerrors.Errorf("create pipe: %w", err) | ||
} | ||
|
||
process.stderr, err = process.cmd.StderrPipe() | ||
if err != nil { | ||
return nil, xerrors.Errorf("create pipe: %w", err) | ||
} | ||
|
||
err = process.cmd.Start() | ||
if err != nil { | ||
return nil, xerrors.Errorf("start command: %w", err) | ||
} | ||
} | ||
|
||
return &process, nil | ||
} |
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,30 @@ | ||
// +build windows | ||
|
||
package wsep | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os/exec" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
type localProcess struct { | ||
// tty may be nil | ||
tty uintptr | ||
cmd *exec.Cmd | ||
|
||
stdin io.WriteCloser | ||
stdout io.Reader | ||
stderr io.Reader | ||
} | ||
|
||
func (l *localProcess) Resize(_ context.Context, rows, cols uint16) error { | ||
return xerrors.Errorf("Windows local execution is not supported") | ||
} | ||
|
||
// Start executes the given command locally | ||
func (l LocalExecer) Start(ctx context.Context, c Command) (Process, error) { | ||
return nil, xerrors.Errorf("Windows local execution is not supported") | ||
} |
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