Added plugin support for Dequoter
All checks were successful
Release Build / build (push) Successful in 6m59s
All checks were successful
Release Build / build (push) Successful in 6m59s
This commit is contained in:
parent
cb9c05a57b
commit
ac437b7409
5 changed files with 528 additions and 1 deletions
144
plugins.go
Normal file
144
plugins.go
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"ucl.lmika.dev/ucl"
|
||||
)
|
||||
|
||||
const pluginKeyPrefix = "plugin:"
|
||||
|
||||
// PluginFilters is the registry of text filters defined by UCL plugin files.
|
||||
type PluginFilters struct {
|
||||
inst *ucl.Inst
|
||||
filters map[string]TextProcessor
|
||||
errors []error
|
||||
}
|
||||
|
||||
func newPluginFilters() *PluginFilters {
|
||||
return &PluginFilters{
|
||||
filters: make(map[string]TextProcessor),
|
||||
}
|
||||
}
|
||||
|
||||
// Module returns the "d" UCL module, exposing d:filter.
|
||||
func (p *PluginFilters) Module() ucl.Module {
|
||||
return ucl.Module{
|
||||
Name: "d",
|
||||
Builtins: map[string]ucl.BuiltinHandler{
|
||||
"filter": p.filterBuiltin,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// filterBuiltin implements `d:filter LABEL PROC`. PROC receives the filter input as its only
|
||||
// argument and its result becomes the filter output.
|
||||
func (p *PluginFilters) filterBuiltin(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
var (
|
||||
label string
|
||||
proc ucl.Invokable
|
||||
)
|
||||
|
||||
if err := args.Bind(&label, &proc); err != nil {
|
||||
return nil, fmt.Errorf("d:filter: expected LABEL and PROC: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(label) == "" {
|
||||
return nil, errors.New("d:filter: label must not be empty")
|
||||
}
|
||||
if proc.IsNil() {
|
||||
return nil, errors.New("d:filter: PROC must not be nil")
|
||||
}
|
||||
|
||||
p.filters[pluginKeyPrefix+slugify(label)] = TextProcessor{
|
||||
Label: label,
|
||||
Filter: func(ctx context.Context, input string) (TextFilterResponse, error) {
|
||||
res, err := proc.Invoke(ctx, input)
|
||||
if err != nil {
|
||||
return TextFilterResponse{}, err
|
||||
}
|
||||
|
||||
switch v := res.(type) {
|
||||
case nil:
|
||||
return TextFilterResponse{}, errors.New("filter returned no value")
|
||||
case string:
|
||||
return TextFilterResponse{Output: v}, nil
|
||||
default:
|
||||
return TextFilterResponse{Output: fmt.Sprint(v)}, nil
|
||||
}
|
||||
},
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// LoadDir evaluates every *.ucl file found under dir (recursively, in lexical order). Files
|
||||
// that fail to load are recorded in Errors() and skipped; loading continues with the rest.
|
||||
// A missing directory is not an error.
|
||||
func (p *PluginFilters) LoadDir(ctx context.Context, inst *ucl.Inst, dir string) {
|
||||
if _, err := os.Stat(dir); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
p.recordError(fmt.Errorf("%s: %w", dir, err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
p.recordError(fmt.Errorf("%s: %w", path, err))
|
||||
return nil
|
||||
}
|
||||
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".ucl") {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.loadFile(ctx, inst, path); err != nil {
|
||||
p.recordError(fmt.Errorf("%s: %w", path, err))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (p *PluginFilters) loadFile(ctx context.Context, inst *ucl.Inst, path string) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = inst.Eval(ctx, f)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PluginFilters) recordError(err error) {
|
||||
log.Printf("plugin load error: %v", err)
|
||||
p.errors = append(p.errors, err)
|
||||
}
|
||||
|
||||
// Errors returns the errors encountered while loading plugin files.
|
||||
func (p *PluginFilters) Errors() []error {
|
||||
return p.errors
|
||||
}
|
||||
|
||||
// pluginDir returns the plugin directory: $HOME/.config/dequoter/plugins
|
||||
func pluginDir() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, ".config", "dequoter", "plugins"), nil
|
||||
}
|
||||
|
||||
var nonSlugChars = regexp.MustCompile(`[^a-z0-9]+`)
|
||||
|
||||
// slugify converts a label into a key-safe slug: lower-cased, with runs of characters other than
|
||||
// [a-z0-9] replaced by a single hyphen and leading/trailing hyphens removed.
|
||||
func slugify(label string) string {
|
||||
return strings.Trim(nonSlugChars.ReplaceAllString(strings.ToLower(label), "-"), "-")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue