src: use consistent separators for fs.FS instances

This commit is contained in:
Luke Kingland 2021-05-03 22:37:45 +09:00
parent 3bfc13380f
commit 4db07b412f
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
2 changed files with 17 additions and 7 deletions

View File

@ -72,7 +72,7 @@ func (t *templateWriter) load() (err error) {
// Template may be prefixed with a custom repo name.
func (t *templateWriter) Write(runtime, template, dest string) (err error) {
if t.templates == nil {
// load static embedded templates into t.templates as an fs.FSA
// load static embedded templates into t.templates as an fs.FS
if err = t.load(); err != nil {
return
}
@ -102,18 +102,21 @@ func (t *templateWriter) writeCustom(repositories, runtime, template, dest strin
return ErrTemplateMissingRepository
}
// ex: /home/alice/.config/func/repositories/boson/go/http
src := filepath.Join(cc[0], runtime, cc[1])
// Note that the FS instance returned by os.DirFS uses forward slashes
// internally, so source paths do not use the os path separator due to
// that breaking Windows.
src := cc[0] + "/" + runtime + "/" + cc[1]
return t.cp(src, dest, os.DirFS(repositories))
}
func (t *templateWriter) writeEmbedded(runtime, template, dest string) error {
runtimePath := filepath.Join("templates", runtime)
runtimePath := "templates/" + runtime // embedded FS alwas uses '/'
_, err := fs.Stat(t.templates, runtimePath)
if errors.Is(err, fs.ErrNotExist) {
return ErrRuntimeNotFound
}
templatePath := filepath.Join("templates", runtime, template)
templatePath := "templates/" + runtime + "/" + template // always '/' in embedded fs
_, err = fs.Stat(t.templates, templatePath)
if errors.Is(err, fs.ErrNotExist) {
return ErrTemplateNotFound
@ -158,7 +161,11 @@ func (t *templateWriter) copyNode(src, dest string, files fs.FS) error {
return err
}
for _, child := range children {
if err = t.cp(filepath.Join(src, child.Name()), filepath.Join(dest, child.Name()), files); err != nil {
// NOTE: instances of fs.FS use forward slashes,
// even on Windows.
childSrc := src + "/" + child.Name()
childDest := filepath.Join(dest, child.Name())
if err = t.cp(childSrc, childDest, files); err != nil {
return err
}
}

View File

@ -41,9 +41,12 @@ func TestWriteCustom(t *testing.T) {
root := "testdata/testWriteFilesystem"
defer using(t, root)()
// Writer which includes custom repositories
// Writer which includes reference to custom repositories location
w := templateWriter{repositories: "testdata/repositories"}
err := w.Write(TestRuntime, "customProvider/tpla", root)
// template, in form [provider]/[template], on disk the template is
// located at testdata/repositories/[provider]/[runtime]/[template]
tpl := "customProvider/tpla"
err := w.Write(TestRuntime, tpl, root)
if err != nil {
t.Fatal(err)
}