Skip to content

Commit 97794d3

Browse files
committed
feat(workflow-cli): log support follow stream
1 parent c836770 commit 97794d3

7 files changed

Lines changed: 42 additions & 28 deletions

File tree

cmd/apps.go

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

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"regexp"
78
"strings"
@@ -13,6 +14,7 @@ import (
1314
"github.com/drycc/workflow-cli/pkg/logging"
1415
"github.com/drycc/workflow-cli/pkg/webbrowser"
1516
"github.com/drycc/workflow-cli/settings"
17+
"github.com/gorilla/websocket"
1618
)
1719

1820
// AppCreate creates an app.
@@ -158,22 +160,28 @@ func (d *DryccCmd) AppOpen(appID string) error {
158160
}
159161

160162
// AppLogs returns the logs from an app.
161-
func (d *DryccCmd) AppLogs(appID string, lines int) error {
163+
func (d *DryccCmd) AppLogs(appID string, lines int, follow bool, timeout int) error {
162164
s, appID, err := load(d.ConfigFile, appID)
163165

164166
if err != nil {
165167
return err
166168
}
167169

168-
logs, err := apps.Logs(s.Client, appID, lines)
169-
if d.checkAPICompatibility(s.Client, err) != nil {
170+
conn, err := apps.Logs(s.Client, appID, lines, follow, timeout)
171+
if err != nil {
170172
return err
171173
}
172-
173-
for _, log := range strings.Split(strings.TrimRight(logs, `\n`), `\n`) {
174-
logging.PrintLog(os.Stdout, log)
174+
defer conn.Close()
175+
for {
176+
_, message, err := conn.ReadMessage()
177+
if err != nil {
178+
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
179+
log.Printf("error: %v", err)
180+
}
181+
break
182+
}
183+
logging.PrintLog(os.Stdout, strings.TrimRight(string(message), "\n"))
175184
}
176-
177185
return nil
178186
}
179187

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Commander interface {
1414
AppsList(int) error
1515
AppInfo(string) error
1616
AppOpen(string) error
17-
AppLogs(string, int) error
17+
AppLogs(string, int, bool, int) error
1818
AppRun(string, string, []string) error
1919
AppDestroy(string, string) error
2020
AppTransfer(string, string) error

cmd/ps.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"fmt"
66
"io"
7+
"log"
78
"os"
89
"regexp"
910
"strconv"
@@ -143,7 +144,10 @@ func printProcesses(appID string, input []api.Pods, wOut io.Writer) {
143144
func printExec(d *DryccCmd, conn *websocket.Conn) error {
144145
messageType, message, err := conn.ReadMessage()
145146
if err != nil {
146-
return err
147+
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
148+
log.Printf("error: %v", err)
149+
}
150+
return nil
147151
}
148152
if messageType == websocket.TextMessage {
149153
d.Printf("%s", string(message))
@@ -166,6 +170,9 @@ func streamExec(conn *websocket.Conn) error {
166170
for {
167171
messageType, message, err := conn.ReadMessage()
168172
if err != nil {
173+
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
174+
log.Printf("error: %v", err)
175+
}
169176
break
170177
} else if messageType == websocket.CloseMessage {
171178
break

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
7-
github.com/drycc/controller-sdk-go v0.0.0-20220822080925-801edf7edf7b
7+
github.com/drycc/controller-sdk-go v0.0.0-20220826021230-a1b71eb5e437
88
github.com/drycc/pkg v0.0.0-20210826011456-c60b87108840
99
github.com/gorilla/websocket v1.5.0
1010
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
4949
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5050
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
5151
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
52-
github.com/drycc/controller-sdk-go v0.0.0-20220822080925-801edf7edf7b h1:lMOLLsl/qBqiDbZ+vl2MU4H1AR9k3RW9I4DkaTVVdGg=
53-
github.com/drycc/controller-sdk-go v0.0.0-20220822080925-801edf7edf7b/go.mod h1:AWyrmGt7lPQ5Dd2HwO4zyOM/sfuBXVPlpw1Mr5kUZNg=
52+
github.com/drycc/controller-sdk-go v0.0.0-20220826021230-a1b71eb5e437 h1:pOEQvj0mZbWTwSYj/07FTrZ8NYaOLW2uH9IQP4hb378=
53+
github.com/drycc/controller-sdk-go v0.0.0-20220826021230-a1b71eb5e437/go.mod h1:AWyrmGt7lPQ5Dd2HwO4zyOM/sfuBXVPlpw1Mr5kUZNg=
5454
github.com/drycc/pkg v0.0.0-20210826011456-c60b87108840 h1:0OhP9AQ0mh3q0TMxK4PJTPSFwD/wj0xugiaZ3lnLcNA=
5555
github.com/drycc/pkg v0.0.0-20210826011456-c60b87108840/go.mod h1:KX1FLm7Fq6FLCsjjRsgfI/bMQuHZXqYf1ZXU9fzJhDw=
5656
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=

parser/apps.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package parser
22

33
import (
4-
"strconv"
54
"strings"
65

76
docopt "github.com/docopt/docopt-go"
@@ -170,7 +169,11 @@ Options:
170169
-a --app=<app>
171170
the uniquely identifiable name for the application.
172171
-n --lines=<lines>
173-
the number of lines to display
172+
the number of lines to display.
173+
-f --follow
174+
specify if the logs should be streamed.
175+
-t --timeout=<timeout>
176+
the max seconds of follow the log stream.
174177
`
175178

176179
args, err := docopt.Parse(usage, argv, true, "", false, true)
@@ -180,21 +183,17 @@ Options:
180183
}
181184

182185
app := safeGetValue(args, "--app")
183-
184-
linesStr := safeGetValue(args, "--lines")
185-
var lines int
186-
187-
if linesStr == "" {
188-
lines = -1
189-
} else {
190-
lines, err = strconv.Atoi(linesStr)
191-
192-
if err != nil {
193-
return err
194-
}
186+
lines := safeGetInt(args, "--lines")
187+
if lines <= 0 {
188+
lines = 300
189+
}
190+
follow := args["--follow"].(bool)
191+
timeout := safeGetInt(args, "--timeout")
192+
if timeout <= 0 {
193+
timeout = 300
195194
}
196195

197-
return cmdr.AppLogs(app, lines)
196+
return cmdr.AppLogs(app, lines, follow, timeout)
198197
}
199198

200199
func appRun(argv []string, cmdr cmd.Commander) error {

parser/apps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (d FakeDryccCmd) AppOpen(string) error {
2828
return errors.New("apps:open")
2929
}
3030

31-
func (d FakeDryccCmd) AppLogs(string, int) error {
31+
func (d FakeDryccCmd) AppLogs(string, int, bool, int) error {
3232
return errors.New("apps:logs")
3333
}
3434

0 commit comments

Comments
 (0)