Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ To report bugs and request features, please [open an issue](https://github.com/c

## Installation

To install the latest version use:

```bash
go get cdr.dev/coder-cli/cmd/coder
```

To install a specific [release](https://github.com/cdr/coder-cli/releases):
Install the latest release [here](https://github.com/cdr/coder-cli/releases):

1. Click a release and download the tar file for your operating system (ex: coder-cli-linux-amd64.tar.gz)
2. Extract the `coder` binary from the tar file, ex:
Expand Down
66 changes: 35 additions & 31 deletions cmd/coder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
"go.coder.com/cli"
"go.coder.com/flog"

client "cdr.dev/coder-cli/internal/entclient"
"cdr.dev/coder-cli/wush"
"cdr.dev/wsep"
)

type shellCmd struct {
Expand Down Expand Up @@ -45,29 +44,32 @@ func enableTerminal(fd int) (restore func(), err error) {
}, nil
}

func sendResizeEvents(termfd int, client *wush.Client) {
func sendResizeEvents(ctx context.Context, termfd int, process wsep.Process) {
sigs := make(chan os.Signal, 16)
signal.Notify(sigs, unix.SIGWINCH)

// Limit the frequency of resizes to prevent a stuttering effect.
resizeLimiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1)

for {
for ctx.Err() == nil {
if ctx.Err() != nil {
return
}
width, height, err := terminal.GetSize(termfd)
if err != nil {
flog.Error("get term size: %v", err)
return
}

err = client.Resize(width, height)
err = process.Resize(ctx, uint16(height), uint16(width))
if err != nil {
flog.Error("get term size: %v", err)
flog.Error("set term size: %v", err)
return
}

// Do this last so the first resize is sent.
<-sigs
resizeLimiter.Wait(context.Background())
resizeLimiter.Wait(ctx)
}
}

Expand All @@ -78,6 +80,7 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
var (
envName = fl.Arg(0)
command = fl.Arg(1)
ctx = context.Background()
)

var args []string
Expand All @@ -91,14 +94,16 @@ func (cmd *shellCmd) Run(fl *pflag.FlagSet) {
args = []string{"-c", "exec $(getent passwd $(whoami) | awk -F: '{ print $7 }')"}
}

exitCode, err := runCommand(envName, command, args)
err := runCommand(ctx, envName, command, args)
if exitErr, ok := err.(wsep.ExitError); ok {
os.Exit(exitErr.Code)
}
if err != nil {
flog.Fatal("run command: %v Is it online?", err)
}
os.Exit(exitCode)
}

func runCommand(envName string, command string, args []string) (int, error) {
func runCommand(ctx context.Context, envName string, command string, args []string) error {
var (
entClient = requireAuth()
env = findEnv(entClient, envName)
Expand All @@ -110,38 +115,37 @@ func runCommand(envName string, command string, args []string) (int, error) {
if tty {
restore, err := enableTerminal(termfd)
if err != nil {
return -1, err
return err
}
defer restore()
}

conn, err := entClient.DialWush(
env,
&client.WushOptions{
TTY: tty,
Stdin: true,
}, command, args...)
conn, err := entClient.DialWsep(ctx, env)
if err != nil {
return err
}

execer := wsep.RemoteExecer(conn)
process, err := execer.Start(ctx, wsep.Command{
Command: command,
Args: args,
TTY: tty,
})
if err != nil {
return -1, err
return err
}
ctx := context.Background()

wc := wush.NewClient(ctx, conn)
if tty {
go sendResizeEvents(termfd, wc)
go sendResizeEvents(ctx, termfd, process)
}

go func() {
defer wc.Stdin.Close()
io.Copy(wc.Stdin, os.Stdin)
stdin := process.Stdin()
defer stdin.Close()
io.Copy(stdin, os.Stdin)
}()
go io.Copy(os.Stdout, wc.Stdout)
go io.Copy(os.Stderr, wc.Stderr)

exitCode, err := wc.Wait()
if err != nil {
return -1, err
}
go io.Copy(os.Stdout, process.Stdout())
go io.Copy(os.Stderr, process.Stderr())

return int(exitCode), nil
return process.Wait()
}
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ module cdr.dev/coder-cli
go 1.14

require (
cdr.dev/wsep v0.0.0-20200602025116-5cbe721683df
github.com/fatih/color v1.9.0 // indirect
github.com/gorilla/websocket v1.4.1
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/klauspost/compress v1.10.7 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/rjeczalik/notify v0.9.2
github.com/spf13/pflag v1.0.5
go.coder.com/cli v0.4.0
go.coder.com/flog v0.0.0-20190906214207-47dd47ea0512
golang.org/x/crypto v0.0.0-20200422194213-44a606286825
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4
golang.org/x/sys v0.0.0-20200523222454-059865788121
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
nhooyr.io/websocket v1.8.5
nhooyr.io/websocket v1.8.6
)
Loading