-
Notifications
You must be signed in to change notification settings - Fork 7
/
fs.go
74 lines (64 loc) · 1.36 KB
/
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
// Copyright © Weifeng Wang <[email protected]>
//
// Licensed under the Apache License 2.0.
package source
import (
"archive/tar"
"embed"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
//go:embed .lgtmp.tar
var DirFS embed.FS
//go:generate tar cf .lgtmp.tar .bingo alloy-modules docker-compose kubernetes monitoring-mixins tools Makefile
var GenDir = ".lgtmp"
func init() {
if err := EmbedFsToGenDirectory(); err != nil {
fmt.Print(err)
os.Exit(1)
}
}
func EmbedFsToGenDirectory() error {
if err := os.RemoveAll(GenDir); err != nil {
return err
}
r, err := DirFS.Open(".lgtmp.tar")
if err != nil {
return err
}
defer func() { _ = r.Close() }()
tr := tar.NewReader(r)
for {
hdr, trErr := tr.Next()
if errors.Is(trErr, io.EOF) {
break
}
if trErr != nil {
return trErr
}
target := filepath.Join(GenDir, strings.TrimPrefix(hdr.Name, string(filepath.Separator)))
info := hdr.FileInfo()
if info.IsDir() {
if err = os.MkdirAll(target, 0o777); err != nil {
return err
}
continue
}
w, openErr := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o666|info.Mode()&0o777)
if openErr != nil {
return openErr
}
if _, ioErr := io.Copy(w, tr); ioErr != nil {
_ = w.Close()
return fmt.Errorf("copying %s: %v", target, ioErr)
}
if err = w.Close(); err != nil {
return err
}
}
return nil
}