2023-05-03 16:05:12 +00:00
|
|
|
package local
|
2023-03-15 17:56:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-14 18:08:59 +00:00
|
|
|
"io"
|
2023-08-23 18:14:02 +00:00
|
|
|
"log/slog"
|
2023-03-15 17:56:38 +00:00
|
|
|
|
2023-05-02 16:44:47 +00:00
|
|
|
"code.icb4dc0.de/buildr/buildr/modules/state"
|
|
|
|
|
2023-04-18 19:30:46 +00:00
|
|
|
"code.icb4dc0.de/buildr/buildr/internal/logging"
|
2023-04-14 18:08:59 +00:00
|
|
|
"code.icb4dc0.de/buildr/buildr/modules"
|
2023-04-11 20:30:48 +00:00
|
|
|
)
|
2023-03-15 17:56:38 +00:00
|
|
|
|
2023-04-14 18:08:59 +00:00
|
|
|
var _ modules.ExecutionContext = (*DefaultExecutionContext)(nil)
|
|
|
|
|
2023-03-15 17:56:38 +00:00
|
|
|
type DefaultExecutionContextOption interface {
|
|
|
|
ApplyToDefaultExecutionContext(ctx *DefaultExecutionContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultExecutionContextOptionFunc func(ctx *DefaultExecutionContext)
|
|
|
|
|
|
|
|
func (f defaultExecutionContextOptionFunc) ApplyToDefaultExecutionContext(ctx *DefaultExecutionContext) {
|
|
|
|
f(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLoggerFactory(factory func() *slog.Logger) defaultExecutionContextOptionFunc {
|
|
|
|
return func(ctx *DefaultExecutionContext) {
|
|
|
|
ctx.loggerFactory = factory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultExecutionContext(
|
|
|
|
ctx context.Context,
|
2023-05-02 16:44:47 +00:00
|
|
|
store state.Store,
|
2023-04-18 16:06:13 +00:00
|
|
|
mod modules.ModuleWithMeta,
|
2023-05-03 16:05:12 +00:00
|
|
|
workingDir, outDir, binDir, logsDir string,
|
|
|
|
logToStdErr bool,
|
2023-03-15 17:56:38 +00:00
|
|
|
opts ...DefaultExecutionContextOption,
|
2023-04-14 18:08:59 +00:00
|
|
|
) (DefaultExecutionContext, error) {
|
2023-03-15 17:56:38 +00:00
|
|
|
execCtx := DefaultExecutionContext{
|
2023-05-03 16:05:12 +00:00
|
|
|
Context: ctx,
|
|
|
|
mod: mod,
|
|
|
|
workingDir: workingDir,
|
|
|
|
outDir: outDir,
|
|
|
|
binDir: binDir,
|
|
|
|
store: store,
|
2023-03-15 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i].ApplyToDefaultExecutionContext(&execCtx)
|
|
|
|
}
|
|
|
|
|
2023-05-03 16:05:12 +00:00
|
|
|
outputSink, err := logging.NewTaskOutputSink(logsDir, logToStdErr, modules.LogFileNameFormatter(mod))
|
2023-04-18 19:30:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return DefaultExecutionContext{}, err
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:30:46 +00:00
|
|
|
execCtx.outputSink = outputSink
|
|
|
|
|
2023-04-14 18:08:59 +00:00
|
|
|
return execCtx, nil
|
2023-03-15 17:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultExecutionContext struct {
|
|
|
|
context.Context
|
2023-06-22 16:06:56 +00:00
|
|
|
outputSink logging.TaskOutputSink
|
|
|
|
mod modules.ModuleWithMeta
|
|
|
|
store state.Store
|
2023-03-15 17:56:38 +00:00
|
|
|
loggerFactory func() *slog.Logger
|
2023-06-22 16:06:56 +00:00
|
|
|
workingDir string
|
|
|
|
outDir string
|
|
|
|
binDir string
|
2023-03-15 17:56:38 +00:00
|
|
|
}
|
2023-05-24 20:10:01 +00:00
|
|
|
|
|
|
|
func (d DefaultExecutionContext) Name() string {
|
|
|
|
return d.mod.Name()
|
|
|
|
}
|
2023-03-15 17:56:38 +00:00
|
|
|
|
2023-05-02 16:44:47 +00:00
|
|
|
func (d DefaultExecutionContext) GetState(ctx context.Context, key string) ([]byte, state.Metadata, error) {
|
|
|
|
k := state.KeyOfStrings(d.mod.Category(), d.mod.Name(), key)
|
|
|
|
return d.store.Get(ctx, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DefaultExecutionContext) SetState(ctx context.Context, key string, value []byte) error {
|
|
|
|
k := state.KeyOfStrings(d.mod.Category(), d.mod.Name(), key)
|
|
|
|
return d.store.Set(ctx, k, value)
|
|
|
|
}
|
|
|
|
|
2023-04-14 18:08:59 +00:00
|
|
|
func (d DefaultExecutionContext) WorkingDir() string {
|
2023-05-03 16:05:12 +00:00
|
|
|
return d.workingDir
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d DefaultExecutionContext) OutDir() string {
|
2023-05-03 16:05:12 +00:00
|
|
|
return d.outDir
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 08:37:08 +00:00
|
|
|
func (d DefaultExecutionContext) BinariesDir() string {
|
2023-05-03 16:05:12 +00:00
|
|
|
return d.binDir
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d DefaultExecutionContext) StdOut() io.Writer {
|
2023-04-18 19:30:46 +00:00
|
|
|
return d.outputSink.StdOut()
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d DefaultExecutionContext) StdErr() io.Writer {
|
2023-04-18 19:30:46 +00:00
|
|
|
return d.outputSink.StdErr()
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 17:56:38 +00:00
|
|
|
func (d DefaultExecutionContext) Logger() *slog.Logger {
|
|
|
|
if d.loggerFactory != nil {
|
|
|
|
return d.loggerFactory()
|
|
|
|
}
|
|
|
|
return slog.Default()
|
|
|
|
}
|
2023-04-14 18:08:59 +00:00
|
|
|
|
|
|
|
func (d DefaultExecutionContext) Close() error {
|
2023-04-18 19:30:46 +00:00
|
|
|
return d.outputSink.Close()
|
2023-04-14 18:08:59 +00:00
|
|
|
}
|