Skip to content

Commit

Permalink
Update template for golang
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Rockwood <[email protected]>
  • Loading branch information
rockwotj committed Jun 12, 2023
1 parent d44fcad commit 107a7bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
19 changes: 19 additions & 0 deletions src/go/rpk/pkg/cli/wasm/template/golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Redpanda Golang WASM Transform

To get started you first need to have installed [tinygo].

You can get started by modifying the <code>transform.go</code> file
with your logic.

Once you're ready to test out your transform live you need to:

1. Make sure you have a container running via <code>rpk container start</code>
1. Run <code>rpk wasm build</code>
1. Run <code>rpk wasm deploy</code>
1. Then use <code>rpk topic produce</code> and <code>rpk topic consume</code>
to see your transformation live!

⚠️ At the moment the transform you deploy is not persisted to disk, so if you
restart your container you'll need to redeploy.

[tinygo]: https://tinygo.org/getting-started/install/
13 changes: 13 additions & 0 deletions src/go/rpk/pkg/cli/wasm/template/golang/transform.gosrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"github.com/rockwotj/redpanda/src/go/sdk"
)

func main() {
redpanda.OnTransform(onTransform)
}

func onTransform(e redpanda.TransformEvent) ([]redpanda.Record, error) {
return []redpanda.Record{e.Record()}, nil
}
50 changes: 7 additions & 43 deletions src/go/rpk/pkg/cli/wasm/template/golang_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,19 @@
package template

import (
_ "embed"
"strings"
"text/template"
)

const wasmMainFile = `
package main
import (
"io"
"github.com/rockwotj/redpanda/src/go/sdk"
)
func main() {
redpanda.OnTransform(onTransform)
}
// This is an example of the "identity" transform that does nothing.
// You'll want to replace this with something that modifies the key
// or value.
func onTransform(e redpanda.TransformEvent) ([]redpanda.Record, error) {
return []redpanda.Record{e.Record()}, nil
}
`
//go:embed golang/transform.gosrc
var wasmMainFile string

func WasmGoMain() string {
return wasmMainFile
}

func ExecTemplate(filename string, source string, data interface{}) (string, error) {
func execTemplate(filename string, source string, data interface{}) (string, error) {
var tpl strings.Builder
t, err := template.New(filename).Parse(source)
if err != nil {
Expand All @@ -56,30 +39,11 @@ go 1.18
`

func WasmGoModule(name string) (string, error) {
return ExecTemplate("go.mod", wasmGoModFile, name)
return execTemplate("go.mod", wasmGoModFile, name)
}

const wasmGoReadme = `
# Redpanda Golang WASM Transform
To get started you first need to have installed [tinygo].
You can get started by modifying the <code>transform.go</code> file
with your logic.
Once you're ready to test out your transform live you need to:
1. Make sure you have a container running via <code>rpk container start</code>
1. Run <code>rpk wasm build</code>
1. Run <code>rpk wasm deploy [topic]</code>
1. Then use <code>rpk topic produce [topic]</code> and <code>rpk topic consume [topic]</code>
to see your transformation live!
⚠️ At the moment the transform you deploy is applied on all topics in a cluster
and is not persisted to disk, so if you restart your container you'll need to redeploy.
[tinygo]: https://tinygo.org/getting-started/install/
`
//go:embed golang/README.md
var wasmGoReadme string

func WasmGoReadme() string {
return wasmGoReadme
Expand Down

0 comments on commit 107a7bd

Please sign in to comment.