-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtemplates.go
61 lines (51 loc) · 1.24 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"embed"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/pkg/errors"
)
//go:embed swagger_templates/*
var templatesFS embed.FS
func writeTemplates(destinationRoot string) error {
err := os.RemoveAll(destinationRoot)
if err != nil && !os.IsNotExist(err) {
return errors.Wrapf(err, "cannot remove directory %s", destinationRoot)
}
walkDirFn := func(path string, d os.DirEntry, walkErr error) error {
if path == "." {
return nil
}
if d.IsDir() {
err2 := os.MkdirAll(filepath.Join(destinationRoot, path), 0o750)
if err2 != nil && !os.IsExist(err2) {
return err2
}
return nil
}
src, err := templatesFS.Open(path)
if err != nil {
return errors.Wrapf(err, "Cannot open embedded file %s for reading", path)
}
defer src.Close()
dstFileName := filepath.Join(destinationRoot, path)
dst, err := os.Create(dstFileName)
if err != nil {
return errors.Wrapf(err, "Cannot open %s for writing contents of %s",
dstFileName, path)
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
return errors.Wrapf(err, "Cannot copy %s contents to %s",
path, dstFileName)
}
return nil
}
if err := fs.WalkDir(templatesFS, ".", walkDirFn); err != nil {
return err
}
return nil
}