/
push.go
81 lines (67 loc) · 1.6 KB
/
push.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
package recipe
import (
"context"
"errors"
"strings"
"github.com/futurice/jalapeno/pkg/oci"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/file"
)
const LatestTag = "latest"
var (
ErrorUnauthorized = errors.New("unauthorized")
)
func PushRecipe(ctx context.Context, path string, opts oci.Repository, replaceLatest bool) error {
re, err := LoadRecipe(path)
if err != nil {
return err
}
repo, err := oci.NewRepository(opts)
if err != nil {
return err
}
store, err := file.New("")
if err != nil {
return err
}
defer store.Close()
// TODO: Add each file separately so media types can be set correctly. Also, the recipe directory
// could contain additional files which are not related directly to the recipe.
mediaType := "inode/directory"
desc, err := store.Add(ctx, re.Name, mediaType, path)
if err != nil {
return err
}
fileDescriptors := []v1.Descriptor{desc}
root, err := oras.PackManifest(
ctx,
store,
oras.PackManifestVersion1_1_RC4,
"application/x.futurice.jalapeno.recipe.v1",
oras.PackManifestOptions{
Layers: fileDescriptors,
})
if err != nil {
return err
}
err = store.Tag(ctx, root, re.Version)
if err != nil {
return err
}
destTags := []string{re.Version}
if replaceLatest {
destTags = append(destTags, LatestTag)
}
for _, tag := range destTags {
_, err = oras.Copy(ctx, store, re.Version, repo, tag, oras.DefaultCopyOptions)
if err != nil {
if strings.Contains(err.Error(), "credential required") {
return ErrorUnauthorized
} else {
return err
}
}
}
return nil
}