Skip to content

Commit 224290f

Browse files
committed
feat: support streaming of rule logs
1 parent 7ea959a commit 224290f

7 files changed

Lines changed: 118 additions & 0 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/enapter/enapter-cli
33
go 1.21
44

55
require (
6+
github.com/gorilla/websocket v1.5.3
67
github.com/stretchr/testify v1.9.0
78
github.com/urfave/cli/v2 v2.27.4
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB
22
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
6+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
57
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
68
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
79
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=

internal/app/enaptercli/cmd_base.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/url"
1313
"strings"
1414

15+
"github.com/gorilla/websocket"
1516
"github.com/urfave/cli/v2"
1617
)
1718

@@ -122,6 +123,31 @@ func (c *cmdBase) doHTTPRequest(ctx context.Context, p doHTTPRequestParams) erro
122123
return p.RespProcessor(resp)
123124
}
124125

126+
func (c *cmdBase) dialWebSocket(ctx context.Context, path string) (*websocket.Conn, error) {
127+
url, err := url.Parse(c.apiHost + "/v3" + path)
128+
if err != nil {
129+
return nil, fmt.Errorf("parse url: %w", err)
130+
}
131+
if url.Scheme == "https" {
132+
url.Scheme = "wss"
133+
} else {
134+
url.Scheme = "ws"
135+
}
136+
137+
headers := make(http.Header)
138+
headers.Add("X-Enapter-Auth-Token", c.token)
139+
140+
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, url.String(), headers)
141+
if err != nil {
142+
return nil, fmt.Errorf("dial: %w", err)
143+
}
144+
if resp.StatusCode != http.StatusSwitchingProtocols {
145+
return nil, cli.Exit(parseRespErrorMessage(resp), 1)
146+
}
147+
148+
return conn, nil
149+
}
150+
125151
func (c *cmdBase) defaultRespProcessor(resp *http.Response) error {
126152
if resp.StatusCode != http.StatusOK {
127153
return cli.Exit(parseRespErrorMessage(resp), 1)

internal/app/enaptercli/cmd_rule_engine.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/url"
77

8+
"github.com/gorilla/websocket"
89
"github.com/urfave/cli/v2"
910
)
1011

@@ -33,3 +34,11 @@ func (c *cmdRuleEngine) doHTTPRequest(ctx context.Context, p doHTTPRequestParams
3334
p.Path = path
3435
return c.cmdBase.doHTTPRequest(ctx, p)
3536
}
37+
38+
func (c *cmdRuleEngine) dialWebSocket(ctx context.Context, path string) (*websocket.Conn, error) {
39+
path, err := url.JoinPath("/site/rule_engine", path)
40+
if err != nil {
41+
return nil, fmt.Errorf("join path: %w", err)
42+
}
43+
return c.cmdBase.dialWebSocket(ctx, path)
44+
}

internal/app/enaptercli/cmd_rule_engine_rule.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/url"
77

8+
"github.com/gorilla/websocket"
89
"github.com/urfave/cli/v2"
910
)
1011

@@ -30,6 +31,7 @@ func buildCmdRuleEngineRule() *cli.Command {
3031
buildCmdRuleEngineRuleList(),
3132
buildCmdRuleEngineRuleUpdate(),
3233
buildCmdRuleEngineRuleUpdateScript(),
34+
buildCmdRuleEngineRuleLogsf(),
3335
},
3436
}
3537
}
@@ -42,3 +44,11 @@ func (c *cmdRuleEngineRule) doHTTPRequest(ctx context.Context, p doHTTPRequestPa
4244
p.Path = path
4345
return c.cmdRuleEngine.doHTTPRequest(ctx, p)
4446
}
47+
48+
func (c *cmdRuleEngineRule) dialWebSocket(ctx context.Context, path string) (*websocket.Conn, error) {
49+
path, err := url.JoinPath("/rules", path)
50+
if err != nil {
51+
return nil, fmt.Errorf("join path: %w", err)
52+
}
53+
return c.cmdRuleEngine.dialWebSocket(ctx, path)
54+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package enaptercli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
type cmdRuleEngineRuleLogsf struct {
10+
cmdRuleEngineRule
11+
ruleID string
12+
}
13+
14+
func buildCmdRuleEngineRuleLogsf() *cli.Command {
15+
cmd := &cmdRuleEngineRuleLogsf{}
16+
return &cli.Command{
17+
Name: "logsf",
18+
Usage: "Stream rule logs",
19+
CustomHelpTemplate: cmd.HelpTemplate(),
20+
Flags: cmd.Flags(),
21+
Before: cmd.Before,
22+
Action: func(cliCtx *cli.Context) error {
23+
return cmd.do(cliCtx)
24+
},
25+
}
26+
}
27+
28+
func (c *cmdRuleEngineRuleLogsf) Flags() []cli.Flag {
29+
return append(c.cmdRuleEngineRule.Flags(),
30+
&cli.StringFlag{
31+
Name: "rule-id",
32+
Usage: "Rule ID or slug to update",
33+
Destination: &c.ruleID,
34+
Required: true,
35+
},
36+
)
37+
}
38+
39+
func (c *cmdRuleEngineRuleLogsf) do(cliCtx *cli.Context) error {
40+
conn, err := c.dialWebSocket(cliCtx.Context, c.ruleID+"/logs/ws")
41+
if err != nil {
42+
return fmt.Errorf("dial ws: %w", err)
43+
}
44+
defer conn.Close()
45+
46+
for {
47+
select {
48+
case <-cliCtx.Done():
49+
return nil
50+
default:
51+
}
52+
53+
_, payload, err := conn.ReadMessage()
54+
if err != nil {
55+
return fmt.Errorf("read: %w", err)
56+
}
57+
58+
select {
59+
case <-cliCtx.Done():
60+
return nil
61+
default:
62+
}
63+
64+
payload = append(payload, '\n')
65+
if _, err = c.writer.Write(payload); err != nil {
66+
return fmt.Errorf("write: %w", err)
67+
}
68+
}
69+
}

internal/app/enaptercli/testdata/helps/enapter rule-engine rule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ COMMANDS:
1313
list List rules
1414
update Update a rule
1515
update-script Update the script of a rule
16+
logsf Stream rule logs
1617
help, h Shows a list of commands or help for one command
1718

1819
OPTIONS:

0 commit comments

Comments
 (0)