/
screen_dump.go
60 lines (48 loc) · 1.28 KB
/
screen_dump.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
package dao
import (
"context"
"errors"
"os"
"regexp"
"github.com/kswapd/k13s/internal"
"github.com/kswapd/k13s/internal/render"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var (
_ Accessor = (*ScreenDump)(nil)
_ Nuker = (*ScreenDump)(nil)
// InvalidCharsRX contains invalid filename characters.
invalidPathCharsRX = regexp.MustCompile(`[:]+`)
)
// ScreenDump represents a scraped resources.
type ScreenDump struct {
NonResource
}
// Delete a ScreenDump.
func (d *ScreenDump) Delete(path string, _ *metav1.DeletionPropagation, force bool) error {
return os.Remove(path)
}
// List returns a collection of screen dumps.
func (d *ScreenDump) List(ctx context.Context, _ string) ([]runtime.Object, error) {
dir, ok := ctx.Value(internal.KeyDir).(string)
if !ok {
return nil, errors.New("no screendump dir found in context")
}
ff, err := os.ReadDir(SanitizeFilename(dir))
if err != nil {
return nil, err
}
oo := make([]runtime.Object, len(ff))
for i, f := range ff {
if fi, err := f.Info(); err == nil {
oo[i] = render.FileRes{File: fi, Dir: dir}
}
}
return oo, nil
}
// Helpers...
// SanitizeFilename sanitizes the dump filename.
func SanitizeFilename(name string) string {
return invalidPathCharsRX.ReplaceAllString(name, "-")
}