Skip to content

Commit

Permalink
🙈 proto: rename local -> tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed Jul 7, 2024
1 parent 5812f4e commit 149d4bb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
8 changes: 4 additions & 4 deletions pkg/plugin/builtin/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ func (s *Step) Run(ctx context.Context, c *check.S) error {
)

var (
tr = trace.ContextPattern(ctx)
loc = new(Locals)
tr = trace.ContextPattern(ctx)
tags = MakeTags()
)

pre, err := s.p.MakeSensor(ctx, &s.p.Config.Pre, loc.opts()...)
pre, err := s.p.MakeSensor(ctx, &s.p.Config.Pre, tags.opts()...)
if err != nil {
return errors.New("failed to make pre sensor: %w", err)
}

sns, err := s.p.MakeSensor(ctx, &s.Step, loc.opts()...)
sns, err := s.p.MakeSensor(ctx, &s.Step, tags.opts()...)
if err != nil {
return errors.New("failed to make sensor: %w", err)
}
Expand Down
44 changes: 30 additions & 14 deletions pkg/plugin/builtin/event/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,50 @@ package event

import (
"context"
"sync"
"text/template"

"hookt.dev/cmd/pkg/async"
"hookt.dev/cmd/pkg/errors"
"hookt.dev/cmd/pkg/plugin/builtin/event/wire"
"hookt.dev/cmd/pkg/proto"
)

type Locals struct {
async.Map
type Tags struct {
mu sync.Mutex
m map[string]any
}

func (l *Locals) setlocal(name string, value any) any {
l.Map.Store(name, value)
return value
func MakeTags() *Tags {
return &Tags{
m: make(map[string]any),
}
}

func (l *Locals) getlocal(name string) any {
value, _ := l.Map.Load(name)
return value
func (t *Tags) tag(name string, value ...any) (any, error) {
switch len(value) {
case 0:
t.mu.Lock()
value, ok := t.m[name]
t.mu.Unlock()
if !ok {
return nil, errors.New("tag not found: %q", name)
}
return value, nil
case 1:
t.mu.Lock()
t.m[name] = value
t.mu.Unlock()
return value, nil
default:
return nil, errors.New("too many arguments for tag: %q", name)
}
}

func (l *Locals) opts() []proto.TOption {
func (t *Tags) opts() []proto.TOption {
return []proto.TOption{
func(t *template.Template) *template.Template {
return t.Funcs(template.FuncMap{
"setlocal": l.setlocal,
"local": l.getlocal,
func(tmpl *template.Template) *template.Template {
return tmpl.Funcs(template.FuncMap{
"tag": t.tag,
})
},
}
Expand Down

0 comments on commit 149d4bb

Please sign in to comment.