linstrom/shared/fswrapper.go
2025-04-14 16:59:59 +02:00

40 lines
895 B
Go

package shared
import (
"io/fs"
"github.com/rs/zerolog/log"
)
// Fix for go:embed file systems including the full path of the embedded files
// Adds a given string to the front of all requests
type FSWrapper struct {
wrapped fs.FS
toAdd string
log bool
}
// Compile time interface implementation assertion
var _ fs.FS = &FSWrapper{}
// No test, as no processing happens, only wrapping the arguments in a struct
func NewFSWrapper(wraps fs.FS, appends string, logAccess bool) *FSWrapper {
return &FSWrapper{
wrapped: wraps,
toAdd: appends,
log: logAccess,
}
}
// Pretty sure this can't be reliably tested
func (fw *FSWrapper) Open(name string) (fs.File, error) {
res, err := fw.wrapped.Open(fw.toAdd + name)
if fw.log {
log.Debug().
Str("prefix", fw.toAdd).
Str("filename", name).
Err(err).
Msg("fswrapper: File access result")
}
return res, err
}