-
Notifications
You must be signed in to change notification settings - Fork 263
/
formatter_fs.go
114 lines (102 loc) · 3.11 KB
/
formatter_fs.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package controldisplay
import (
"embed"
"encoding/json"
"io/fs"
"log"
"os"
"path/filepath"
"github.com/turbot/steampipe/pkg/filepaths"
)
//go:embed templates/*
var builtinTemplateFS embed.FS
type TemplateVersionFile struct {
Version string `json:"version"`
}
// EnsureTemplates scans the '$STEAMPIPE_INSTALL_DIR/check/templates' directory and
// copies over the templates defined in the 'templates' package if needed.
//
// The name of the folder in the 'templates' package is used to identify
// templates in '$STEAMPIPE_INSTALL_DIR/templates' - where it is expected
// that a directory with the same name will exist.
//
// We re-write the templates, when there is a higher template version
// available in the 'templates' package.
func EnsureTemplates() error {
log.Println("[TRACE] ensuring check export/output templates")
dirs, err := fs.ReadDir(builtinTemplateFS, "templates")
if err != nil {
return err
}
for _, d := range dirs {
targetDirectory := filepath.Join(filepaths.EnsureTemplateDir(), d.Name())
currentVersionsFilePath := filepath.Join(targetDirectory, "version.json")
embeddedVersionsFilePath := filepath.Join("templates", d.Name(), "version.json")
// check if version in version.json matches with embedded template version
if getCurrentTemplateVersion(currentVersionsFilePath) != getEmbeddedTemplateVersion(embeddedVersionsFilePath) {
log.Printf("[TRACE] versions do not match for dir '%s' - copying updated template\n", d)
if err := writeTemplate(d.Name(), targetDirectory); err != nil {
log.Println("[TRACE] error copying template", err)
return err
}
}
}
return nil
}
func getCurrentTemplateVersion(path string) string {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
log.Println("[TRACE] template version file does not exist - install the new template")
} else {
log.Println("[TRACE] error reading current version file - installing the new template")
}
return ""
}
var ver TemplateVersionFile
err = json.Unmarshal(data, &ver)
if err != nil {
log.Println("[TRACE] error while unmarshaling current version.json file", err)
return ""
}
return ver.Version
}
func getEmbeddedTemplateVersion(path string) string {
data, err := fs.ReadFile(builtinTemplateFS, path)
if err != nil {
log.Println("[TRACE] error reading embedded version file - installing the new template")
return ""
}
var ver TemplateVersionFile
err = json.Unmarshal(data, &ver)
if err != nil {
log.Println("[TRACE] error while unmarshaling json", err)
return ""
}
return ver.Version
}
func writeTemplate(path string, target string) error {
err := os.MkdirAll(target, 0744)
if err != nil {
return err
}
entries, err := fs.ReadDir(builtinTemplateFS, filepath.Join("templates", path))
if err != nil {
return err
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
sourceInEmbedFs := filepath.Join("templates", path, entry.Name())
bytes, err := fs.ReadFile(builtinTemplateFS, sourceInEmbedFs)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(target, entry.Name()), bytes, 0744)
if err != nil {
return err
}
}
return nil
}