Files
radiantwave-legacy/internal/assets/assets.go
T

51 lines
1.4 KiB
Go

// Package assets provisions the static assets bundled into the binary
// (fonts, shaders, and terms-of-service pages) into a per-user assets
// directory at application startup.
package assets
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
// Provision copies every file from the embedded filesystem into destDir,
// preserving the relative directory structure. Existing files are
// overwritten so the deployed assets always match the running binary.
func Provision(bundled fs.FS, destDir string) error {
return fs.WalkDir(bundled, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
src, err := bundled.Open(path)
if err != nil {
return fmt.Errorf("opening bundled asset %s: %w", path, err)
}
defer src.Close()
dst := filepath.Join(destDir, filepath.FromSlash(path))
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return fmt.Errorf("creating asset directory for %s: %w", path, err)
}
out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("creating asset file %s: %w", dst, err)
}
if _, err := io.Copy(out, src); err != nil {
out.Close()
return fmt.Errorf("writing asset file %s: %w", dst, err)
}
if err := out.Close(); err != nil {
return fmt.Errorf("closing asset file %s: %w", dst, err)
}
return nil
})
}