Skip to content

Commit 9353b0b

Browse files
ts-diffing (#392)
1 parent 202565c commit 9353b0b

File tree

1 file changed

+92
-2
lines changed

1 file changed

+92
-2
lines changed

cmd/lekko/sync.go

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"io"
2121
"io/fs"
2222
"os"
23+
"os/exec"
2324
"path"
2425
"path/filepath"
2526

27+
bffv1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/bff/v1beta1"
2628
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
2729
"github.com/lekkodev/cli/pkg/gen"
2830
"github.com/lekkodev/cli/pkg/repo"
@@ -32,6 +34,7 @@ import (
3234
"github.com/pkg/errors"
3335
"github.com/spf13/cobra"
3436

37+
"google.golang.org/protobuf/encoding/protojson"
3538
"google.golang.org/protobuf/proto"
3639
"google.golang.org/protobuf/reflect/protoregistry"
3740
"google.golang.org/protobuf/types/descriptorpb"
@@ -286,6 +289,87 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
286289
return true, nil
287290
}
288291

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+
289373
/*
290374
* Questions we need answered:
291375
* Is repo main = lekko main?
@@ -298,13 +382,18 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
298382

299383
func diffCmd() *cobra.Command {
300384
var repoPath, basePath, headPath string
385+
var ts bool
301386
cmd := &cobra.Command{
302387
Use: "diff",
303388
Short: "diff",
304389
Hidden: true,
305390
RunE: func(cmd *cobra.Command, args []string) error {
306391
ctx := cmd.Context()
307392

393+
sameFunc := isSame
394+
if ts {
395+
sameFunc = isSameTS
396+
}
308397
/*
309398
existing, registry, err := getRegistryAndNamespacesFromBff(ctx)
310399
if err != nil {
@@ -316,11 +405,11 @@ func diffCmd() *cobra.Command {
316405
return err
317406
}
318407
// 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)
320409
if err != nil {
321410
return err
322411
}
323-
isBaseSame, err := isSame(ctx, existing, registry, basePath)
412+
isBaseSame, err := sameFunc(ctx, existing, registry, basePath)
324413
if err != nil {
325414
return err
326415
}
@@ -343,6 +432,7 @@ func diffCmd() *cobra.Command {
343432
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
344433
cmd.Flags().StringVarP(&basePath, "base-path", "b", "", "path to head repository")
345434
cmd.Flags().StringVarP(&headPath, "head-path", "H", "", "path to base repository")
435+
cmd.Flags().BoolVar(&ts, "ts", false, "typescript mode")
346436
return cmd
347437
}
348438

0 commit comments

Comments
 (0)