@@ -20,9 +20,11 @@ import (
20
20
"io"
21
21
"io/fs"
22
22
"os"
23
+ "os/exec"
23
24
"path"
24
25
"path/filepath"
25
26
27
+ bffv1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/bff/v1beta1"
26
28
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
27
29
"github.com/lekkodev/cli/pkg/gen"
28
30
"github.com/lekkodev/cli/pkg/repo"
@@ -32,6 +34,7 @@ import (
32
34
"github.com/pkg/errors"
33
35
"github.com/spf13/cobra"
34
36
37
+ "google.golang.org/protobuf/encoding/protojson"
35
38
"google.golang.org/protobuf/proto"
36
39
"google.golang.org/protobuf/reflect/protoregistry"
37
40
"google.golang.org/protobuf/types/descriptorpb"
@@ -286,6 +289,87 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
286
289
return true , nil
287
290
}
288
291
292
+ func isSameTS (ctx context.Context , existing map [string ]map [string ]* featurev1beta1.Feature , registry * protoregistry.Types , root string ) (bool , error ) {
293
+ startingDirectory , err := os .Getwd ()
294
+ defer func () {
295
+ err := os .Chdir (startingDirectory )
296
+ if err != nil {
297
+ panic (err )
298
+ }
299
+ }()
300
+ if err != nil {
301
+ return false , err
302
+ }
303
+ cmd := exec .Command ("npx" , "ts-to-proto" , "--lekko-dir" , "src/lekko" )
304
+ cmd .Dir = root
305
+ nsString , err := cmd .CombinedOutput ()
306
+ if err != nil {
307
+ fmt .Println ("Error running ts-to-proto" )
308
+ return false , err
309
+ }
310
+ var namespaces bffv1beta1.NamespaceContents
311
+ err = protojson.UnmarshalOptions {Resolver : registry }.Unmarshal (nsString , & namespaces )
312
+ if err != nil {
313
+ return false , err
314
+ }
315
+ var notEqual bool
316
+ for _ , namespace := range namespaces .Namespaces {
317
+ existingNs , ok := existing [namespace .Name ]
318
+ if ! ok {
319
+ // New namespace not in existing
320
+ fmt .Println ("New namespace not in existing" )
321
+ return false , nil
322
+ }
323
+ if len (namespace .Configs ) != len (existingNs ) {
324
+ // Mismatched number of configs - perhaps due to addition or removal
325
+ fmt .Printf ("Mismatched number of configs - perhaps due to addition or removal: %d to %d\n " , len (namespace .Configs ), len (existingNs ))
326
+ return false , nil
327
+ }
328
+
329
+ for _ , c := range namespace .Configs {
330
+ f := c .StaticFeature
331
+ if f .GetTree ().GetDefault () != nil {
332
+ f .Tree .DefaultNew = anyToLekkoAny (f .Tree .Default )
333
+ }
334
+ for _ , c := range f .GetTree ().GetConstraints () {
335
+ if c .GetValue () != nil {
336
+ c .ValueNew = anyToLekkoAny (c .Value )
337
+ }
338
+ }
339
+ existingConfig , ok := existingNs [f .Key ]
340
+ if ! ok {
341
+ fmt .Print ("New Config!\n " )
342
+ notEqual = true
343
+ } else if proto .Equal (f .Tree , existingConfig .Tree ) {
344
+ // fmt.Print("Equal! - from proto.Equal\n")
345
+ } else {
346
+ // These might still be equal, because the typescript path combines logical things in ways that the go path does not
347
+ // Using ts since it has fewer args..
348
+ gen .TypeRegistry = registry
349
+ //fmt.Printf("%+v\n\n", f)
350
+ o , err := gen .GenTSForFeature (f , namespace .Name , "" )
351
+ if err != nil {
352
+ return false , err
353
+ }
354
+ e , err := gen .GenTSForFeature (existingConfig , namespace .Name , "" )
355
+ if err != nil {
356
+ return false , err
357
+ }
358
+ if o == e {
359
+ // fmt.Print("Equal! - from codeGen\n")
360
+ } else {
361
+ fmt .Printf ("Not Equal:\n \n %s\n %s\n \n " , o , e )
362
+ notEqual = true
363
+ }
364
+ }
365
+ }
366
+ }
367
+ if notEqual {
368
+ return false , nil
369
+ }
370
+ return true , nil
371
+ }
372
+
289
373
/*
290
374
* Questions we need answered:
291
375
* Is repo main = lekko main?
@@ -298,13 +382,18 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
298
382
299
383
func diffCmd () * cobra.Command {
300
384
var repoPath , basePath , headPath string
385
+ var ts bool
301
386
cmd := & cobra.Command {
302
387
Use : "diff" ,
303
388
Short : "diff" ,
304
389
Hidden : true ,
305
390
RunE : func (cmd * cobra.Command , args []string ) error {
306
391
ctx := cmd .Context ()
307
392
393
+ sameFunc := isSame
394
+ if ts {
395
+ sameFunc = isSameTS
396
+ }
308
397
/*
309
398
existing, registry, err := getRegistryAndNamespacesFromBff(ctx)
310
399
if err != nil {
@@ -316,11 +405,11 @@ func diffCmd() *cobra.Command {
316
405
return err
317
406
}
318
407
// TODO: There might be some way to only need to have one clone of the repository
319
- isHeadSame , err := isSame (ctx , existing , registry , headPath )
408
+ isHeadSame , err := sameFunc (ctx , existing , registry , headPath )
320
409
if err != nil {
321
410
return err
322
411
}
323
- isBaseSame , err := isSame (ctx , existing , registry , basePath )
412
+ isBaseSame , err := sameFunc (ctx , existing , registry , basePath )
324
413
if err != nil {
325
414
return err
326
415
}
@@ -343,6 +432,7 @@ func diffCmd() *cobra.Command {
343
432
cmd .Flags ().StringVarP (& repoPath , "repo-path" , "r" , "" , "path to config repository, will use autodetect if not set" )
344
433
cmd .Flags ().StringVarP (& basePath , "base-path" , "b" , "" , "path to head repository" )
345
434
cmd .Flags ().StringVarP (& headPath , "head-path" , "H" , "" , "path to base repository" )
435
+ cmd .Flags ().BoolVar (& ts , "ts" , false , "typescript mode" )
346
436
return cmd
347
437
}
348
438
0 commit comments