-
Notifications
You must be signed in to change notification settings - Fork 20
/
attr.go
78 lines (65 loc) · 1.9 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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package cacheattr
import (
"fmt"
"os"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
"github.com/open-component-model/ocm/pkg/utils"
)
const (
ATTR_KEY = "github.com/mandelsoft/oci/cache"
ATTR_SHORT = "cache"
)
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*
Filesystem folder to use for caching OCI blobs
`
}
func (a AttributeType) Encode(v interface{}, marshaller runtime.Marshaler) ([]byte, error) {
if _, ok := v.(accessio.BlobCache); !ok {
return nil, fmt.Errorf("accessio.BlobCache required")
}
return nil, nil
}
func (a AttributeType) Decode(data []byte, unmarshaller runtime.Unmarshaler) (interface{}, error) {
var value string
err := unmarshaller.Unmarshal(data, &value)
if value != "" {
value, err = utils.ResolvePath(value)
if err != nil {
return nil, err
}
// TODO: This should use the virtual filesystem.
err = os.MkdirAll(value, 0o700)
if err == nil {
return accessio.NewStaticBlobCache(value)
}
} else if err == nil {
err = errors.Newf("file path missing")
}
return value, err
}
////////////////////////////////////////////////////////////////////////////////
func Get(ctx datacontext.Context) accessio.BlobCache {
a := ctx.GetAttributes().GetAttribute(ATTR_KEY)
if a == nil {
return nil
}
return a.(accessio.BlobCache)
}
func Set(ctx datacontext.Context, cache accessio.BlobCache) error {
return ctx.GetAttributes().SetAttribute(ATTR_KEY, cache)
}