Repository navigation
Expand file tree
/
Copy pathembed.go
More file actions
74 lines (67 loc) · 1.66 KB
/
Copy pathembed.go
File metadata and controls
74 lines (67 loc) · 1.66 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
71
72
73
74
// Package proxmoxbackup embeds installable project documentation.
package proxmoxbackup
import (
"embed"
"fmt"
"io/fs"
"slices"
"strings"
)
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. The returned DocAsset
// slice is copied, but DocAsset.Data shares the embedded installableDocs backing
// data; callers must treat Data as read-only or copy it before mutation.
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
}
slices.SortFunc(assets, func(a, b DocAsset) int {
return strings.Compare(a.Name, b.Name)
})
return assets, nil
}