feat(blogo,plugins,multisourcer): logging and assertions to follow the code style
This commit is contained in:
@@ -22,43 +22,48 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
|
|
||||||
"forge.capytal.company/loreddev/x/blogo/plugin"
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
||||||
|
"forge.capytal.company/loreddev/x/tinyssert"
|
||||||
)
|
)
|
||||||
|
|
||||||
const plainTextName = "blogo-plaintext-renderer"
|
const plainTextName = "blogo-plaintext-renderer"
|
||||||
|
|
||||||
type painText struct{}
|
func NewPlainText(opts ...PlainTextOpts) plugin.Renderer {
|
||||||
|
opt := PlainTextOpts{}
|
||||||
|
if len(opts) > 0 {
|
||||||
|
opt = opts[0]
|
||||||
|
}
|
||||||
|
|
||||||
func NewPlainText() plugin.Renderer {
|
if opt.Assertions == nil {
|
||||||
return &painText{}
|
opt.Assertions = tinyssert.NewDisabledAssertions()
|
||||||
|
}
|
||||||
|
|
||||||
|
return &painText{
|
||||||
|
assert: opt.Assertions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type PlainTextOpts struct {
|
||||||
|
Assertions tinyssert.Assertions
|
||||||
|
}
|
||||||
|
|
||||||
|
type painText struct {
|
||||||
|
assert tinyssert.Assertions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *painText) Name() string {
|
func (p *painText) Name() string {
|
||||||
return plainTextName
|
return plainTextName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *painText) Render(f fs.File, w io.Writer) error {
|
func (p *painText) Render(src fs.File, w io.Writer) error {
|
||||||
if d, ok := f.(fs.ReadDirFile); ok {
|
p.assert.NotNil(src)
|
||||||
|
p.assert.NotNil(w)
|
||||||
|
|
||||||
|
if d, ok := src.(fs.ReadDirFile); ok {
|
||||||
return p.renderDirectory(d, w)
|
return p.renderDirectory(d, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
_, err := io.Copy(w, src)
|
||||||
for {
|
|
||||||
n, err := f.Read(buf)
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write(buf[:n])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *painText) renderDirectory(f fs.ReadDirFile, w io.Writer) error {
|
func (p *painText) renderDirectory(f fs.ReadDirFile, w io.Writer) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user