-
Notifications
You must be signed in to change notification settings - Fork 3
/
a_embed.go
50 lines (46 loc) · 899 Bytes
/
a_embed.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
// Copyright The KCL Authors. All rights reserved.
// kcl plugins.
package kcl_plugin
import (
"embed"
"errors"
"io/fs"
"os"
"path/filepath"
)
//go:embed README.md
//go:embed hello
//go:embed http
var PluginFS embed.FS
func InstallPlugins(root string) error {
embedFS := PluginFS
err := os.MkdirAll(root, 0777)
// If permission denied, ignore it.
if errors.Is(err, fs.ErrPermission) {
return nil
}
if err != nil {
return err
}
return fs.WalkDir(embedFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
abspath := filepath.Join(root, path)
if err := os.MkdirAll(filepath.Dir(abspath), 0777); err != nil {
_ = err
}
data, err := fs.ReadFile(embedFS, path)
if err != nil {
return err
}
err = os.WriteFile(abspath, data, 0777)
if err != nil {
return err
}
return nil
})
}