forked from open-component-model/ocm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
94 lines (81 loc) · 2.61 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"github.com/mandelsoft/goutils/errors"
"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/ocm"
"ocm.software/ocm/api/ocm/compdesc"
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
"ocm.software/ocm/api/ocm/extensions/accessmethods/ociartifact"
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/repositories/ocireg"
"ocm.software/ocm/api/tech/oci/identity"
"ocm.software/ocm/api/utils/blobaccess"
"ocm.software/ocm/api/utils/mime"
"ocm.software/ocm/examples/lib/helper"
)
func SimpleWriteWithCredentials() (ferr error) {
cfg, err := helper.ReadConfig(CFG)
if err != nil {
return err
}
octx := ocm.DefaultContext()
octx.CredentialsContext().SetCredentialsForConsumer(
credentials.NewConsumerIdentity(identity.CONSUMER_TYPE,
identity.ID_HOSTNAME, "ghcr.io",
identity.ID_PATHPREFIX, "mandelsoft",
),
cfg.GetCredentials(),
)
repoSpec := ocireg.NewRepositorySpec(cfg.Repository, nil)
repo, err := octx.RepositoryForSpec(repoSpec)
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 errors.PropagateError(&ferr, 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")
}
imageAccess := ociartifact.New("ghcr.io/open-component-model/ocm/ocm.software/toi/installers/helminstaller/helminstaller:0.4.0")
err = compvers.SetResource(&compdesc.ResourceMeta{
ElementMeta: compdesc.ElementMeta{
Name: "helminstaller",
Version: "0.4.0",
},
Type: resourcetypes.OCI_IMAGE,
Relation: metav1.ExternalRelation,
}, imageAccess)
if err != nil {
return errors.Wrapf(err, "cannot add image 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 to %s\n", cfg.Component, cfg.Version, cfg.Repository)
return nil
}