Skip to content

Commit 26123a1

Browse files
authored
Merge pull request #123 from lxfontes/lxfontes/wasmbus
wasmbus: New package
2 parents 6a26345 + 4428b83 commit 26123a1

File tree

101 files changed

+8339
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+8339
-0
lines changed

.github/workflows/wasmbus-go.yaml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
name: wasmbus
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
paths:
7+
- ".github/workflows/wasmbus-go.yaml"
8+
- "x/wasmbus/**"
9+
pull_request:
10+
branches: ["main"]
11+
paths:
12+
- ".github/workflows/wasmbus-go.yaml"
13+
- "x/wasmbus/**"
14+
15+
env:
16+
GOLANGCI_VERSION: "v1.61"
17+
WASH_VERSION: "0.37.0"
18+
19+
permissions:
20+
contents: read
21+
22+
jobs:
23+
lint:
24+
# Context: https://github.com/golangci/golangci-lint-action/blob/v6.1.1/README.md#annotations
25+
permissions:
26+
# Required: allow read access to the content for analysis.
27+
contents: read
28+
# Optional: allow write access to checks to allow the action to annotate code in the PR.
29+
checks: write
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
33+
- uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
34+
with:
35+
version: ${{ env.GOLANGCI_VERSION }}
36+
working-directory: x/wasmbus
37+
38+
- name: Go generate
39+
working-directory: x/wasmbus
40+
run: |
41+
go generate ./...
42+
if ! test -z "$(git status --porcelain)"; then
43+
echo "Go generate modified files. Please run go generate and commit the changes."
44+
git status
45+
exit 1
46+
fi
47+
48+
test:
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
52+
- uses: taiki-e/install-action@ed8c79bccf0b1cb1544a358f81684d3acaa5133f # v2.46.11
53+
with:
54+
tool: ${{ format('wash-cli@{0}', env.WASH_VERSION) }}
55+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
56+
with:
57+
go-version-file: "x/wasmbus/go.mod"
58+
59+
- name: Build
60+
working-directory: x/wasmbus
61+
run: go build -v ./...
62+
63+
# NOTE(lxf): Tests are split into multiple steps to avoid wash race conditions.
64+
65+
- name: wasmbus
66+
working-directory: x/wasmbus
67+
run: go test -cover -v -wash-output
68+
69+
- name: wasmbus/control
70+
working-directory: x/wasmbus/control
71+
run: go test -cover -v -wash-output
72+
73+
- name: wasmbus/wadm
74+
working-directory: x/wasmbus/wadm
75+
run: go test -cover -v -wash-output
76+
77+
- name: wasmbus/events
78+
working-directory: x/wasmbus/events
79+
run: go test -cover -v -wash-output
80+
81+
examples:
82+
# Context: https://github.com/golangci/golangci-lint-action/blob/v6.1.1/README.md#annotations
83+
permissions:
84+
# Required: allow read access to the content for analysis.
85+
contents: read
86+
# Optional: allow write access to checks to allow the action to annotate code in the PR.
87+
checks: write
88+
strategy:
89+
matrix:
90+
example:
91+
- wasmcloud-rpc
92+
runs-on: ubuntu-latest
93+
steps:
94+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
95+
- uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
96+
with:
97+
go-version-file: "./examples/wasmbus/${{ matrix.example }}/go.mod"
98+
- uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
99+
with:
100+
version: ${{ env.GOLANGCI_VERSION }}
101+
working-directory: "./examples/wasmbus/${{ matrix.example }}"
102+
- uses: taiki-e/install-action@ed8c79bccf0b1cb1544a358f81684d3acaa5133f # v2.46.11
103+
with:
104+
tool: ${{ format('wash-cli@{0}', env.WASH_VERSION) }}
105+
106+
- name: Go generate
107+
working-directory: "./examples/wasmbus/${{ matrix.example }}"
108+
run: |
109+
go generate ./...
110+
if ! test -z "$(git status --porcelain)"; then
111+
echo "Go generate modified files. Please run go generate and commit the changes."
112+
git status
113+
exit 1
114+
fi
115+
116+
- name: build
117+
working-directory: "./examples/wasmbus/${{ matrix.example }}"
118+
run: |
119+
go build -v

docs/x/wasmbus/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<meta
4+
name="go-import"
5+
content="go.wasmcloud.dev git https://github.com/wasmCloud/go"
6+
/>
7+
</head>
8+
</html>
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/joho/godotenv"
9+
"github.com/urfave/cli/v3"
10+
"go.wasmcloud.dev/x/wasmbus/control"
11+
)
12+
13+
func configCommand() *cli.Command {
14+
var targetName string
15+
nameArg := &cli.StringArg{
16+
Name: "name",
17+
Destination: &targetName,
18+
Max: 1,
19+
}
20+
return &cli.Command{
21+
Name: "config",
22+
Usage: "Interact with Lattice Config",
23+
Flags: []cli.Flag{},
24+
Commands: []*cli.Command{
25+
{
26+
Name: "get",
27+
Usage: "Get a config",
28+
Action: wrapNamedAction(getConfigCommand, &targetName),
29+
Arguments: []cli.Argument{
30+
nameArg,
31+
},
32+
},
33+
{
34+
Name: "delete",
35+
Usage: "Delete a config",
36+
Action: wrapNamedAction(deleteConfigCommand, &targetName),
37+
Arguments: []cli.Argument{
38+
nameArg,
39+
},
40+
},
41+
{
42+
Name: "put",
43+
Usage: "Put a config",
44+
Action: putConfigCommand,
45+
Flags: []cli.Flag{
46+
&cli.StringFlag{
47+
Name: "name",
48+
Usage: "Name of the config to store",
49+
Required: true,
50+
},
51+
&cli.StringFlag{
52+
Name: "file",
53+
Aliases: []string{"f"},
54+
Usage: "File to read config from, in dotenv format (one KEY=VALUE per line)",
55+
Required: true,
56+
},
57+
},
58+
Arguments: []cli.Argument{
59+
nameArg,
60+
},
61+
},
62+
},
63+
}
64+
}
65+
66+
func deleteConfigCommand(ctx context.Context, cmd *cli.Command, name string) error {
67+
client, err := controlClientFromCommand(cmd)
68+
if err != nil {
69+
return err
70+
}
71+
72+
resp, err := client.ConfigDelete(ctx, &control.ConfigDeleteRequest{
73+
Name: name,
74+
})
75+
if err != nil {
76+
return err
77+
}
78+
if !resp.Success {
79+
return fmt.Errorf("received error response: %s", resp.Message)
80+
}
81+
82+
fmt.Println(titleStyle.Render("⁜", name), "deleted")
83+
84+
return nil
85+
}
86+
87+
func getConfigCommand(ctx context.Context, cmd *cli.Command, name string) error {
88+
client, err := controlClientFromCommand(cmd)
89+
if err != nil {
90+
return err
91+
}
92+
93+
resp, err := client.ConfigGet(ctx, &control.ConfigGetRequest{
94+
Name: name,
95+
})
96+
if err != nil {
97+
return err
98+
}
99+
if !resp.Success {
100+
return fmt.Errorf("received error response: %s", resp.Message)
101+
}
102+
103+
// NOTE(lxf): This feels like a bug in hosts' crates/host/src/wasmbus/mod.rs
104+
if resp.Message == "Configuration not found" {
105+
return fmt.Errorf("configuration not found")
106+
}
107+
108+
config, err := godotenv.Marshal(resp.Response)
109+
if err != nil {
110+
return err
111+
}
112+
fmt.Println(config)
113+
114+
return nil
115+
}
116+
117+
func putConfigCommand(ctx context.Context, cmd *cli.Command) error {
118+
name := cmd.String("name")
119+
client, err := controlClientFromCommand(cmd)
120+
if err != nil {
121+
return err
122+
}
123+
124+
f, err := os.ReadFile(cmd.String("file"))
125+
if err != nil {
126+
return err
127+
}
128+
129+
var values map[string]string
130+
if values, err = godotenv.UnmarshalBytes(f); err != nil {
131+
return err
132+
}
133+
134+
resp, err := client.ConfigPut(ctx, &control.ConfigPutRequest{
135+
Name: name,
136+
Values: values,
137+
})
138+
if err != nil {
139+
return err
140+
}
141+
if !resp.Success {
142+
return fmt.Errorf("received error response: %s", resp.Message)
143+
}
144+
145+
fmt.Println(titleStyle.Render("⁜", name), "stored")
146+
147+
return nil
148+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"time"
9+
10+
"github.com/urfave/cli/v3"
11+
"go.wasmcloud.dev/x/wasmbus"
12+
"go.wasmcloud.dev/x/wasmbus/events"
13+
)
14+
15+
func eventCommand() *cli.Command {
16+
return &cli.Command{
17+
Name: "events",
18+
Usage: "listen for lattice events",
19+
Flags: []cli.Flag{
20+
&cli.StringFlag{
21+
Name: "pattern",
22+
Value: wasmbus.PatternAll,
23+
Usage: "The event pattern to subscribe to",
24+
},
25+
&cli.IntFlag{
26+
Name: "backlog",
27+
Value: wasmbus.NoBackLog,
28+
Usage: "Bus backlog size. Default is no backlog",
29+
},
30+
},
31+
Action: func(ctx context.Context, cmd *cli.Command) error {
32+
bus, err := busFromCommand(cmd)
33+
if err != nil {
34+
return err
35+
}
36+
37+
lattice := cmd.String("lattice")
38+
pattern := cmd.String("pattern")
39+
backlog := cmd.Int("backlog")
40+
41+
callback := func(_ context.Context, ev events.Event) {
42+
jsonEv, err := json.MarshalIndent(ev.BusEvent, "", " ")
43+
if err != nil {
44+
log.Printf("Error marshalling event: %s", err)
45+
return
46+
}
47+
48+
fmt.Println(titleStyle.Render("⁜", ev.CloudEvent.Type()))
49+
renderKv("Time", ev.CloudEvent.Time().Format(time.RFC3339))
50+
renderKv("Source", ev.CloudEvent.Source())
51+
renderKv("Id", ev.CloudEvent.ID())
52+
fmt.Println(string(jsonEv))
53+
}
54+
subscription, err := events.Subscribe(bus, lattice, pattern, int(backlog), events.DiscardErrorsHandler(callback))
55+
if err != nil {
56+
return err
57+
}
58+
defer func() { _ = subscription.Drain() }()
59+
60+
log.Printf("Listening for events on lattice '%s' with pattern '%s'", lattice, pattern)
61+
<-ctx.Done()
62+
log.Printf("Shutting down event listener")
63+
64+
return nil
65+
},
66+
}
67+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module wasmcloud-rpc
2+
3+
go 1.23.3
4+
5+
require (
6+
github.com/charmbracelet/lipgloss v1.0.0
7+
github.com/google/go-cmp v0.6.0
8+
github.com/joho/godotenv v1.5.1
9+
github.com/urfave/cli/v3 v3.0.0-beta1
10+
go.wasmcloud.dev/x/wasmbus v0.1.0
11+
)
12+
13+
require (
14+
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
15+
github.com/charmbracelet/x/ansi v0.4.2 // indirect
16+
github.com/cloudevents/sdk-go/v2 v2.15.2 // indirect
17+
github.com/goccy/go-yaml v1.15.13 // indirect
18+
github.com/google/uuid v1.1.1 // indirect
19+
github.com/json-iterator/go v1.1.10 // indirect
20+
github.com/klauspost/compress v1.17.11 // indirect
21+
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
22+
github.com/mattn/go-isatty v0.0.20 // indirect
23+
github.com/mattn/go-runewidth v0.0.15 // indirect
24+
github.com/minio/highwayhash v1.0.3 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
26+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
27+
github.com/muesli/termenv v0.15.2 // indirect
28+
github.com/nats-io/jwt/v2 v2.7.3 // indirect
29+
github.com/nats-io/nats-server/v2 v2.10.24 // indirect
30+
github.com/nats-io/nats.go v1.38.0 // indirect
31+
github.com/nats-io/nkeys v0.4.9 // indirect
32+
github.com/nats-io/nuid v1.0.1 // indirect
33+
github.com/rivo/uniseg v0.4.7 // indirect
34+
go.uber.org/atomic v1.4.0 // indirect
35+
go.uber.org/multierr v1.1.0 // indirect
36+
go.uber.org/zap v1.10.0 // indirect
37+
golang.org/x/crypto v0.31.0 // indirect
38+
golang.org/x/sys v0.28.0 // indirect
39+
golang.org/x/time v0.8.0 // indirect
40+
)
41+
42+
replace go.wasmcloud.dev/x/wasmbus => ../../../x/wasmbus

0 commit comments

Comments
 (0)