Cleanup internal packages

This commit is contained in:
Tom Wiesing 2022-09-11 17:12:57 +02:00
parent 86a4334796
commit abafab9f0a
No known key found for this signature in database
7 changed files with 93 additions and 145 deletions

View file

@ -5,6 +5,8 @@ import (
"bufio"
"fmt"
"io"
"io/fs"
"os"
"strings"
"golang.org/x/exp/maps"
@ -194,3 +196,38 @@ parseloop:
return nil
}
// InstallTemplate unpacks the resource located at src in fsys, then processes it as a template, and eventually writes it to dst.
// Any existing file is truncated and overwritten.
//
// See [WriteTemplate] for possible errors.
func InstallTemplate(dst string, context map[string]string, src string, fsys fs.FS) error {
// open the srcFile
srcFile, err := fsys.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// stat it
srcInfo, err := srcFile.Stat()
if err != nil {
return err
}
// check if it is a directory
if srcInfo.IsDir() {
return errExpectedFileButGotDirectory
}
// open the destination file
file, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())
if err != nil {
return err
}
defer file.Close()
// write the file!
return WriteTemplate(file, context, srcFile)
}