This repository has been archived on 2024-05-03. You can view files and clone it, but cannot push or open issues or pull requests.
treefmt/internal/format/context.go
2023-12-23 12:50:51 +00:00

37 lines
860 B
Go

package format
import (
"context"
)
const (
formattersKey = "formatters"
completedChKey = "completedCh"
)
func RegisterFormatters(ctx context.Context, formatters map[string]*Formatter) context.Context {
return context.WithValue(ctx, formattersKey, formatters)
}
func GetFormatters(ctx context.Context) map[string]*Formatter {
return ctx.Value(formattersKey).(map[string]*Formatter)
}
func SetCompletedChannel(ctx context.Context, completedCh chan string) context.Context {
return context.WithValue(ctx, completedChKey, completedCh)
}
func MarkFormatComplete(ctx context.Context, path string) {
ctx.Value(completedChKey).(chan string) <- path
}
func ForwardPath(ctx context.Context, path string, names []string) {
if len(names) == 0 {
return
}
formatters := GetFormatters(ctx)
for _, name := range names {
formatters[name].Put(path)
}
}