Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Adds support for windows #79

Merged
merged 3 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adds support for windows
Change-Id: I2797929cfa72752932e334fc44614095eb1cdea6
  • Loading branch information
cmoog committed Jul 28, 2020
commit 6a5fbc59b5de413cc1d560080458ecb3635dbb7f
Empty file added .github/workflows/test.yaml
Empty file.
1 change: 1 addition & 0 deletions ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build(){
if [[ "$(uname)" == "Darwin" ]]; then
GOOS=linux build
CGO_ENABLED=1 GOOS=darwin build
GOOS=windows GOARCH=386 build
exit 0
fi

Expand Down
39 changes: 39 additions & 0 deletions cmd/coder/resize_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build !windows

package main

import (
"context"
"os"
"os/signal"

"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"

"go.coder.com/flog"
)

func resizeEvents(ctx context.Context, termfd int) chan resizeEvent {
sigs := make(chan os.Signal, 16)
signal.Notify(sigs, unix.SIGWINCH)

events := make(chan resizeEvent)

go func() {
for ctx.Err() == nil {
width, height, err := terminal.GetSize(termfd)
if err != nil {
flog.Error("get term size: %v", err)
break
}
events <- resizeEvent{
height: uint16(height),
width: uint16(width),
}

<-sigs
}
}()

return events
}
46 changes: 46 additions & 0 deletions cmd/coder/resize_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// +build windows

package main

import (
"context"
"time"

"golang.org/x/crypto/ssh/terminal"

"go.coder.com/flog"
)

// windows does have a unix.SIGWINCH equivalent, so we poll the terminal size
// and send resize events when needed
func resizeEvents(ctx context.Context, termfd int) chan resizeEvent {
events := make(chan resizeEvent)
t := time.Tick(time.Millisecond * 100)

go func() {
var lastEvent *resizeEvent

for {
select {
case <-ctx.Done():
break
case <-t:
width, height, err := terminal.GetSize(termfd)
if err != nil {
flog.Error("get term size: %v", err)
break
}
event := resizeEvent{
height: uint16(height),
width: uint16(width),
}
if !event.equal(lastEvent) {
events <- event
}
lastEvent = &event
}
}
}()

return events
}
42 changes: 21 additions & 21 deletions cmd/coder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"context"
"io"
"os"
"os/signal"
"strings"
"time"

"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
"golang.org/x/time/rate"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
Expand Down Expand Up @@ -46,31 +44,33 @@ func enableTerminal(fd int) (restore func(), err error) {
}, nil
}

type resizeEvent struct {
height, width uint16
}

func (s resizeEvent) equal(s2 *resizeEvent) bool {
if s2 == nil {
return false
}
return s.height == s2.height && s.width == s2.width
}

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

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

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 = process.Resize(ctx, uint16(height), uint16(width))
if err != nil {
for {
select {
case newsize := <-events:
err := process.Resize(ctx, newsize.height, newsize.width)
if err != nil {
return
}
_ = resizeLimiter.Wait(ctx)
case <-ctx.Done():
return
}

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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module cdr.dev/coder-cli
go 1.14

require (
cdr.dev/wsep v0.0.0-20200616212001-0613cfe9a4ac
cdr.dev/wsep v0.0.0-20200727194627-13ef1f8df965
github.com/fatih/color v1.9.0 // indirect
github.com/gorilla/websocket v1.4.1
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
cdr.dev/slog v1.3.0 h1:MYN1BChIaVEGxdS7I5cpdyMC0+WfJfK8BETAfzfLUGQ=
cdr.dev/slog v1.3.0/go.mod h1:C5OL99WyuOK8YHZdYY57dAPN1jK2WJlCdq2VP6xeQns=
cdr.dev/wsep v0.0.0-20200615020153-e2b1c576fc40 h1:f369880iSAZ3cXwvbdc9WIyy3FZ4yanusYZjaVHeis4=
cdr.dev/wsep v0.0.0-20200615020153-e2b1c576fc40/go.mod h1:2VKClUml3gfmLez0gBxTJIjSKszpQotc2ZqPdApfK/Y=
cdr.dev/wsep v0.0.0-20200616212001-0613cfe9a4ac h1:rl4O0qfxgNRWBUe5gQu4of2cdsclcpjGYmLQhSCHX7c=
cdr.dev/wsep v0.0.0-20200616212001-0613cfe9a4ac/go.mod h1:2VKClUml3gfmLez0gBxTJIjSKszpQotc2ZqPdApfK/Y=
cdr.dev/wsep v0.0.0-20200727194627-13ef1f8df965 h1:zFpraxgmcgX60oWFs8r1moWFMJ0x945t4moxNb6fjJ8=
cdr.dev/wsep v0.0.0-20200727194627-13ef1f8df965/go.mod h1:N20HJdMn6q9NG7sjxL4uYdBQBGOf8/6psfdMyuJnYw8=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down Expand Up @@ -109,6 +107,8 @@ github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/iamacarpet/go-winpty v1.0.2 h1:jwPVTYrjAHZx6Mcm6K5i9G4opMp5TblEHH5EQCl/Gzw=
github.com/iamacarpet/go-winpty v1.0.2/go.mod h1:/GHKJicG/EVRQIK1IQikMYBakBkhj/3hTjLgdzYsmpI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -240,8 +240,6 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38=
golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down