embed: Begin refactor to use clearer paths

This commit is contained in:
Tom Wiesing 2022-09-11 12:47:00 +02:00
parent e75dc29de1
commit e1ee569629
No known key found for this signature in database
16 changed files with 431 additions and 181 deletions

View file

@ -11,9 +11,9 @@ import (
"github.com/pkg/errors"
)
// resourceEmbed contains all the resources required by the WissKI-Distillery package.
// ResourceEmbed contains all the resources required by the WissKI-Distillery package.
//go:embed all:resources
var resourceEmbed embed.FS
var ResourceEmbed embed.FS
// InstallResource install a resource src into dest.
// When it encounters a directory, recursively installs the directory is called.
@ -22,7 +22,7 @@ var resourceEmbed embed.FS
// If src points to a file, dst must either be an existing file, or not exist.
// If src points to a directory, dst must either be an existing directory, or not exist.
func InstallResource(dst, src string, onInstallFile func(dst, src string)) error {
return installFile(dst, resourceEmbed, src, onInstallFile)
return installFile(dst, ResourceEmbed, src, onInstallFile)
}
var errExpectedFileButGotDirectory = errors.New("Expected a file, but got a directory")

View file

@ -1,60 +0,0 @@
package embed
import (
"os"
"github.com/FAU-CDI/wisski-distillery/internal/unpack"
"github.com/pkg/errors"
)
// InstallTemplates open the resource src, and installs it into dst.
// the template resource must fit into memory.
//
// For each variable ${THING} inside dest, a key 'THING' must exist in context.
// Extra or missing template keys are an error.
func InstallTemplate(dst, src string, context map[string]string) error {
// open the source file!
srcFile, err := resourceEmbed.Open(src)
if err != nil {
return errors.Wrapf(err, "Error opening source file %s", src)
}
defer srcFile.Close()
// write the template
bytes, srcMode, err := unpack.UnpackTemplate(context, srcFile)
if err != nil {
return err
}
// determine if we need to create the destination file, or if it already exists
dstStat, dstErr := os.Stat(dst)
switch {
case os.IsNotExist(dstErr):
case dstErr != nil:
return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst)
case dstStat.IsDir():
return errors.Wrapf(errExpectedFileButGotDirectory, "Error processing destination %s", dst)
}
// open and write the destination file
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcMode)
if err != nil {
return errors.Wrapf(err, "Unable to open file %s", dst)
}
_, err = dstFile.Write(bytes)
return errors.Wrapf(err, "Unable to write destination %s", dst)
}
// ReadTemplate is like InstallTemplate, except that it writes template into a byte slice and returns it.
func ReadTemplate(src string, context map[string]string) ([]byte, error) {
// open the source file!
srcFile, err := resourceEmbed.Open(src)
if err != nil {
return nil, errors.Wrapf(err, "Error opening source file %s", src)
}
defer srcFile.Close()
// and return it
bytes, _, err := unpack.UnpackTemplate(context, srcFile)
return bytes, err
}