forked from open-component-model/ocm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
70 lines (59 loc) · 1.94 KB
/
example.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
package main
import (
"fmt"
"github.com/mandelsoft/goutils/errors"
"github.com/open-component-model/ocm/examples/lib/helper"
"github.com/open-component-model/ocm/pkg/blobaccess"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/ocireg"
"github.com/open-component-model/ocm/pkg/contexts/ocm/resourcetypes"
"github.com/open-component-model/ocm/pkg/mime"
)
func SimpleWriteWithCredentials() error {
cfg, err := helper.ReadConfig(CFG)
if err != nil {
return err
}
octx := ocm.DefaultContext()
repoSpec := ocireg.NewRepositorySpec(cfg.Repository, nil)
repo, err := octx.RepositoryForSpec(repoSpec, cfg.GetCredentials())
if err != nil {
return err
}
defer repo.Close()
comp, err := repo.LookupComponent(cfg.Component)
if err != nil {
return errors.Wrapf(err, "cannot lookup component %s", cfg.Component)
}
defer comp.Close()
compvers, err := comp.NewVersion(cfg.Version, true)
if err != nil {
return errors.Wrapf(err, "cannot create new version %s", cfg.Version)
}
defer compvers.Close()
// add provider information
compvers.GetDescriptor().Provider = metav1.Provider{Name: "mandelsoft"}
// add a new resource artifact with the local identity `name="test"`.
err = compvers.SetResourceBlob(
&compdesc.ResourceMeta{
ElementMeta: compdesc.ElementMeta{
Name: "test",
},
Type: resourcetypes.BLOB,
Relation: metav1.LocalRelation,
},
blobaccess.ForString(mime.MIME_TEXT, "testdata"),
"", nil,
)
if err != nil {
return errors.Wrapf(err, "cannot add resource")
}
// finally push the new component version
if err = comp.AddVersion(compvers); err != nil {
return errors.Wrapf(err, "cannot add new version")
}
fmt.Printf("added component %s version %s\n", cfg.Component, cfg.Version)
return nil
}