-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
benchmark.go
65 lines (53 loc) · 1.42 KB
/
benchmark.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
package dao
import (
"context"
"errors"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/render"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var (
_ Accessor = (*Benchmark)(nil)
_ Nuker = (*Benchmark)(nil)
BenchRx = regexp.MustCompile(`[:|]+`)
)
// Benchmark represents a benchmark resource.
type Benchmark struct {
NonResource
}
// Delete nukes a resource.
func (b *Benchmark) Delete(path string, _ *metav1.DeletionPropagation, force bool) error {
return os.Remove(path)
}
// Get returns a resource.
func (b *Benchmark) Get(context.Context, string) (runtime.Object, error) {
panic("NYI")
}
// List returns a collection of resources.
func (b *Benchmark) List(ctx context.Context, _ string) ([]runtime.Object, error) {
dir, ok := ctx.Value(internal.KeyDir).(string)
if !ok {
return nil, errors.New("no benchmark dir found in context")
}
path, _ := ctx.Value(internal.KeyPath).(string)
ff, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
fileName := BenchRx.ReplaceAllString(strings.Replace(path, "/", "_", 1), "_")
oo := make([]runtime.Object, 0, len(ff))
for _, f := range ff {
if path != "" && !strings.HasPrefix(f.Name(), fileName) {
continue
}
if fi, err := f.Info(); err == nil {
oo = append(oo, render.BenchInfo{File: fi, Path: filepath.Join(dir, f.Name())})
}
}
return oo, nil
}