8
8
"archive/zip"
9
9
"bytes"
10
10
"context"
11
+ "encoding/json"
11
12
"fmt"
12
13
"io"
13
14
"log"
@@ -23,6 +24,10 @@ import (
23
24
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
24
25
)
25
26
27
+ const (
28
+ cueModuleAnnotationFileEnv = "ANDUIN_CUE_MODULE_ANNOTATION_FILE"
29
+ )
30
+
26
31
var logging , _ = strconv .ParseBool (os .Getenv ("ANDUIN_CUE_DEBUG" ))
27
32
28
33
type anduinPatch struct {
@@ -87,6 +92,7 @@ func (p *anduinPatch) repackZipFile(repackZip *os.File, ctx context.Context, m *
87
92
Size : int64 (len (data )),
88
93
Annotations : annotation ,
89
94
}
95
+ logf ("pushing data layer for file %s" , zf .Name )
90
96
if _ , err := loc .Registry .PushBlob (ctx , loc .Repository , dataLayer , bytes .NewReader (data )); err != nil {
91
97
return fmt .Errorf ("cannot push oras data layer: %v" , err )
92
98
}
@@ -124,6 +130,36 @@ func (p *anduinPatch) repackZipFile(repackZip *os.File, ctx context.Context, m *
124
130
return orasLayers , nil
125
131
}
126
132
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
+
127
163
func shouldRepackFile (zf * zip.File ) bool {
128
164
return strings .HasSuffix (zf .Name , ".cue" ) || strings .ToLower (zf .Name ) == "license"
129
165
}
@@ -162,6 +198,15 @@ func validateModVersion(mv module.Version, mf *modfile.File) (string, bool) {
162
198
return major , true
163
199
}
164
200
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
+
165
210
func logf (f string , a ... any ) {
166
211
if logging {
167
212
log .Printf ("anduin: " + f , a ... )
0 commit comments