From 2dc3a1e6b3f904467b7e4832ceb47b6b4d763fa8 Mon Sep 17 00:00:00 2001 From: xo Date: Sun, 14 Jul 2024 12:56:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20=20hookt/engine:=20run=20b?= =?UTF-8?q?ytes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hkt/main.go | 8 +++++++- pkg/hookt/engine.go | 13 ++++--------- pkg/hookt/options.go | 5 ++++- pkg/proto/proto.go | 4 ++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/hkt/main.go b/hkt/main.go index 21d2a71..c7402e9 100644 --- a/hkt/main.go +++ b/hkt/main.go @@ -7,6 +7,7 @@ import ( "os/signal" "hookt.dev/cmd/pkg/command" + "hookt.dev/cmd/pkg/errors" "hookt.dev/cmd/pkg/trace" "github.com/spf13/cobra" @@ -68,7 +69,12 @@ func newRunCommand(ctx context.Context, app *command.App) *cobra.Command { ctx = trace.WithSchedule(ctx, trace.LogSchedule()) } - s, err := app.Engine.Run(ctx, files[0]) + p, err := os.ReadFile(files[0]) + if err != nil { + return errors.New("failed to read file: %w", err) + } + + s, err := app.Engine.Run(ctx, p) if s != nil && len(s.Events) != 0 { app.Render(s.Results()) } diff --git a/pkg/hookt/engine.go b/pkg/hookt/engine.go index 286c32c..315bd76 100644 --- a/pkg/hookt/engine.go +++ b/pkg/hookt/engine.go @@ -3,16 +3,16 @@ package hookt import ( "context" "log/slog" - "os" "strconv" - "github.com/lmittmann/tint" - "golang.org/x/sync/errgroup" "hookt.dev/cmd/pkg/check" "hookt.dev/cmd/pkg/errors" "hookt.dev/cmd/pkg/plugin/builtin" "hookt.dev/cmd/pkg/proto" "hookt.dev/cmd/pkg/trace" + + "github.com/lmittmann/tint" + "golang.org/x/sync/errgroup" ) var plugins []proto.Interface @@ -39,12 +39,7 @@ func New(opts ...func(*Engine)) *Engine { return ngn } -func (e *Engine) Run(ctx context.Context, file string) (*check.S, error) { - p, err := os.ReadFile(file) - if err != nil { - return nil, errors.New("failed to read file: %w", err) - } - +func (e *Engine) Run(ctx context.Context, p []byte) (*check.S, error) { var s check.S ctx = trace.WithPattern(ctx, trace.ContextPattern(ctx).Join(s.Trace())) diff --git a/pkg/hookt/options.go b/pkg/hookt/options.go index 6963c26..81531ab 100644 --- a/pkg/hookt/options.go +++ b/pkg/hookt/options.go @@ -1,6 +1,9 @@ package hookt -func With() func(*Engine) { +import "hookt.dev/cmd/pkg/proto" + +func WithProtoOptions(opts ...func(*proto.P)) func(*Engine) { return func(e *Engine) { + e.p = e.p.With(opts...) } } diff --git a/pkg/proto/proto.go b/pkg/proto/proto.go index b367254..c7fb5a7 100644 --- a/pkg/proto/proto.go +++ b/pkg/proto/proto.go @@ -50,6 +50,10 @@ func New(opts ...func(*P)) *P { t: NewT(), m: make(map[string]Interface), } + return p.With(opts...) +} + +func (p *P) With(opts ...func(*P)) *P { for _, opt := range opts { opt(p) }