buildr/internal/rpc/v1/ctx.go
Peter 34c431790e
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build is failing
refactor: use connect-go instead of regular Google gRPC
- support binary name for plugins
- register plugins for container jobs
2023-09-12 18:43:34 +02:00

88 lines
2 KiB
Go

package v1
import (
"context"
"io"
"log/slog"
"os"
"code.icb4dc0.de/buildr/buildr/modules/state"
remotev1 "code.icb4dc0.de/buildr/api/generated/remote/v1"
"code.icb4dc0.de/buildr/buildr/modules"
)
var _ modules.ExecutionContext = (*ContainerExecutionContext)(nil)
func NewContainerExecutionContext(
ctx context.Context,
logger *slog.Logger,
name,
workingDir string,
sender StreamSender[*remotev1.ExecutionServerMessage],
remoteState *RemoteStateClient,
) *ContainerExecutionContext {
return &ContainerExecutionContext{
Context: ctx,
name: name,
remoteState: remoteState,
logger: logger,
workingDir: workingDir,
stdOutWriter: io.MultiWriter(newTaskOutputWriter(
remotev1.TaskOutputSource_TASK_OUTPUT_SOURCE_STDOUT,
sender,
), os.Stdout),
stdErrWriter: io.MultiWriter(newTaskOutputWriter(
remotev1.TaskOutputSource_TASK_OUTPUT_SOURCE_STDERR,
sender,
), os.Stderr),
}
}
type ContainerExecutionContext struct {
context.Context
stdOutWriter io.Writer
stdErrWriter io.Writer
logger *slog.Logger
remoteState *RemoteStateClient
name string
workingDir string
}
func (c ContainerExecutionContext) Name() string {
return c.name
}
func (c ContainerExecutionContext) GetState(ctx context.Context, key string) ([]byte, state.Metadata, error) {
return c.remoteState.GetState(ctx, key)
}
func (c ContainerExecutionContext) SetState(ctx context.Context, key string, value []byte) error {
return c.remoteState.SetState(ctx, key, value)
}
func (c ContainerExecutionContext) WorkingDir() string {
return c.workingDir
}
func (c ContainerExecutionContext) OutDir() string {
return c.workingDir
}
func (c ContainerExecutionContext) BinariesDir() string {
return "/opt/buildr/bin"
}
func (c ContainerExecutionContext) StdOut() io.Writer {
return c.stdOutWriter
}
func (c ContainerExecutionContext) StdErr() io.Writer {
return c.stdErrWriter
}
func (c ContainerExecutionContext) Logger() *slog.Logger {
return c.logger
}