Skip to content

Commit 14f2479

Browse files
committed
feat: allow to read manifest annotations from file
1 parent de4d84e commit 14f2479

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed
+6-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
MODULE_1_VERSION:=v1.0.3
2+
MODULE_2_VERSION:=v1.0.0
3+
14
nuke:
25
sudo rm -rf ./cue.cache/mod && \
36
rm -rf ./data/* && \
47
mkdir -p ./cue.cache ./data
58

69
publish-module1:
710
cd ./module1 && \
8-
../cue.sh mod publish v1.0.1
11+
../cue.sh mod publish ${MODULE_1_VERSION}
912

1013
publish-module2:
1114
cd ./module2 && \
12-
../cue.sh mod publish v1.0.0
15+
../cue.sh mod publish ${MODULE_2_VERSION}
1316

1417
eval-module2:
1518
cd ./module2 && \
@@ -18,4 +21,4 @@ eval-module2:
1821

1922
oras-pull:
2023
cd data && \
21-
oras pull localhost:5000/anduin.com/module1:v1.0.1
24+
oras pull localhost:5000/anduin.com/module1:${MODULE_1_VERSION}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"$manifest": {
3+
"org.opencontainers.image.authors": "tuanvuong@anduintransact.com"
4+
}
5+
}

examples/anduin/multi-modules/module2/cue.mod/module.cue

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ source: {
77
}
88
deps: {
99
"dev.anduintransact.com/module1@v1": {
10-
v: "v1.0.1"
10+
v: "v1.0.3"
1111
default: true
1212
}
1313
}

mod/modregistry/anduin.go

+45
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"archive/zip"
99
"bytes"
1010
"context"
11+
"encoding/json"
1112
"fmt"
1213
"io"
1314
"log"
@@ -23,6 +24,10 @@ import (
2324
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2425
)
2526

27+
const (
28+
cueModuleAnnotationFileEnv = "ANDUIN_CUE_MODULE_ANNOTATION_FILE"
29+
)
30+
2631
var logging, _ = strconv.ParseBool(os.Getenv("ANDUIN_CUE_DEBUG"))
2732

2833
type anduinPatch struct {
@@ -87,6 +92,7 @@ func (p *anduinPatch) repackZipFile(repackZip *os.File, ctx context.Context, m *
8792
Size: int64(len(data)),
8893
Annotations: annotation,
8994
}
95+
logf("pushing data layer for file %s", zf.Name)
9096
if _, err := loc.Registry.PushBlob(ctx, loc.Repository, dataLayer, bytes.NewReader(data)); err != nil {
9197
return fmt.Errorf("cannot push oras data layer: %v", err)
9298
}
@@ -124,6 +130,36 @@ func (p *anduinPatch) repackZipFile(repackZip *os.File, ctx context.Context, m *
124130
return orasLayers, nil
125131
}
126132

133+
func (p *anduinPatch) mergeManifestAnnotations(annotations map[string]string) map[string]string {
134+
annotationsFile := resolveModuleAnnotationFile()
135+
f, err := os.Open(annotationsFile)
136+
if err != nil && !os.IsNotExist(err) {
137+
logf("warn: unable to open manifest annotations file. File: `%s`. Err: %v", annotationsFile, err)
138+
return annotations
139+
}
140+
defer f.Close()
141+
logf("reading manifest annotations file: %s", annotationsFile)
142+
143+
r := json.NewDecoder(f)
144+
var content struct {
145+
Manifest map[string]string `json:"$manifest"`
146+
}
147+
if err := r.Decode(&content); err != nil {
148+
logf("warn: unable to parse manifest annotations file. Err :%v", err)
149+
return annotations
150+
}
151+
152+
// merging values
153+
if annotations == nil {
154+
annotations = map[string]string{}
155+
}
156+
annotations["com.anduintransact.annotations.file"] = annotationsFile
157+
for key, val := range content.Manifest {
158+
annotations[key] = val
159+
}
160+
return annotations
161+
}
162+
127163
func shouldRepackFile(zf *zip.File) bool {
128164
return strings.HasSuffix(zf.Name, ".cue") || strings.ToLower(zf.Name) == "license"
129165
}
@@ -162,6 +198,15 @@ func validateModVersion(mv module.Version, mf *modfile.File) (string, bool) {
162198
return major, true
163199
}
164200

201+
func resolveModuleAnnotationFile() string {
202+
envFile := os.Getenv(cueModuleAnnotationFileEnv)
203+
if envFile != "" {
204+
return envFile
205+
}
206+
// default to cwd `annotations.json`
207+
return "annotations.json"
208+
}
209+
165210
func logf(f string, a ...any) {
166211
if logging {
167212
log.Printf("anduin: "+f, a...)

mod/modregistry/client.go

+2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ func (c *Client) putCheckedModule(ctx context.Context, m *checkedModule, meta *M
250250
}
251251
annotations = annotations0
252252
}
253+
annotations = c.anduin.mergeManifestAnnotations(annotations)
254+
253255
loc, err := c.resolve(m.mv)
254256
if err != nil {
255257
return err

0 commit comments

Comments
 (0)