Skip to content

Commit

Permalink
⚙️ hookt/engine: run bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
xojigsx committed Jul 14, 2024
1 parent 1815abc commit 2dc3a1e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
8 changes: 7 additions & 1 deletion hkt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
Expand Down
13 changes: 4 additions & 9 deletions pkg/hookt/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()))
Expand Down
5 changes: 4 additions & 1 deletion pkg/hookt/options.go
Original file line number Diff line number Diff line change
@@ -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...)
}
}
4 changes: 4 additions & 0 deletions pkg/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 2dc3a1e

Please sign in to comment.