Skip to content

Commit f2299c2

Browse files
committed
chore(workflow-cli): remove gorilla websocket
1 parent 18cbadf commit f2299c2

7 files changed

Lines changed: 56 additions & 91 deletions

File tree

cmd/apps.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/drycc/controller-sdk-go/api"
1112
"github.com/drycc/controller-sdk-go/apps"
1213
"github.com/drycc/controller-sdk-go/domains"
1314
"github.com/drycc/workflow-cli/pkg/git"
1415
"github.com/drycc/workflow-cli/pkg/logging"
1516
"github.com/drycc/workflow-cli/pkg/webbrowser"
1617
"github.com/drycc/workflow-cli/settings"
17-
"github.com/gorilla/websocket"
18+
"golang.org/x/net/websocket"
1819
)
1920

2021
// AppCreate creates an app.
@@ -166,18 +167,21 @@ func (d *DryccCmd) AppLogs(appID string, lines int, follow bool, timeout int) er
166167
if err != nil {
167168
return err
168169
}
169-
170-
conn, err := apps.Logs(s.Client, appID, lines, follow, timeout)
170+
request := api.AppLogsRequest{
171+
Lines: lines,
172+
Follow: follow,
173+
Timeout: timeout,
174+
}
175+
conn, err := apps.Logs(s.Client, appID, request)
171176
if err != nil {
172177
return err
173178
}
174179
defer conn.Close()
175180
for {
176-
_, message, err := conn.ReadMessage()
181+
var message string
182+
err := websocket.Message.Receive(conn, &message)
177183
if err != nil {
178-
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
179-
log.Printf("error: %v", err)
180-
}
184+
log.Printf("error: %v", err)
181185
break
182186
}
183187
logging.PrintLog(os.Stdout, strings.TrimRight(string(message), "\n"))

cmd/ps.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"encoding/base64"
65
"fmt"
76
"io"
87
"log"
@@ -13,7 +12,7 @@ import (
1312
"github.com/containerd/console"
1413
"github.com/drycc/controller-sdk-go/api"
1514
"github.com/drycc/controller-sdk-go/ps"
16-
"github.com/gorilla/websocket"
15+
"golang.org/x/net/websocket"
1716
)
1817

1918
// PsList lists an app's processes.
@@ -43,7 +42,12 @@ func (d *DryccCmd) PsExec(appID, podID string, tty, stdin bool, command []string
4342
if err != nil {
4443
return err
4544
}
46-
conn, err := ps.Exec(s.Client, appID, podID, tty, stdin, command)
45+
request := api.Command{
46+
Tty: tty,
47+
Stdin: stdin,
48+
Command: command,
49+
}
50+
conn, err := ps.Exec(s.Client, appID, podID, request)
4751
if err != nil {
4852
return err
4953
}
@@ -127,18 +131,13 @@ func printProcesses(appID string, input []api.Pods, wOut io.Writer) {
127131
}
128132

129133
func printExec(d *DryccCmd, conn *websocket.Conn) error {
130-
messageType, message, err := conn.ReadMessage()
134+
var message string
135+
err := websocket.Message.Receive(conn, &message)
131136
if err != nil {
132-
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
133-
log.Printf("error: %v", err)
134-
}
137+
log.Printf("error: %v", err)
135138
return nil
136139
}
137-
if messageType == websocket.TextMessage {
138-
d.Printf("%s", string(message))
139-
} else {
140-
d.Printf(base64.StdEncoding.EncodeToString(message))
141-
}
140+
d.Printf("%s", message)
142141
return nil
143142
}
144143

@@ -151,22 +150,22 @@ func streamExec(conn *websocket.Conn, tty bool) error {
151150
}
152151
}
153152

154-
recvQueue := make(chan []byte)
153+
recvQueue := make(chan string)
155154
defer close(recvQueue)
156155
ctx, cancel := context.WithCancel(context.Background())
157156
go func() {
158157
for {
159-
messageType, message, err := conn.ReadMessage()
160-
if err != nil || messageType == websocket.CloseMessage {
158+
var message string
159+
err := websocket.Message.Receive(conn, &message)
160+
if err != nil {
161161
cancel()
162162
break
163-
} else {
164-
recvQueue <- message
165163
}
164+
recvQueue <- message
166165
}
167166
}()
168167

169-
sendQueue := make(chan []byte)
168+
sendQueue := make(chan string)
170169
defer close(sendQueue)
171170
go func() {
172171
buf := make([]byte, 1024)
@@ -178,7 +177,7 @@ func streamExec(conn *websocket.Conn, tty bool) error {
178177
} else if err != nil {
179178
continue
180179
} else {
181-
sendQueue <- buf[:size]
180+
sendQueue <- string(buf[:size])
182181
}
183182
}
184183
}()
@@ -188,11 +187,11 @@ func streamExec(conn *websocket.Conn, tty bool) error {
188187
case <-ctx.Done():
189188
return nil
190189
case message := <-sendQueue:
191-
if err := conn.WriteMessage(websocket.TextMessage, message); err != nil {
190+
if err := websocket.Message.Send(conn, message); err != nil {
192191
return err
193192
}
194193
case message := <-recvQueue:
195-
c.Write(message)
194+
c.Write([]byte(message))
196195
}
197196
}
198197
}

cmd/ps_test.go

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6-
"log"
6+
"io"
77
"net/http"
88
"testing"
99

1010
"github.com/drycc/controller-sdk-go/api"
1111
"github.com/drycc/controller-sdk-go/pkg/time"
1212
"github.com/drycc/workflow-cli/pkg/testutil"
13-
"github.com/gorilla/websocket"
1413
"github.com/stretchr/testify/assert"
14+
"golang.org/x/net/websocket"
1515
)
1616

1717
func TestPrintProcesses(t *testing.T) {
@@ -81,8 +81,6 @@ foo-web-4084101150-c871y up (v2)
8181
`, "output")
8282
}
8383

84-
var upgrader = websocket.Upgrader{} // use default options
85-
8684
func TestPsExec(t *testing.T) {
8785
t.Parallel()
8886
cf, server, err := testutil.NewTestServerAndClient()
@@ -92,30 +90,12 @@ func TestPsExec(t *testing.T) {
9290
defer server.Close()
9391
var b bytes.Buffer
9492
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
95-
96-
server.Mux.HandleFunc("/v2/apps/foo/pods/foo-web-111/exec/", func(w http.ResponseWriter, r *http.Request) {
97-
c, err := upgrader.Upgrade(w, r, nil)
98-
if err != nil {
99-
log.Print("upgrade:", err)
100-
return
101-
}
102-
defer c.Close()
103-
for {
104-
messageType, message, err := c.ReadMessage()
105-
if err != nil {
106-
log.Println("read:", err)
107-
break
108-
}
109-
110-
log.Printf("recv: %s", message)
111-
err = c.WriteMessage(messageType, []byte("# "+"\n"))
112-
if err != nil {
113-
log.Println("write:", err)
114-
break
115-
}
116-
}
117-
})
118-
93+
server.Mux.Handle(
94+
"/v2/apps/foo/pods/foo-web-111/exec/",
95+
websocket.Handler(func(conn *websocket.Conn) {
96+
io.Copy(conn, conn)
97+
}),
98+
)
11999
err = cmdr.PsExec("foo", "foo-web-111", true, false, []string{"/bin/sh"})
120100
assert.NoError(t, err)
121101
}

cmd/resources.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"io"
65
"regexp"
76
"strconv"
87
"strings"
@@ -124,7 +123,7 @@ func (d *DryccCmd) ResourcesList(appID string, results int) error {
124123
if count == 0 {
125124
d.Println("Could not find any resources")
126125
} else {
127-
printResources(d, appID, resources, d.WOut)
126+
printResources(d, appID, resources)
128127
}
129128
return nil
130129
}
@@ -144,7 +143,7 @@ func (d *DryccCmd) ResourceGet(appID, name string) error {
144143
return err
145144
}
146145
// todo format data json to yaml
147-
printResourceDetail(d, appID, resource, d.WOut)
146+
printResourceDetail(d, appID, resource)
148147
//d.Println(resource)
149148
return nil
150149
}
@@ -251,9 +250,8 @@ func (d *DryccCmd) ResourceUnbind(appID string, name string) error {
251250
}
252251

253252
// printResources format Resources data
254-
func printResources(d *DryccCmd, appID string, resources api.Resources, wOut io.Writer) {
255-
256-
fmt.Fprintf(wOut, "=== %s resources\n", appID)
253+
func printResources(d *DryccCmd, appID string, resources api.Resources) {
254+
fmt.Fprintf(d.WOut, "=== %s resources\n", appID)
257255
resourceNames := make([]string, len(resources))
258256

259257
for _, resource := range resources {
@@ -263,11 +261,12 @@ func printResources(d *DryccCmd, appID string, resources api.Resources, wOut io.
263261

264262
for _, resource := range resources {
265263
spaces := strings.Repeat(" ", lenResourceNames-len(resource.Name))
266-
fmt.Fprintf(wOut, "%s%s%s\n", resource.Name, spaces, resource.Plan)
264+
fmt.Fprintf(d.WOut, "%s%s%s\n", resource.Name, spaces, resource.Plan)
265+
267266
}
268267
}
269268

270-
func printResourceDetail(d *DryccCmd, appID string, resource api.Resource, wOut io.Writer) {
269+
func printResourceDetail(d *DryccCmd, appID string, resource api.Resource) {
271270
d.Printf("=== %s resource %s\n", appID, resource.Name)
272271

273272
dataMap := make(map[string]string)

go.mod

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ go 1.20
55
require (
66
github.com/containerd/console v1.0.3
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20230602065836-df0a0b2ab4b0
8+
github.com/drycc/controller-sdk-go v0.0.0-20230711045631-016fac5b12ba
99
github.com/drycc/pkg v0.0.0-20230619083908-711ad2e9f1b8
10-
github.com/gorilla/websocket v1.5.0
1110
github.com/olekukonko/tablewriter v0.0.5
1211
github.com/stretchr/testify v1.8.4
1312
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
13+
golang.org/x/net v0.12.0
1414
gopkg.in/yaml.v3 v3.0.1
1515
sigs.k8s.io/yaml v1.3.0
1616
)
1717

1818
require (
19-
github.com/PuerkitoBio/purell v1.1.1 // indirect
20-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
2119
github.com/davecgh/go-spew v1.1.1 // indirect
22-
github.com/goware/urlx v0.3.2 // indirect
2320
github.com/mattn/go-runewidth v0.0.9 // indirect
2421
github.com/pmezard/go-difflib v1.0.0 // indirect
25-
golang.org/x/net v0.8.0 // indirect
26-
golang.org/x/sys v0.6.0 // indirect
27-
golang.org/x/text v0.8.0 // indirect
22+
golang.org/x/sys v0.10.0 // indirect
2823
gopkg.in/yaml.v2 v2.4.0 // indirect
2924
)

go.sum

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
2-
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
3-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
4-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
51
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
62
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
73
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
84
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
95
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
106
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
11-
github.com/drycc/controller-sdk-go v0.0.0-20230602065836-df0a0b2ab4b0 h1:Ngg3bHE4Cw1qHQt76zjeWcbOKfVFZkwcjkItxw/GkXM=
12-
github.com/drycc/controller-sdk-go v0.0.0-20230602065836-df0a0b2ab4b0/go.mod h1:q6gbIO0f+xUM8mejPgbbUT8pdAYTIb5vugUsoTcbLw0=
7+
github.com/drycc/controller-sdk-go v0.0.0-20230711045631-016fac5b12ba h1:LCpMSZf5WO36gia4wqI1w3ff71klcvnKszlpyy4Py6k=
8+
github.com/drycc/controller-sdk-go v0.0.0-20230711045631-016fac5b12ba/go.mod h1:iMC7o/PFeLnx2y2xuA5TLkaTNeHA7he/bw3eOWv0hdI=
139
github.com/drycc/pkg v0.0.0-20230619083908-711ad2e9f1b8 h1:bTPrdjJTYxMK3T6nYk7Qa8nDbvNNxvNOdI/fgVsiuCg=
1410
github.com/drycc/pkg v0.0.0-20230619083908-711ad2e9f1b8/go.mod h1:y/6vsR0kXRg3Vxj2M/UqApU2uL43KfMDMN2SyvwF0Ek=
15-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
16-
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
17-
github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo=
18-
github.com/goware/urlx v0.3.2/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw=
1911
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
2012
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
2113
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
@@ -26,15 +18,11 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
2618
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
2719
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
2820
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
29-
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
30-
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
31-
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
21+
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
22+
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
3223
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
33-
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
34-
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
36-
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
37-
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
24+
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
25+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3826
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3927
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4028
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

parser/users_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// Create fake implementations of each method that return the argument
1313
// we expect to have called the function (as an error to satisfy the interface).
1414

15-
func (d FakeDryccCmd) UsersList(results int) error {
15+
func (d FakeDryccCmd) UsersList(int) error {
1616
return errors.New("users:list")
1717
}
1818

0 commit comments

Comments
 (0)