@@ -5,13 +5,16 @@ import (
55 "fmt"
66 "io"
77 "log"
8+ "os"
89 "regexp"
910 "strconv"
11+ "strings"
1012 "time"
1113
1214 "github.com/containerd/console"
1315 "github.com/drycc/controller-sdk-go/api"
1416 "github.com/drycc/controller-sdk-go/ps"
17+ "github.com/drycc/workflow-cli/pkg/logging"
1518 "golang.org/x/net/websocket"
1619 yaml "gopkg.in/yaml.v3"
1720)
@@ -45,6 +48,37 @@ func (d *DryccCmd) PsList(appID string, results int) error {
4548 return nil
4649}
4750
51+ // PodLogs returns the logs from an pod.
52+ func (d * DryccCmd ) PsLogs (appID , podID string , lines int , follow bool , container string ) error {
53+ s , appID , err := load (d .ConfigFile , appID )
54+
55+ if err != nil {
56+ return err
57+ }
58+ request := api.PodLogsRequest {
59+ Lines : lines ,
60+ Follow : follow ,
61+ Container : container ,
62+ }
63+ conn , err := ps .Logs (s .Client , appID , podID , request )
64+ if err != nil {
65+ return err
66+ }
67+ defer conn .Close ()
68+ for {
69+ var message string
70+ err := websocket .Message .Receive (conn , & message )
71+ if err != nil {
72+ if err != io .EOF {
73+ log .Printf ("error: %v" , err )
74+ }
75+ break
76+ }
77+ logging .PrintLog (os .Stdout , strings .TrimRight (string (message ), "\n " ))
78+ }
79+ return nil
80+ }
81+
4882// PsList lists an app's processes.
4983func (d * DryccCmd ) PsExec (appID , podID string , tty , stdin bool , command []string ) error {
5084 s , appID , err := load (d .ConfigFile , appID )
@@ -173,12 +207,12 @@ func runRecvTask(conn *websocket.Conn, c console.Console, recvChan, sendChan cha
173207 cancel ()
174208 break
175209 }
176- if message , err := parseChannelMessage (data ); err != nil {
210+ message , err := parseChannelMessage (data )
211+ if err != nil {
177212 cancel ()
178213 break
179- } else {
180- recvChan <- message
181214 }
215+ recvChan <- message
182216 }
183217 }()
184218 go func () {
@@ -272,12 +306,10 @@ func parseChannelMessage(data string) (string, error) {
272306 if value , hasKey := data ["message" ]; hasKey {
273307 if message , ok := value .(string ); ok {
274308 return message , nil
275- } else {
276- return "" , fmt .Errorf ("message is not string, type: %T" , message )
277309 }
278- } else {
279- return "" , nil
310+ return "" , fmt .Errorf ("message is not string, type: %T" , message )
280311 }
312+ return "" , nil
281313 }
282314 return message , nil
283315}
0 commit comments