@@ -17,6 +17,7 @@ package sync
17
17
import (
18
18
"context"
19
19
"io"
20
+ "os"
20
21
"path/filepath"
21
22
"strings"
22
23
@@ -25,6 +26,7 @@ import (
25
26
"github.com/lekkodev/cli/pkg/proto"
26
27
"github.com/lekkodev/cli/pkg/repo"
27
28
"github.com/lekkodev/cli/pkg/star"
29
+ "github.com/lekkodev/cli/pkg/star/prototypes"
28
30
"github.com/lekkodev/cli/pkg/star/static"
29
31
"github.com/lekkodev/go-sdk/pkg/eval"
30
32
"github.com/pkg/errors"
@@ -46,11 +48,17 @@ func WriteContentsToLocalRepo(ctx context.Context, contents *featurev1beta1.Repo
46
48
if err != nil {
47
49
return errors .Wrap (err , "prepare repo" )
48
50
}
51
+ return WriteContentsToRepo (ctx , contents , r )
52
+ }
53
+
54
+ func WriteContentsToRepo (ctx context.Context , contents * featurev1beta1.RepositoryContents , r repo.ConfigurationRepository ) error {
49
55
// Discard logs, mainly for silencing compilation later
50
- // TODO: Maybe a verbose flag
51
- r .ConfigureLogger (& repo.LoggingConfiguration {
52
- Writer : io .Discard ,
56
+ // TODO: Allow passing in writer
57
+ clearFn := r .ConfigureLogger (& repo.LoggingConfiguration {
58
+ Writer : io .Discard ,
59
+ ColorsDisabled : true ,
53
60
})
61
+ defer clearFn ()
54
62
rootMD , _ , err := r .ParseMetadata (ctx )
55
63
if err != nil {
56
64
return err
@@ -159,8 +167,35 @@ func WriteContentsToLocalRepo(ctx context.Context, contents *featurev1beta1.Repo
159
167
if err := WriteTypesToRepo (ctx , contents .FileDescriptorSet , r ); err != nil {
160
168
return errors .Wrap (err , "write type files" )
161
169
}
162
- if _ , err := r .ReBuildDynamicTypeRegistry (ctx , rootMD .ProtoDirectory , false ); err != nil {
163
- return errors .Wrap (err , "final rebuild type registry" )
170
+ // NOTE: This is a workaround to correctly rebuild the type registry in ephemeral repositories.
171
+ // Because ephemeral repos use an in-mem fs, we can't run the buf cmd line on its files.
172
+ // So we copy the proto dir to a temp dir in the real fs, build there, then copy the image back.
173
+ // Still does require the buf CLI to be available.
174
+ tmpProtoDir , err := os .MkdirTemp ("" , "proto" )
175
+ if err != nil {
176
+ return errors .Wrap (err , "create tmp proto dir" )
177
+ }
178
+ defer os .RemoveAll (tmpProtoDir )
179
+ if err := copyDirFiles (ctx , r , rootMD .ProtoDirectory , tmpProtoDir ); err != nil {
180
+ return errors .Wrap (err , "copy proto files" )
181
+ }
182
+ bufIn := filepath .Join (tmpProtoDir , rootMD .ProtoDirectory )
183
+ if err := prototypes .BufLint (bufIn ); err != nil {
184
+ return errors .Wrap (err , "lint protos" )
185
+ }
186
+ bufImage , err := prototypes .NewBufImage (bufIn )
187
+ if err != nil {
188
+ return errors .Wrap (err , "build protos" )
189
+ }
190
+ bufContents , err := os .ReadFile (bufImage .Filename )
191
+ if err != nil {
192
+ return errors .Wrap (err , "read buf image" )
193
+ }
194
+ if err := r .WriteFile (filepath .Join (rootMD .ProtoDirectory , filepath .Base (bufImage .Filename )), bufContents , 0644 ); err != nil {
195
+ return errors .Wrap (err , "copy back buf image" )
196
+ }
197
+ if _ , err := r .BuildDynamicTypeRegistry (ctx , rootMD .ProtoDirectory ); err != nil {
198
+ return errors .Wrap (err , "final build type registry" )
164
199
}
165
200
166
201
// Final compile to verify overall health
@@ -206,3 +241,40 @@ func WriteTypesToRepo(ctx context.Context, fds *descriptorpb.FileDescriptorSet,
206
241
})
207
242
return writeErr
208
243
}
244
+
245
+ // Copies a directory in a configuration repository to a directory on the OS filesystem.
246
+ // e.g. <dir> -> <dst>/<dir>
247
+ func copyDirFiles (ctx context.Context , r repo.ConfigurationRepository , dir string , dst string ) error {
248
+ pfs , err := r .GetDirContents (ctx , dir )
249
+ if err != nil {
250
+ return errors .Wrap (err , "get dir contents" )
251
+ }
252
+ dstDir := filepath .Join (dst , dir )
253
+ if err := os .MkdirAll (dstDir , 0777 ); err != nil {
254
+ return errors .Wrapf (err , "mkdir %s" , dstDir )
255
+ }
256
+ for _ , pf := range pfs {
257
+ dstPath := filepath .Join (dst , pf .Path )
258
+ if pf .IsDir {
259
+ if err := os .MkdirAll (dstPath , 0777 ); err != nil {
260
+ return errors .Wrapf (err , "mkdir %s" , pf .Path )
261
+ }
262
+ if err := copyDirFiles (ctx , r , pf .Path , dst ); err != nil {
263
+ return err
264
+ }
265
+ continue
266
+ }
267
+ b , err := r .Read (pf .Path )
268
+ if err != nil {
269
+ return errors .Wrapf (err , "read %s" , pf .Path )
270
+ }
271
+ f , err := os .Create (dstPath )
272
+ if err != nil {
273
+ return errors .Wrapf (err , "create %s" , dstPath )
274
+ }
275
+ if _ , err := f .Write (b ); err != nil {
276
+ return errors .Wrapf (err , "copy %s" , dstPath )
277
+ }
278
+ }
279
+ return nil
280
+ }
0 commit comments