-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathembed.go
More file actions
70 lines (63 loc) · 1.4 KB
/
embed.go
File metadata and controls
70 lines (63 loc) · 1.4 KB
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
package proxmoxbackup
import (
"embed"
"fmt"
"io/fs"
"sort"
)
var (
//go:embed README.md
embeddedReadme []byte
//go:embed docs
embeddedDocs embed.FS
)
// DocAsset represents an embedded documentation file that can be
// materialized during installation.
type DocAsset struct {
Name string
Data []byte
}
var installableDocs = func() []DocAsset {
builtins := []DocAsset{
{Name: "README.md", Data: embeddedReadme},
}
docAssets, err := collectEmbeddedDocs()
if err != nil {
panic(fmt.Errorf("failed to load embedded docs: %w", err))
}
return append(builtins, docAssets...)
}()
// InstallableDocs returns the list of documentation files embedded in the
// binary that should be written to the installation root.
func InstallableDocs() []DocAsset {
out := make([]DocAsset, len(installableDocs))
copy(out, installableDocs)
return out
}
func collectEmbeddedDocs() ([]DocAsset, error) {
assets := make([]DocAsset, 0, 4)
err := fs.WalkDir(embeddedDocs, "docs", func(path string, d fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if d.IsDir() {
return nil
}
data, err := embeddedDocs.ReadFile(path)
if err != nil {
return err
}
assets = append(assets, DocAsset{
Name: path,
Data: data,
})
return nil
})
if err != nil {
return nil, err
}
sort.Slice(assets, func(i, j int) bool {
return assets[i].Name < assets[j].Name
})
return assets, nil
}