2025-01-13 16:23:27 -03:00
|
|
|
// 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.
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
package plugins
|
2025-01-13 16:18:58 -03:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2025-01-23 09:48:45 -03:00
|
|
|
"io/fs"
|
2025-01-13 16:18:58 -03:00
|
|
|
"log/slog"
|
2025-01-22 19:35:44 -03:00
|
|
|
|
2025-01-23 09:48:45 -03:00
|
|
|
"forge.capytal.company/loreddev/x/blogo/metadata"
|
2025-01-22 19:35:44 -03:00
|
|
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
2025-01-23 17:18:33 -03:00
|
|
|
"forge.capytal.company/loreddev/x/tinyssert"
|
2025-01-13 16:18:58 -03:00
|
|
|
)
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
const multiSourcerName = "blogo-multisourcer-sourcer"
|
2025-01-13 16:18:58 -03:00
|
|
|
|
|
|
|
|
func NewMultiSourcer(opts ...MultiSourcerOpts) MultiSourcer {
|
|
|
|
|
opt := MultiSourcerOpts{}
|
|
|
|
|
if len(opts) > 0 {
|
|
|
|
|
opt = opts[0]
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:18:33 -03:00
|
|
|
if opt.Assertions == nil {
|
|
|
|
|
opt.Assertions = tinyssert.NewDisabledAssertions()
|
|
|
|
|
}
|
2025-01-13 16:18:58 -03:00
|
|
|
if opt.Logger == nil {
|
|
|
|
|
opt.Logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &multiSourcer{
|
2025-01-23 17:18:04 -03:00
|
|
|
plugins: []plugin.Sourcer{},
|
2025-01-13 16:18:58 -03:00
|
|
|
|
2025-01-23 17:18:04 -03:00
|
|
|
skipOnSourceError: opt.SkipOnSourceError,
|
|
|
|
|
skipOnFSError: opt.SkipOnFSError,
|
2025-01-13 16:18:58 -03:00
|
|
|
|
|
|
|
|
log: opt.Logger,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 17:18:04 -03:00
|
|
|
type MultiSourcer interface {
|
|
|
|
|
plugin.Sourcer
|
|
|
|
|
plugin.WithPlugins
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MultiSourcerOpts struct {
|
|
|
|
|
SkipOnSourceError bool
|
|
|
|
|
SkipOnFSError bool
|
|
|
|
|
|
|
|
|
|
Assertions tinyssert.Assertions
|
|
|
|
|
Logger *slog.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type multiSourcer struct {
|
|
|
|
|
plugins []plugin.Sourcer
|
|
|
|
|
|
|
|
|
|
skipOnSourceError bool
|
|
|
|
|
skipOnFSError bool
|
|
|
|
|
|
|
|
|
|
assert tinyssert.Assertions
|
|
|
|
|
log *slog.Logger
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
func (s *multiSourcer) Name() string {
|
|
|
|
|
return multiSourcerName
|
2025-01-13 16:18:58 -03:00
|
|
|
}
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
func (s *multiSourcer) Use(p plugin.Plugin) {
|
2025-01-23 17:18:33 -03:00
|
|
|
s.assert.NotNil(p)
|
|
|
|
|
s.assert.NotNil(s.plugins)
|
|
|
|
|
s.assert.NotNil(s.log)
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
log := s.log.With(slog.String("plugin", p.Name()))
|
2025-01-13 16:18:58 -03:00
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
if plg, ok := p.(plugin.Sourcer); ok {
|
2025-01-13 17:02:08 -03:00
|
|
|
log.Debug("Added sourcer plugin")
|
2025-01-23 17:18:04 -03:00
|
|
|
s.plugins = append(s.plugins, plg)
|
2025-01-13 16:18:58 -03:00
|
|
|
} else {
|
2025-01-23 17:18:04 -03:00
|
|
|
log.Error(fmt.Sprintf(
|
|
|
|
|
"Failed to add plugin %q, since it doesn't implement plugin.Sourcer",
|
|
|
|
|
p.Name(),
|
|
|
|
|
))
|
2025-01-13 16:18:58 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
func (s *multiSourcer) Source() (fs.FS, error) {
|
2025-01-23 17:18:33 -03:00
|
|
|
s.assert.NotNil(s.plugins)
|
|
|
|
|
s.assert.NotNil(s.log)
|
|
|
|
|
|
2025-01-23 17:18:04 -03:00
|
|
|
log := s.log.With()
|
2025-01-13 16:18:58 -03:00
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
fileSystems := []fs.FS{}
|
2025-01-13 16:18:58 -03:00
|
|
|
|
2025-01-23 17:18:04 -03:00
|
|
|
for _, ps := range s.plugins {
|
2025-01-22 19:35:44 -03:00
|
|
|
log = log.With(slog.String("plugin", ps.Name()))
|
2025-01-13 16:18:58 -03:00
|
|
|
log.Info("Sourcing file system of plugin")
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
f, err := ps.Source()
|
|
|
|
|
if err != nil && s.skipOnSourceError {
|
2025-01-23 17:18:04 -03:00
|
|
|
log.Warn(
|
2025-01-13 16:18:58 -03:00
|
|
|
"Failed to source file system of plugin, skipping",
|
|
|
|
|
slog.String("error", err.Error()),
|
|
|
|
|
)
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
log.Error(
|
|
|
|
|
"Failed to source file system of plugin, returning error",
|
|
|
|
|
slog.String("error", err.Error()),
|
|
|
|
|
)
|
|
|
|
|
return f, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileSystems = append(fileSystems, f)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
f := make([]fs.FS, len(fileSystems))
|
2025-01-13 16:18:58 -03:00
|
|
|
for i := range f {
|
|
|
|
|
f[i] = fileSystems[i]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &multiSourcerFS{
|
|
|
|
|
fileSystems: f,
|
2025-01-22 19:35:44 -03:00
|
|
|
skipOnError: s.skipOnFSError,
|
2025-01-13 16:18:58 -03:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type multiSourcerFS struct {
|
2025-01-22 19:35:44 -03:00
|
|
|
fileSystems []fs.FS
|
2025-01-13 16:18:58 -03:00
|
|
|
skipOnError bool
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 09:48:45 -03:00
|
|
|
func (pf *multiSourcerFS) Metadata() metadata.Metadata {
|
|
|
|
|
ms := []metadata.Metadata{}
|
2025-01-14 17:35:11 -03:00
|
|
|
for _, v := range pf.fileSystems {
|
2025-01-23 09:48:45 -03:00
|
|
|
if m, err := metadata.GetMetadata(v); err == nil {
|
|
|
|
|
ms = append(ms, m)
|
|
|
|
|
}
|
2025-01-14 17:35:11 -03:00
|
|
|
}
|
2025-01-23 09:48:45 -03:00
|
|
|
return metadata.Join(ms...)
|
2025-01-14 17:35:11 -03:00
|
|
|
}
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
func (mf *multiSourcerFS) Open(name string) (fs.File, error) {
|
2025-01-13 16:18:58 -03:00
|
|
|
for _, f := range mf.fileSystems {
|
|
|
|
|
file, err := f.Open(name)
|
|
|
|
|
|
|
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) && !mf.skipOnError {
|
|
|
|
|
return file, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
return file, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, fs.ErrNotExist
|
|
|
|
|
}
|