Skip to content

Commit 63611ae

Browse files
sguiheuxyesnault
authored andcommitted
feat(cli,sdk): add listen cds events (#3994)
Co-Authored-By: sguiheux <[email protected]>
1 parent 6adf694 commit 63611ae

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

cli/cdsctl/events.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/ovh/cds/cli"
12+
"github.com/ovh/cds/sdk"
13+
"github.com/ovh/cds/sdk/cdsclient"
14+
)
15+
16+
var eventsCmd = cli.Command{
17+
Name: "events",
18+
Short: "Listen CDS Events",
19+
}
20+
21+
func events() *cobra.Command {
22+
return cli.NewCommand(eventsCmd, nil, []*cobra.Command{
23+
cli.NewCommand(eventsListenCmd, eventsListenRun, nil, withAllCommandModifiers()...),
24+
})
25+
}
26+
27+
var eventsListenCmd = cli.Command{
28+
Name: "listen",
29+
Short: "Listen CDS events",
30+
}
31+
32+
func eventsListenRun(v cli.Values) error {
33+
ctx := context.Background()
34+
chanSSE := make(chan cdsclient.SSEvent)
35+
36+
sdk.GoRoutine(ctx, "EventsListenCmd", func(ctx context.Context) {
37+
client.EventsListen(ctx, chanSSE)
38+
})
39+
40+
for {
41+
select {
42+
case <-ctx.Done():
43+
return ctx.Err()
44+
case evt := <-chanSSE:
45+
var e sdk.Event
46+
content, _ := ioutil.ReadAll(evt.Data)
47+
_ = json.Unmarshal(content, &e)
48+
if e.EventType == "" {
49+
continue
50+
}
51+
fmt.Printf("%s: %s %s %s\n", e.EventType, e.ProjectKey, e.WorkflowName, e.Status)
52+
}
53+
}
54+
}

cli/cdsctl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func main() {
3131
signup(),
3232
application(),
3333
environment(),
34+
events(),
3435
pipeline(),
3536
group(),
3637
health(),

sdk/cdsclient/client_events.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cdsclient
2+
3+
import (
4+
"context"
5+
"log"
6+
"time"
7+
)
8+
9+
func (c *client) EventsListen(ctx context.Context, chanSSEvt chan<- SSEvent) {
10+
for ctx.Err() == nil {
11+
if err := c.RequestSSEGet(ctx, "/events", chanSSEvt); err != nil {
12+
log.Println("QueuePolling", err)
13+
}
14+
time.Sleep(1 * time.Second)
15+
}
16+
}

sdk/cdsclient/interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ type EnvironmentVariableClient interface {
130130
EnvironmentVariableUpdate(projectKey string, envName string, variable *sdk.Variable) error
131131
}
132132

133+
// EventsClient listen SSE Events from CDS API
134+
type EventsClient interface {
135+
// Must be run in a go routine
136+
EventsListen(ctx context.Context, chanSSEvt chan<- SSEvent)
137+
}
138+
133139
// DownloadClient exposes download related functions
134140
type DownloadClient interface {
135141
Download() ([]sdk.Download, error)
@@ -336,6 +342,7 @@ type Interface interface {
336342
ConfigUser() (map[string]string, error)
337343
DownloadClient
338344
EnvironmentClient
345+
EventsClient
339346
ExportImportInterface
340347
GroupClient
341348
GRPCPluginsClient

0 commit comments

Comments
 (0)