From e6775cd2d1bbab96479143320f67ac102d5c1627 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 2 Feb 2021 15:19:43 +0000 Subject: [PATCH] use embed.fs wrapper for langFS so lang/ is not needed in paths [files]lang_files is now the path to the lang directory, not path to a directory containing it. --- config/config-base.json | 2 +- embed.py | 22 +++++++++++++++++----- main.go | 10 +++++----- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/config/config-base.json b/config/config-base.json index 0547651..b75f871 100644 --- a/config/config-base.json +++ b/config/config-base.json @@ -867,7 +867,7 @@ "requires_restart": true, "type": "text", "value": "", - "description": "The path to a directory CONTAINING a 'lang/' directory, which follow the same form as the internal one. See GitHub for more info." + "description": "The path to a directory which following the same form as the internal 'lang/' directory. See GitHub for more info." } } } diff --git a/embed.py b/embed.py index 21fd16f..b066c77 100755 --- a/embed.py +++ b/embed.py @@ -13,18 +13,30 @@ with open("embed.go", "w") as f: if EMBED in trues: f.write("""package main import ( - "embed" - "log" + "embed" + "io/fs" + "log" ) //go:embed data data/html data/web data/web/css data/web/js var localFS embed.FS //go:embed lang/common lang/admin lang/email lang/form lang/setup -var langFS embed.FS +var lFS embed.FS + +var langFS LangFS + +type LangFS struct { + fs embed.FS +} + +func (l LangFS) Open(name string) (fs.File, error) { return l.fs.Open("lang/" + name) } +func (l LangFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir("lang/" + name) } +func (l LangFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile("lang/" + name) } func loadLocalFS() { - log.Println("Using internal storage") + langFS = LangFS{lFS} + log.Println("Using internal storage") }""") elif EMBED in falses: f.write("""package main @@ -42,5 +54,5 @@ func loadLocalFS() { log.Println("Using external storage") executable, _ := os.Executable() localFS = os.DirFS(filepath.Dir(executable)) - langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data")) + langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data", "lang")) }""") diff --git a/main.go b/main.go index ea6362e..ed3ce47 100644 --- a/main.go +++ b/main.go @@ -339,10 +339,10 @@ func start(asDaemon, firstCall bool) { }() } - app.storage.lang.CommonPath = "lang/common" - app.storage.lang.FormPath = "lang/form" - app.storage.lang.AdminPath = "lang/admin" - app.storage.lang.EmailPath = "lang/email" + app.storage.lang.CommonPath = "common" + app.storage.lang.FormPath = "form" + app.storage.lang.AdminPath = "admin" + app.storage.lang.EmailPath = "email" externalLang := app.config.Section("files").Key("lang_files").MustString("") var err error if externalLang == "" { @@ -577,7 +577,7 @@ func start(asDaemon, firstCall bool) { } else { debugMode = false address = "0.0.0.0:8056" - app.storage.lang.SetupPath = "lang/setup" + app.storage.lang.SetupPath = "setup" err := app.storage.loadLangSetup(langFS) if err != nil { app.info.Fatalf("Failed to load language files: %+v\n", err)