refactor(blogo,plugins): move built-in plugins to plugins package
This commit is contained in:
@@ -13,15 +13,18 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import "io/fs"
|
import (
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/fs"
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
|
)
|
||||||
|
|
||||||
const emptySourcerPluginName = "blogo-empty-sourcer"
|
const emptySourcerPluginName = "blogo-empty-sourcer"
|
||||||
|
|
||||||
type emptySourcer struct{}
|
type emptySourcer struct{}
|
||||||
|
|
||||||
func NewEmptySourcer() Plugin {
|
func NewEmptySourcer() plugin.Plugin {
|
||||||
return &emptySourcer{}
|
return &emptySourcer{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +38,10 @@ func (p *emptySourcer) Source() (fs.FS, error) {
|
|||||||
|
|
||||||
type emptyFS struct{}
|
type emptyFS struct{}
|
||||||
|
|
||||||
|
func (f emptyFS) Metadata() fs.Metadata {
|
||||||
|
return fs.MetadataMap(map[string]any{})
|
||||||
|
}
|
||||||
|
|
||||||
func (f emptyFS) Open(name string) (fs.File, error) {
|
func (f emptyFS) Open(name string) (fs.File, error) {
|
||||||
return nil, fs.ErrNotExist
|
return nil, fs.ErrNotExist
|
||||||
}
|
}
|
||||||
@@ -13,19 +13,22 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/fs"
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const foldingRendererPluginName = "blogo-foldingrenderer-renderer"
|
const foldingRendererPluginName = "blogo-foldingrenderer-renderer"
|
||||||
|
|
||||||
type foldingRenderer struct {
|
type foldingRenderer struct {
|
||||||
plugins []RendererPlugin
|
plugins []plugin.Renderer
|
||||||
|
|
||||||
panicOnInit bool
|
panicOnInit bool
|
||||||
|
|
||||||
@@ -39,8 +42,8 @@ type FoldingRendererOpts struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FoldingRenderer interface {
|
type FoldingRenderer interface {
|
||||||
PluginWithPlugins
|
plugin.WithPlugins
|
||||||
RendererPlugin
|
plugin.Renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFoldingRenderer(opts ...FoldingRendererOpts) FoldingRenderer {
|
func NewFoldingRenderer(opts ...FoldingRendererOpts) FoldingRenderer {
|
||||||
@@ -55,32 +58,32 @@ func NewFoldingRenderer(opts ...FoldingRendererOpts) FoldingRenderer {
|
|||||||
opt.Logger = opt.Logger.WithGroup(foldingRendererPluginName)
|
opt.Logger = opt.Logger.WithGroup(foldingRendererPluginName)
|
||||||
|
|
||||||
return &foldingRenderer{
|
return &foldingRenderer{
|
||||||
plugins: []RendererPlugin{},
|
plugins: []plugin.Renderer{},
|
||||||
|
|
||||||
log: opt.Logger,
|
log: opt.Logger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *foldingRenderer) Name() string {
|
func (r *foldingRenderer) Name() string {
|
||||||
return foldingRendererPluginName
|
return foldingRendererPluginName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *foldingRenderer) Use(plugin Plugin) {
|
func (r *foldingRenderer) Use(p plugin.Plugin) {
|
||||||
log := p.log.With(slog.String("plugin", plugin.Name()))
|
log := r.log.With(slog.String("plugin", p.Name()))
|
||||||
|
|
||||||
if plg, ok := plugin.(RendererPlugin); ok {
|
if pr, ok := p.(plugin.Renderer); ok {
|
||||||
p.plugins = append(p.plugins, plg)
|
r.plugins = append(r.plugins, pr)
|
||||||
} else {
|
} else {
|
||||||
m := fmt.Sprintf("failed to add plugin %q, since it doesn't implement RendererPlugin", plugin.Name())
|
m := fmt.Sprintf("failed to add plugin %q, since it doesn't implement plugin.Renderer", p.Name())
|
||||||
log.Error(m)
|
log.Error(m)
|
||||||
if p.panicOnInit {
|
if r.panicOnInit {
|
||||||
panic(fmt.Sprintf("%s: %s", multiRendererPluginName, m))
|
panic(fmt.Sprintf("%s: %s", foldingRendererPluginName, m))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *foldingRenderer) Render(src File, w io.Writer) error {
|
func (r *foldingRenderer) Render(src fs.File, w io.Writer) error {
|
||||||
if len(p.plugins) == 0 {
|
if len(r.plugins) == 0 {
|
||||||
_, err := io.Copy(w, src)
|
_, err := io.Copy(w, src)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -90,8 +93,8 @@ func (p *foldingRenderer) Render(src File, w io.Writer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range p.plugins {
|
for _, p := range r.plugins {
|
||||||
err := r.Render(f, f)
|
err := p.Render(f, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -106,12 +109,12 @@ func (p *foldingRenderer) Render(src File, w io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type foldingFile struct {
|
type foldingFile struct {
|
||||||
File
|
fs.File
|
||||||
read *bytes.Buffer
|
read *bytes.Buffer
|
||||||
writer *bytes.Buffer
|
writer *bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFoldignFile(f File) (*foldingFile, error) {
|
func newFoldignFile(f fs.File) (*foldingFile, error) {
|
||||||
var r, w bytes.Buffer
|
var r, w bytes.Buffer
|
||||||
|
|
||||||
if _, err := io.Copy(&r, f); err != nil {
|
if _, err := io.Copy(&r, f); err != nil {
|
||||||
@@ -13,28 +13,30 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/fs"
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrRendererNotSupportedFile = errors.New("this file is not supported by renderer")
|
var ErrRendererNotSupportedFile = errors.New("this file is not supported by renderer")
|
||||||
|
|
||||||
const multiRendererPluginName = "blogo-multirenderer-renderer"
|
const multiRendererName = "blogo-multirenderer-renderer"
|
||||||
|
|
||||||
type MultiRenderer interface {
|
type MultiRenderer interface {
|
||||||
RendererPlugin
|
plugin.Renderer
|
||||||
PluginWithPlugins
|
plugin.WithPlugins
|
||||||
}
|
}
|
||||||
|
|
||||||
type multiRenderer struct {
|
type multiRenderer struct {
|
||||||
renderers []RendererPlugin
|
renderers []plugin.Renderer
|
||||||
|
|
||||||
skipOnError bool
|
skipOnError bool
|
||||||
panicOnInit bool
|
panicOnInit bool
|
||||||
@@ -57,10 +59,10 @@ func NewMultiRenderer(opts ...MultiRendererOpts) MultiRenderer {
|
|||||||
if opt.Logger == nil {
|
if opt.Logger == nil {
|
||||||
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
||||||
}
|
}
|
||||||
opt.Logger = opt.Logger.WithGroup(multiRendererPluginName)
|
opt.Logger = opt.Logger.WithGroup(multiRendererName)
|
||||||
|
|
||||||
return &multiRenderer{
|
return &multiRenderer{
|
||||||
renderers: []RendererPlugin{},
|
renderers: []plugin.Renderer{},
|
||||||
|
|
||||||
skipOnError: !opt.NotSkipOnError,
|
skipOnError: !opt.NotSkipOnError,
|
||||||
panicOnInit: !opt.NotPanicOnInit,
|
panicOnInit: !opt.NotPanicOnInit,
|
||||||
@@ -69,40 +71,40 @@ func NewMultiRenderer(opts ...MultiRendererOpts) MultiRenderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *multiRenderer) Name() string {
|
func (r *multiRenderer) Name() string {
|
||||||
return multiRendererPluginName
|
return multiRendererName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *multiRenderer) Use(plugin Plugin) {
|
func (r *multiRenderer) Use(p plugin.Plugin) {
|
||||||
log := p.log.With(slog.String("plugin", plugin.Name()))
|
log := r.log.With(slog.String("plugin", p.Name()))
|
||||||
|
|
||||||
if plg, ok := plugin.(RendererPlugin); ok {
|
if pr, ok := p.(plugin.Renderer); ok {
|
||||||
log.Debug("Added renderer plugin")
|
log.Debug("Added renderer plugin")
|
||||||
p.renderers = append(p.renderers, plg)
|
r.renderers = append(r.renderers, pr)
|
||||||
} else {
|
} else {
|
||||||
m := fmt.Sprintf("failed to add plugin %q, since it doesn't implement RendererPlugin", plugin.Name())
|
m := fmt.Sprintf("failed to add plugin %q, since it doesn't implement plugin.Renderer", p.Name())
|
||||||
log.Error(m)
|
log.Error(m)
|
||||||
if p.panicOnInit {
|
if r.panicOnInit {
|
||||||
panic(fmt.Sprintf("%s: %s", p.Name(), m))
|
panic(fmt.Sprintf("%s: %s", r.Name(), m))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *multiRenderer) Render(f File, w io.Writer) error {
|
func (r *multiRenderer) Render(src fs.File, w io.Writer) error {
|
||||||
mf := newMultiRendererFile(f)
|
mf := newMultiRendererFile(src)
|
||||||
for _, r := range p.renderers {
|
for _, pr := range r.renderers {
|
||||||
log := p.log.With(slog.String("plugin", r.Name()))
|
log := r.log.With(slog.String("plugin", pr.Name()))
|
||||||
|
|
||||||
log.Debug("Trying to render with plugin")
|
log.Debug("Trying to render with plugin")
|
||||||
err := r.Render(f, w)
|
err := pr.Render(src, w)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if !p.skipOnError && !errors.Is(err, ErrRendererNotSupportedFile) {
|
if !r.skipOnError && !errors.Is(err, ErrRendererNotSupportedFile) {
|
||||||
log.Error("Failed to render using plugin", slog.String("error", err.Error()))
|
log.Error("Failed to render using plugin", slog.String("error", err.Error()))
|
||||||
return errors.Join(fmt.Errorf("failed to render using plugin %q", p.Name()), err)
|
return errors.Join(fmt.Errorf("failed to render using plugin %q", pr.Name()), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Unable to render using plugin", slog.String("error", err.Error()))
|
log.Debug("Unable to render using plugin", slog.String("error", err.Error()))
|
||||||
@@ -13,25 +13,27 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/fs"
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const multiSourcerPluginName = "blogo-multisourcer-sourcer"
|
const multiSourcerName = "blogo-multisourcer-sourcer"
|
||||||
|
|
||||||
type MultiSourcer interface {
|
type MultiSourcer interface {
|
||||||
SourcerPlugin
|
plugin.Sourcer
|
||||||
PluginWithPlugins
|
plugin.WithPlugins
|
||||||
}
|
}
|
||||||
|
|
||||||
type multiSourcer struct {
|
type multiSourcer struct {
|
||||||
sources []SourcerPlugin
|
sources []plugin.Sourcer
|
||||||
|
|
||||||
panicOnInit bool
|
panicOnInit bool
|
||||||
skipOnSourceError bool
|
skipOnSourceError bool
|
||||||
@@ -57,10 +59,10 @@ func NewMultiSourcer(opts ...MultiSourcerOpts) MultiSourcer {
|
|||||||
if opt.Logger == nil {
|
if opt.Logger == nil {
|
||||||
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
||||||
}
|
}
|
||||||
opt.Logger = opt.Logger.WithGroup(multiSourcerPluginName)
|
opt.Logger = opt.Logger.WithGroup(multiSourcerName)
|
||||||
|
|
||||||
return &multiSourcer{
|
return &multiSourcer{
|
||||||
sources: []SourcerPlugin{},
|
sources: []plugin.Sourcer{},
|
||||||
|
|
||||||
panicOnInit: !opt.NotPanicOnInit,
|
panicOnInit: !opt.NotPanicOnInit,
|
||||||
skipOnSourceError: !opt.NotSkipOnSourceError,
|
skipOnSourceError: !opt.NotSkipOnSourceError,
|
||||||
@@ -70,36 +72,36 @@ func NewMultiSourcer(opts ...MultiSourcerOpts) MultiSourcer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *multiSourcer) Name() string {
|
func (s *multiSourcer) Name() string {
|
||||||
return multiSourcerPluginName
|
return multiSourcerName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *multiSourcer) Use(plugin Plugin) {
|
func (s *multiSourcer) Use(p plugin.Plugin) {
|
||||||
log := p.log.With(slog.String("plugin", plugin.Name()))
|
log := s.log.With(slog.String("plugin", p.Name()))
|
||||||
|
|
||||||
if plg, ok := plugin.(SourcerPlugin); ok {
|
if plg, ok := p.(plugin.Sourcer); ok {
|
||||||
log.Debug("Added sourcer plugin")
|
log.Debug("Added sourcer plugin")
|
||||||
p.sources = append(p.sources, plg)
|
s.sources = append(s.sources, plg)
|
||||||
} else {
|
} else {
|
||||||
m := fmt.Sprintf("failed to add plugin %q, since it doesn't implement SourcerPlugin", plugin.Name())
|
m := fmt.Sprintf("failed to add plugin %q, since it doesn't implement plugin.Sourcer", p.Name())
|
||||||
log.Error(m)
|
log.Error(m)
|
||||||
if p.panicOnInit {
|
if s.panicOnInit {
|
||||||
panic(fmt.Sprintf("%s: %s", p.Name(), m))
|
panic(fmt.Sprintf("%s: %s", p.Name(), m))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *multiSourcer) Source() (FS, error) {
|
func (s *multiSourcer) Source() (fs.FS, error) {
|
||||||
log := p.log
|
log := s.log
|
||||||
|
|
||||||
fileSystems := []FS{}
|
fileSystems := []fs.FS{}
|
||||||
|
|
||||||
for _, s := range p.sources {
|
for _, ps := range s.sources {
|
||||||
log = log.With(slog.String("plugin", p.Name()))
|
log = log.With(slog.String("plugin", ps.Name()))
|
||||||
log.Info("Sourcing file system of plugin")
|
log.Info("Sourcing file system of plugin")
|
||||||
|
|
||||||
f, err := s.Source()
|
f, err := ps.Source()
|
||||||
if err != nil && p.skipOnSourceError {
|
if err != nil && s.skipOnSourceError {
|
||||||
log.Error(
|
log.Error(
|
||||||
"Failed to source file system of plugin, skipping",
|
"Failed to source file system of plugin, skipping",
|
||||||
slog.String("error", err.Error()),
|
slog.String("error", err.Error()),
|
||||||
@@ -115,31 +117,31 @@ func (p *multiSourcer) Source() (FS, error) {
|
|||||||
fileSystems = append(fileSystems, f)
|
fileSystems = append(fileSystems, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
f := make([]FS, len(fileSystems), len(fileSystems))
|
f := make([]fs.FS, len(fileSystems))
|
||||||
for i := range f {
|
for i := range f {
|
||||||
f[i] = fileSystems[i]
|
f[i] = fileSystems[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
return &multiSourcerFS{
|
return &multiSourcerFS{
|
||||||
fileSystems: f,
|
fileSystems: f,
|
||||||
skipOnError: p.skipOnFSError,
|
skipOnError: s.skipOnFSError,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type multiSourcerFS struct {
|
type multiSourcerFS struct {
|
||||||
fileSystems []FS
|
fileSystems []fs.FS
|
||||||
skipOnError bool
|
skipOnError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pf *multiSourcerFS) Metadata() Metadata {
|
func (pf *multiSourcerFS) Metadata() fs.Metadata {
|
||||||
var m Metadata
|
var m fs.Metadata
|
||||||
for _, v := range pf.fileSystems {
|
for _, v := range pf.fileSystems {
|
||||||
m = JoinMetadata(m, v.Metadata())
|
m = fs.JoinMetadata(m, v.Metadata())
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mf *multiSourcerFS) Open(name string) (File, error) {
|
func (mf *multiSourcerFS) Open(name string) (fs.File, error) {
|
||||||
for _, f := range mf.fileSystems {
|
for _, f := range mf.fileSystems {
|
||||||
file, err := f.Open(name)
|
file, err := f.Open(name)
|
||||||
|
|
||||||
@@ -13,25 +13,27 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/fs"
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const plainTextPluginName = "blogo-plaintext-renderer"
|
const plainTextName = "blogo-plaintext-renderer"
|
||||||
|
|
||||||
type painText struct{}
|
type painText struct{}
|
||||||
|
|
||||||
func NewPlainText() Plugin {
|
func NewPlainText() plugin.Plugin {
|
||||||
return &painText{}
|
return &painText{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *painText) Name() string {
|
func (p *painText) Name() string {
|
||||||
return plainTextPluginName
|
return plainTextName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *painText) Render(f fs.File, w io.Writer) error {
|
func (p *painText) Render(f fs.File, w io.Writer) error {
|
||||||
0
blogo/plugins/plugingroup.go
Normal file
0
blogo/plugins/plugingroup.go
Normal file
16
blogo/plugins/plugins.go
Normal file
16
blogo/plugins/plugins.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
// Copyright 2025-present Gustavo "Guz" L. de Mello
|
||||||
|
// Copyright 2025-present The Lored.dev Contributors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package plugins
|
||||||
@@ -13,26 +13,28 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/fs"
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const prefixedSourcerPluginName = "blogo-prefixedsourcer-sourcer"
|
const prefixedSourcerName = "blogo-prefixedsourcer-sourcer"
|
||||||
|
|
||||||
type PrefixedSourcer interface {
|
type PrefixedSourcer interface {
|
||||||
SourcerPlugin
|
plugin.Sourcer
|
||||||
PluginWithPlugins
|
plugin.WithPlugins
|
||||||
UseNamed(string, Plugin)
|
UseNamed(string, plugin.Plugin)
|
||||||
}
|
}
|
||||||
|
|
||||||
type prefixedSourcer struct {
|
type prefixedSourcer struct {
|
||||||
sources map[string]SourcerPlugin
|
sources map[string]plugin.Sourcer
|
||||||
|
|
||||||
prefixSeparator string
|
prefixSeparator string
|
||||||
acceptDuplicated bool
|
acceptDuplicated bool
|
||||||
@@ -69,10 +71,10 @@ func NewPrefixedSourcer(opts ...PrefixedSourcerOpts) PrefixedSourcer {
|
|||||||
if opt.Logger == nil {
|
if opt.Logger == nil {
|
||||||
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
||||||
}
|
}
|
||||||
opt.Logger = opt.Logger.WithGroup(prefixedSourcerPluginName)
|
opt.Logger = opt.Logger.WithGroup(prefixedSourcerName)
|
||||||
|
|
||||||
return &prefixedSourcer{
|
return &prefixedSourcer{
|
||||||
sources: map[string]SourcerPlugin{},
|
sources: map[string]plugin.Sourcer{},
|
||||||
|
|
||||||
prefixSeparator: opt.PrefixSeparator,
|
prefixSeparator: opt.PrefixSeparator,
|
||||||
acceptDuplicated: opt.AcceptDuplicated,
|
acceptDuplicated: opt.AcceptDuplicated,
|
||||||
@@ -85,56 +87,56 @@ func NewPrefixedSourcer(opts ...PrefixedSourcerOpts) PrefixedSourcer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *prefixedSourcer) Name() string {
|
func (s *prefixedSourcer) Name() string {
|
||||||
return prefixedSourcerPluginName
|
return prefixedSourcerName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *prefixedSourcer) Use(plugin Plugin) {
|
func (s *prefixedSourcer) Use(plugin plugin.Plugin) {
|
||||||
p.UseNamed(plugin.Name(), plugin)
|
s.UseNamed(plugin.Name(), plugin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *prefixedSourcer) UseNamed(prefix string, plugin Plugin) {
|
func (s *prefixedSourcer) UseNamed(prefix string, p plugin.Plugin) {
|
||||||
log := p.log.With(slog.String("plugin", plugin.Name()), slog.String("prefix", prefix))
|
log := s.log.With(slog.String("plugin", p.Name()), slog.String("prefix", prefix))
|
||||||
|
|
||||||
var sourcer SourcerPlugin
|
var sourcer plugin.Sourcer
|
||||||
if plg, ok := plugin.(SourcerPlugin); ok {
|
if ps, ok := p.(plugin.Sourcer); ok {
|
||||||
sourcer = plg
|
sourcer = ps
|
||||||
} else {
|
} else {
|
||||||
m := fmt.Sprintf("failed to add plugin %q (with prefix %q), since it doesn't implement SourcerPlugin", plugin.Name(), prefix)
|
m := fmt.Sprintf("failed to add plugin %q (with prefix %q), since it doesn't implement SourcerPlugin", p.Name(), prefix)
|
||||||
log.Error(m)
|
log.Error(m)
|
||||||
if p.panicOnInit {
|
if s.panicOnInit {
|
||||||
panic(fmt.Sprintf("%s: %s", multiRendererPluginName, m))
|
panic(fmt.Sprintf("%s: %s", prefixedSourcerName, m))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := p.sources[prefix]; ok && !p.acceptDuplicated {
|
if _, ok := s.sources[prefix]; ok && !s.acceptDuplicated {
|
||||||
m := fmt.Sprintf(
|
m := fmt.Sprintf(
|
||||||
"duplicated prefix (%q) for plugin %q",
|
"duplicated prefix (%q) for plugin %q",
|
||||||
prefix,
|
prefix,
|
||||||
plugin.Name(),
|
p.Name(),
|
||||||
)
|
)
|
||||||
log.Error(m)
|
log.Error(m)
|
||||||
if p.panicOnInit {
|
if s.panicOnInit {
|
||||||
panic(fmt.Sprintf("%s: %s", multiRendererPluginName, m))
|
panic(fmt.Sprintf("%s: %s", prefixedSourcerName, m))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug(fmt.Sprintf("Added sourcer plugin, with prefix %q", prefix))
|
log.Debug(fmt.Sprintf("Added sourcer plugin, with prefix %q", prefix))
|
||||||
p.sources[prefix] = sourcer
|
s.sources[prefix] = sourcer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *prefixedSourcer) Source() (FS, error) {
|
func (s *prefixedSourcer) Source() (fs.FS, error) {
|
||||||
log := p.log
|
log := s.log
|
||||||
|
|
||||||
fileSystems := make(map[string]FS, len(p.sources))
|
fileSystems := make(map[string]fs.FS, len(s.sources))
|
||||||
|
|
||||||
for a, s := range p.sources {
|
for a, ps := range s.sources {
|
||||||
log = log.With(slog.String("plugin", p.Name()), slog.String("prefix", a))
|
log = log.With(slog.String("plugin", ps.Name()), slog.String("prefix", a))
|
||||||
log.Info("Sourcing file system of plugin")
|
log.Info("Sourcing file system of plugin")
|
||||||
|
|
||||||
f, err := s.Source()
|
f, err := ps.Source()
|
||||||
if err != nil && p.skipOnSourceError {
|
if err != nil && s.skipOnSourceError {
|
||||||
log.Error(
|
log.Error(
|
||||||
"Failed to source file system of plugin, skipping",
|
"Failed to source file system of plugin, skipping",
|
||||||
slog.String("error", err.Error()),
|
slog.String("error", err.Error()),
|
||||||
@@ -152,24 +154,24 @@ func (p *prefixedSourcer) Source() (FS, error) {
|
|||||||
|
|
||||||
return &prefixedSourcerFS{
|
return &prefixedSourcerFS{
|
||||||
fileSystems: fileSystems,
|
fileSystems: fileSystems,
|
||||||
prefixSeparator: p.prefixSeparator,
|
prefixSeparator: s.prefixSeparator,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type prefixedSourcerFS struct {
|
type prefixedSourcerFS struct {
|
||||||
fileSystems map[string]FS
|
fileSystems map[string]fs.FS
|
||||||
prefixSeparator string
|
prefixSeparator string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pf *prefixedSourcerFS) Metadata() Metadata {
|
func (pf *prefixedSourcerFS) Metadata() fs.Metadata {
|
||||||
var m Metadata
|
var m fs.Metadata
|
||||||
for _, v := range pf.fileSystems {
|
for _, v := range pf.fileSystems {
|
||||||
m = JoinMetadata(m, v.Metadata())
|
m = fs.JoinMetadata(m, v.Metadata())
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pf *prefixedSourcerFS) Open(name string) (File, error) {
|
func (pf *prefixedSourcerFS) Open(name string) (fs.File, error) {
|
||||||
prefix, path, found := strings.Cut(name, pf.prefixSeparator)
|
prefix, path, found := strings.Cut(name, pf.prefixSeparator)
|
||||||
if !found {
|
if !found {
|
||||||
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
|
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
|
||||||
@@ -13,43 +13,45 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package blogo
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
const priorityGroupPluginName = "blogo-prioritygroup-group"
|
const priorityGroupName = "blogo-prioritygroup-group"
|
||||||
|
|
||||||
type priorityGroup struct {
|
type priorityGroup struct {
|
||||||
plugins []Plugin
|
plugins []plugin.Plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
type PriorityGroup interface {
|
type PriorityGroup interface {
|
||||||
PluginWithPlugins
|
plugin.WithPlugins
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPriorityGroup(plugins ...Plugin) PriorityGroup {
|
func NewPriorityGroup(plugins ...plugin.Plugin) PriorityGroup {
|
||||||
return &priorityGroup{plugins}
|
return &priorityGroup{plugins}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *priorityGroup) Name() string {
|
func (p *priorityGroup) Name() string {
|
||||||
return priorityGroupPluginName
|
return priorityGroupName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *priorityGroup) Use(plugin Plugin) {
|
func (p *priorityGroup) Use(plugin plugin.Plugin) {
|
||||||
p.plugins = append(p.plugins, plugin)
|
p.plugins = append(p.plugins, plugin)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *priorityGroup) Plugins() []Plugin {
|
func (p *priorityGroup) Plugins() []plugin.Plugin {
|
||||||
slices.SortStableFunc(p.plugins, func(a Plugin, b Plugin) int {
|
slices.SortStableFunc(p.plugins, func(a plugin.Plugin, b plugin.Plugin) int {
|
||||||
return cmp.Compare(p.getPriority(a, b), p.getPriority(b, a))
|
return cmp.Compare(p.getPriority(a, b), p.getPriority(b, a))
|
||||||
})
|
})
|
||||||
return p.plugins
|
return p.plugins
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *priorityGroup) getPriority(plugin Plugin, cmp Plugin) int {
|
func (p *priorityGroup) getPriority(plugin plugin.Plugin, cmp plugin.Plugin) int {
|
||||||
if plg, ok := plugin.(PluginWithDynamicPriority); ok {
|
if plg, ok := plugin.(PluginWithDynamicPriority); ok {
|
||||||
return plg.Priority(cmp)
|
return plg.Priority(cmp)
|
||||||
} else if plg, ok := plugin.(PluginWithPriority); ok {
|
} else if plg, ok := plugin.(PluginWithPriority); ok {
|
||||||
@@ -60,11 +62,11 @@ func (p *priorityGroup) getPriority(plugin Plugin, cmp Plugin) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PluginWithPriority interface {
|
type PluginWithPriority interface {
|
||||||
Plugin
|
plugin.Plugin
|
||||||
Priority() int
|
Priority() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginWithDynamicPriority interface {
|
type PluginWithDynamicPriority interface {
|
||||||
Plugin
|
plugin.Plugin
|
||||||
Priority(Plugin) int
|
Priority(plugin.Plugin) int
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user