-
Notifications
You must be signed in to change notification settings - Fork 18
/
demo.go
123 lines (102 loc) · 3.43 KB
/
demo.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package accessmethods
import (
out "fmt"
"io"
"os"
"strings"
"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/open-component-model/ocm/v2/cmds/demoplugin/common"
"github.com/open-component-model/ocm/v2/cmds/demoplugin/config"
"github.com/open-component-model/ocm/v2/pkg/cobrautils/flagsets"
"github.com/open-component-model/ocm/v2/pkg/contexts/credentials"
"github.com/open-component-model/ocm/v2/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/v2/pkg/contexts/oci/identity"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/accessmethods/options"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/v2/pkg/errors"
"github.com/open-component-model/ocm/v2/pkg/runtime"
)
const (
NAME = "demo"
VERSION = "v1"
)
type AccessSpec struct {
runtime.ObjectVersionedType `json:",inline"`
Path string `json:"path"`
MediaType string `json:"mediaType,omitempty"`
}
const (
OPT_PATH = "path"
OPT_MEDIA = "mediaType"
)
type AccessMethod struct {
ppi.AccessMethodBase
}
var PathOption = options.NewStringOptionType("accessPath", "path in temp repository")
var _ ppi.AccessMethod = (*AccessMethod)(nil)
func New() ppi.AccessMethod {
return &AccessMethod{
AccessMethodBase: ppi.MustNewAccessMethodBase(NAME, "", &AccessSpec{}, "demo access to temp files", ""),
}
}
func (a *AccessMethod) Options() []options.OptionType {
return []options.OptionType{
options.MediatypeOption,
PathOption,
}
}
func (a *AccessMethod) Decode(data []byte, unmarshaler runtime.Unmarshaler) (runtime.TypedObject, error) {
if unmarshaler == nil {
unmarshaler = runtime.DefaultYAMLEncoding
}
var spec AccessSpec
err := unmarshaler.Unmarshal(data, &spec)
if err != nil {
return nil, err
}
return &spec, nil
}
func (a *AccessMethod) ValidateSpecification(p ppi.Plugin, spec ppi.AccessSpec) (*ppi.AccessSpecInfo, error) {
var info ppi.AccessSpecInfo
my := spec.(*AccessSpec)
if my.Path == "" {
return nil, out.Errorf("path not specified")
}
if strings.HasPrefix(my.Path, "/") {
return nil, out.Errorf("path must be relative (%s)", my.Path)
}
if my.MediaType == "" {
return nil, out.Errorf("mediaType not specified")
}
info.MediaType = my.MediaType
info.ConsumerId = credentials.ConsumerIdentity{
cpi.ID_TYPE: common.CONSUMER_TYPE,
identity.ID_HOSTNAME: "localhost",
identity.ID_PATHPREFIX: my.Path,
}
info.Short = "temp file " + my.Path
info.Hint = "temp file " + my.Path
return &info, nil
}
func (a *AccessMethod) ComposeAccessSpecification(p ppi.Plugin, opts ppi.Config, config ppi.Config) error {
list := errors.ErrListf("configuring options")
list.Add(flagsets.AddFieldByOptionP(opts, PathOption, config, "path"))
list.Add(flagsets.AddFieldByOptionP(opts, options.MediatypeOption, config, "mediaType"))
return list.Result()
}
func (a *AccessMethod) Reader(p ppi.Plugin, spec ppi.AccessSpec, creds credentials.Credentials) (io.ReadCloser, error) {
my := spec.(*AccessSpec)
cfg, _ := p.GetConfig()
root := os.TempDir()
if cfg != nil && cfg.(*config.Config).AccessMethods.Path != "" {
root = cfg.(*config.Config).Uploaders.Path
err := os.MkdirAll(root, 0o700)
if err != nil {
return nil, errors.Wrapf(err, "cannot create root dir")
}
}
return os.Open(filepath.Join(root, my.Path))
}