wasi-module-sdk-go/examples/hello_world_go/module/hello_world.go

91 lines
1.9 KiB
Go
Raw Normal View History

2023-05-08 13:21:31 +00:00
package module
import (
2023-05-24 20:11:05 +00:00
"fmt"
2023-08-15 20:10:30 +00:00
"log/slog"
2023-05-24 20:11:05 +00:00
"os"
2023-08-17 15:04:58 +00:00
rpcv1 "code.icb4dc0.de/buildr/api/generated/rpc/v1"
2023-06-29 19:55:40 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/exec"
2023-05-24 20:11:05 +00:00
sdk "code.icb4dc0.de/buildr/wasi-module-sdk-go"
2023-05-08 13:21:31 +00:00
)
2023-08-15 19:46:02 +00:00
var (
_ sdk.Module = (*HelloWorld)(nil)
_ sdk.Helper = (*HelloWorld)(nil)
)
2023-05-08 13:21:31 +00:00
type HelloWorld struct {
Name string
}
func (h HelloWorld) Execute(ctx sdk.ExecutionContext) error {
logger := ctx.Logger()
2023-05-24 20:11:05 +00:00
logger.Info("Executing hello world", slog.String("name", h.Name))
2023-05-08 13:21:31 +00:00
2023-05-24 20:11:05 +00:00
if f, err := os.CreateTemp(ctx.OutDir(), "hello_world.*.txt"); err != nil {
2023-05-08 13:21:31 +00:00
return err
2023-05-24 20:11:05 +00:00
} else if err = f.Close(); err != nil {
2023-05-08 13:21:31 +00:00
return err
}
if foundPath, err := exec.LookPath("go"); err != nil {
return err
} else {
logger.Info("found path for go", slog.String("path", foundPath))
}
cmd := exec.NewCommand("/bin/bash", "-c", `set -ex; echo "Hello process execution!"`)
2023-06-29 19:55:40 +00:00
if err := cmd.Run(); err != nil {
return err
}
2023-05-24 20:11:05 +00:00
fmt.Println("Hello world")
_, _ = fmt.Fprint(ctx.StdOut(), "Hello world via pipeline")
2023-05-08 13:21:31 +00:00
return nil
}
func (HelloWorld) Category() sdk.Category {
return sdk.CategoryTask
}
func (HelloWorld) Type() string {
return "hello_world"
}
2023-08-15 19:46:02 +00:00
func (h HelloWorld) Help() sdk.Help {
return sdk.Help{
Name: "Hello World",
Description: `Example to illustrate how to use the Buildr plugin API.`,
Examples: []sdk.Example{
{
Name: "Simple example",
Description: `well, you know, hello world`,
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "hello_world",
Module: HelloWorld{
Name: "Ted Tester",
},
},
},
{
Name: "Container example",
Description: `well, you know, hello world, but in a container!`,
Spec: sdk.TaskSpec[sdk.Module]{
ModuleName: "hello_world",
Module: HelloWorld{
Name: "Paul Player",
},
Container: &rpcv1.ContainerSpec{
Image: "busybox",
},
},
},
},
}
}