-
Notifications
You must be signed in to change notification settings - Fork 23
/
attr.go
104 lines (85 loc) · 2.45 KB
/
attr.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package tmpcache
import (
"fmt"
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr"
"github.com/open-component-model/ocm/pkg/runtime"
"github.com/open-component-model/ocm/pkg/utils"
)
const (
ATTR_KEY = "github.com/mandelsoft/tempblobcache"
ATTR_SHORT = "blobcache"
)
func init() {
datacontext.RegisterAttributeType(ATTR_KEY, AttributeType{}, ATTR_SHORT)
}
type AttributeType struct{}
func (a AttributeType) Name() string {
return ATTR_KEY
}
func (a AttributeType) Description() string {
return `
*string* Foldername for temporary blob cache
The temporary blob cache is used to accessing large blobs from remote sytems.
The are temporarily stored in the filesystem, instead of the memory, to avoid
blowing up the memory consumption.
`
}
func (a AttributeType) Encode(v interface{}, marshaller runtime.Marshaler) ([]byte, error) {
if a, ok := v.(*Attribute); !ok {
return nil, fmt.Errorf("temppcache attribute")
} else {
return []byte(a.Path), nil
}
}
func (a AttributeType) Decode(data []byte, unmarshaller runtime.Unmarshaler) (interface{}, error) {
var s string
err := runtime.DefaultYAMLEncoding.Unmarshal(data, &s)
if err != nil {
return nil, errors.Wrapf(err, "invalid attribute value for %s", ATTR_KEY)
}
return &Attribute{
Path: s,
}, nil
}
////////////////////////////////////////////////////////////////////////////////
type Attribute struct {
Path string
Filesystem vfs.FileSystem
}
func New(path string, fss ...vfs.FileSystem) *Attribute {
return &Attribute{
Path: path,
Filesystem: utils.FileSystem(fss...),
}
}
func (a *Attribute) CreateTempFile(pat string) (vfs.File, error) {
err := a.Filesystem.MkdirAll(a.Path, 0o777)
if err != nil {
return nil, err
}
return vfs.TempFile(a.Filesystem, a.Path, pat)
}
////////////////////////////////////////////////////////////////////////////////
func Get(ctx datacontext.Context) *Attribute {
var v interface{}
var fs vfs.FileSystem
if ctx != nil {
v = ctx.GetAttributes().GetAttribute(ATTR_KEY)
fs = utils.FileSystem(vfsattr.Get(ctx))
}
fs = utils.FileSystem(fs)
if v != nil {
a := v.(*Attribute)
if a.Filesystem == nil {
a.Filesystem = fs
}
return a
}
return &Attribute{fs.FSTempDir(), fs}
}
func Set(ctx datacontext.Context, a *Attribute) {
ctx.GetAttributes().SetAttribute(ATTR_KEY, a)
}