summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2022-01-23 23:42:33 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2022-01-23 23:42:33 +0100
commit27791043e7e07ba33ffde6d180fcad9bfa992921 (patch)
tree9c8122b77da6f96604dc23c2bc9ea57f2b197227
parentcd8e12750c96264232dcd04886130479e9010e83 (diff)
downloadengarde-importer-27791043e7e07ba33ffde6d180fcad9bfa992921.tar.gz
engarde-importer-27791043e7e07ba33ffde6d180fcad9bfa992921.tar.bz2
engarde-importer-27791043e7e07ba33ffde6d180fcad9bfa992921.zip
First step: write out verbatim template files
Diffstat (limited to '')
-rw-r--r--main.go3
-rw-r--r--template.go55
2 files changed, 54 insertions, 4 deletions
diff --git a/main.go b/main.go
index c6799f7..e2da4cd 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"log"
"os"
"strings"
+ "time"
"github.com/jszwec/csvutil"
)
@@ -159,7 +160,7 @@ func run() error {
return err
}
- return writeVerbatim()
+ return write(cfg)
}
func main() {
diff --git a/template.go b/template.go
index df215e5..67cd565 100644
--- a/template.go
+++ b/template.go
@@ -2,19 +2,68 @@ package main
import (
"embed"
+ "fmt"
+ "io"
+ "io/fs"
+ "os"
+ "path"
+
+ "golang.org/x/text/encoding/charmap"
)
+const verbatimPath = "templates/verbatim"
+
//go:embed templates/verbatim
var verbatim embed.FS
-func writeVerbatim() error {
- entries, err := verbatim.ReadDir("templates/verbatim")
+//goland:noinspection GoUnhandledErrorResult
+func writeVerbatimFile(outputDir string, entry fs.DirEntry) error {
+ inName := path.Join(verbatimPath, entry.Name())
+
+ input, err := verbatim.Open(inName)
+ if err != nil {
+ return fmt.Errorf("reading bundled file '%s': %w", entry.Name(), err)
+ }
+ defer input.Close()
+
+ outName := path.Join(outputDir, entry.Name())
+ output, err := os.Create(outName)
+ if err != nil {
+ return fmt.Errorf("creating '%s': %w", outName, err)
+ }
+ defer output.Close()
+
+ encodedOutput := charmap.ISO8859_1.NewEncoder().Writer(output)
+
+ if _, err = io.Copy(encodedOutput, input); err != nil {
+ return fmt.Errorf("writing to '%s': %w", outName, err)
+ }
+
+ return nil
+}
+
+func writeVerbatim(outputDir string) error {
+ entries, err := verbatim.ReadDir(verbatimPath)
if err != nil {
return err
}
for _, e := range entries {
- print(e.Name())
+ if err = writeVerbatimFile(outputDir, e); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func write(config EngardeConfig) error {
+ if err := os.MkdirAll(config.outputDir, 0755); err != nil {
+ return fmt.Errorf("creating output directory '%s': %w", config.outputDir, err)
+ }
+
+ if err := writeVerbatim(config.outputDir); err != nil {
+ return fmt.Errorf("copying default files: %w", err)
}
return nil