-
Notifications
You must be signed in to change notification settings - Fork 8
/
files.go
65 lines (56 loc) 路 1.42 KB
/
files.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
62
63
64
65
package files
import (
"bytes"
"embed"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
)
//go:embed files/*
var files embed.FS
type ServicesConfig struct {
LocalLogFiles bool // follow local log files
LogFilePath string // follow specified log file
LogFiles bool // true if LocalLogFiles or LogFilePath
Debug bool // enable debug mode in tools
}
// ExtractFiles extracts the files from the embedded filesystem to the target directory
func ExtractFiles(targetDir string, svcConfig ServicesConfig) error {
err := os.MkdirAll(targetDir, os.ModePerm)
if err != nil {
return err
}
return fs.WalkDir(files, "files", func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
data, err := files.ReadFile(path)
if err != nil {
return err
}
if filepath.Ext(path) == ".tmpl" {
serviceTemplate, err := template.New("serviceTemplateConfigFile").Parse(string(data))
if err != nil {
return err
}
var hydratedContent bytes.Buffer
err = serviceTemplate.Execute(&hydratedContent, svcConfig)
if err != nil {
return err
}
data = hydratedContent.Bytes()
path = strings.ReplaceAll(path, ".tmpl", "")
}
targetPath := filepath.Join(targetDir, path)
err = os.MkdirAll(filepath.Dir(targetPath), os.ModePerm)
if err != nil {
return err
}
return os.WriteFile(targetPath, data, os.ModePerm)
})
}