From cfb6db54604dce39d55effc75ce84660b5d0312c Mon Sep 17 00:00:00 2001 From: Wiktor Zykubek Date: Fri, 27 Dec 2024 06:12:12 +0100 Subject: [PATCH] feat: embed templates in final binary --- main.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 8cc770a..8b3aef8 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( + "embed" "errors" "flag" "fmt" + "io/fs" "os" "os/exec" "strings" @@ -17,6 +19,9 @@ type LicensingData struct { Year int } +//go:embed all:templates +var Templates embed.FS + var GitConfigError = errors.New("Can't read Git config") var NotSupportedError = errors.New("Not supported license") @@ -36,11 +41,7 @@ func getGitUserData() (string, string, error) { } func getTemplateList() []string { - d, err := os.Open("templates") - if err != nil { - panic(err) - } - files, err := d.Readdir(0) + files, err := fs.ReadDir(Templates, "templates") if err != nil { panic(err) } @@ -60,7 +61,7 @@ func listTemplates() { func genLicense(lcnsName string, lcnsData LicensingData, outFileName string) error { tmplFile := "templates/" + lcnsName + ".tmpl" - tmpl, err := template.ParseFiles(tmplFile) + tmpl, err := template.ParseFS(Templates, tmplFile) if err != nil { return NotSupportedError }