Skip to content

Commit 706a279

Browse files
authored
gen: autodetect language, unify logic to list namespaces (#397)
same as #394 + fixes to make `push`/`pull` work - `lekko gen` can autodetect language now - consolidated the logic to list namespaces - refactored `diff` command to use .lekko
1 parent bc321ca commit 706a279

File tree

9 files changed

+295
-308
lines changed

9 files changed

+295
-308
lines changed

cmd/lekko/bisync.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/lainio/err2/try"
2121
"github.com/lekkodev/cli/pkg/dotlekko"
22+
"github.com/lekkodev/cli/pkg/native"
2223
"github.com/lekkodev/cli/pkg/repo"
2324
"github.com/lekkodev/cli/pkg/sync"
2425
"github.com/pkg/errors"
@@ -35,7 +36,7 @@ func bisyncCmd() *cobra.Command {
3536
Files at the provided path that contain valid Lekko config functions will first be translated and synced to the config repository on the local filesystem, then translated back to Lekko-canonical form, performing any code generation as necessary.
3637
This may affect ordering of functions/parameters and formatting.`,
3738
RunE: func(cmd *cobra.Command, args []string) error {
38-
nativeLang := try.To1(sync.DetectNativeLang())
39+
nativeLang := try.To1(native.DetectNativeLang())
3940
return bisync(context.Background(), nativeLang, lekkoPath, repoPath)
4041
},
4142
}
@@ -46,7 +47,7 @@ This may affect ordering of functions/parameters and formatting.`,
4647
return cmd
4748
}
4849

49-
func bisync(ctx context.Context, nativeLang sync.NativeLang, lekkoPath, repoPath string) error {
50+
func bisync(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath string) error {
5051
if len(lekkoPath) == 0 {
5152
dot := try.To1(dotlekko.ReadDotLekko())
5253
lekkoPath = dot.LekkoPath
@@ -55,9 +56,9 @@ func bisync(ctx context.Context, nativeLang sync.NativeLang, lekkoPath, repoPath
5556
repoPath = try.To1(repo.PrepareGithubRepo())
5657
}
5758
switch nativeLang {
58-
case sync.GO:
59+
case native.GO:
5960
_ = try.To1(sync.BisyncGo(ctx, lekkoPath, lekkoPath, repoPath))
60-
case sync.TS:
61+
case native.TS:
6162
try.To(sync.BisyncTS(lekkoPath, repoPath))
6263
default:
6364
return errors.New("unsupported language")
@@ -71,7 +72,7 @@ func bisyncGoCmd() *cobra.Command {
7172
Use: "go",
7273
Short: "Lekko bisync for Go. Should be run from project root.",
7374
RunE: func(cmd *cobra.Command, args []string) error {
74-
return bisync(context.Background(), sync.GO, lekkoPath, repoPath)
75+
return bisync(context.Background(), native.GO, lekkoPath, repoPath)
7576
},
7677
}
7778
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
@@ -85,7 +86,7 @@ func bisyncTSCmd() *cobra.Command {
8586
Use: "ts",
8687
Short: "Lekko bisync for Typescript. Should be run from project root.",
8788
RunE: func(cmd *cobra.Command, args []string) error {
88-
return bisync(context.Background(), sync.TS, lekkoPath, repoPath)
89+
return bisync(context.Background(), native.TS, lekkoPath, repoPath)
8990
},
9091
}
9192
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")

cmd/lekko/gen.go

Lines changed: 35 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,18 @@ package main
1717
import (
1818
"context"
1919
"fmt"
20-
"log"
21-
"os"
2220
"path"
2321
"path/filepath"
24-
"regexp"
2522
"strings"
2623

2724
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
28-
"golang.org/x/mod/modfile"
2925

26+
"github.com/lainio/err2"
3027
"github.com/lainio/err2/try"
3128
"github.com/lekkodev/cli/pkg/dotlekko"
3229
"github.com/lekkodev/cli/pkg/feature"
3330
"github.com/lekkodev/cli/pkg/gen"
31+
"github.com/lekkodev/cli/pkg/native"
3432
"github.com/lekkodev/cli/pkg/repo"
3533
"github.com/lekkodev/cli/pkg/star"
3634
"github.com/lekkodev/cli/pkg/star/static"
@@ -41,85 +39,55 @@ import (
4139
)
4240

4341
func genCmd() *cobra.Command {
42+
var lekkoPath, repoPath, ns string
43+
var initMode bool
4444
cmd := &cobra.Command{
4545
Use: "gen",
46-
Short: "generate library code from configs",
46+
Short: "generate Lekko config functions from a local config repository",
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
nativeLang := try.To1(native.DetectNativeLang())
49+
return genNative(context.Background(), nativeLang, lekkoPath, repoPath, ns, initMode)
50+
},
4751
}
52+
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
53+
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
54+
cmd.Flags().StringVarP(&ns, "namespace", "n", "", "namespace to generate code from")
55+
cmd.Flags().BoolVar(&initMode, "init", false, "pass 'init' to generate boilerplate code for a Lekko namespace")
4856
cmd.AddCommand(genGoCmd())
4957
cmd.AddCommand(genTSCmd())
5058
cmd.AddCommand(genStarlarkCmd())
5159
return cmd
5260
}
5361

62+
func genNative(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath, ns string, initMode bool) (err error) {
63+
defer err2.Handle(&err)
64+
if len(lekkoPath) == 0 {
65+
dot := try.To1(dotlekko.ReadDotLekko())
66+
lekkoPath = dot.LekkoPath
67+
}
68+
if len(repoPath) == 0 {
69+
repoPath = try.To1(repo.PrepareGithubRepo())
70+
}
71+
var namespaces []string
72+
if len(ns) > 0 {
73+
namespaces = append(namespaces, ns)
74+
}
75+
return gen.GenNative(ctx, nativeLang, lekkoPath, repoPath, namespaces, ".", initMode)
76+
}
77+
5478
func genGoCmd() *cobra.Command {
55-
var ns string
56-
var outputPath string
57-
var repoPath string
79+
var lekkoPath, repoPath, ns string
5880
var initMode bool
5981
cmd := &cobra.Command{
6082
Use: "go",
6183
Short: "generate Go library code from configs",
6284
RunE: func(cmd *cobra.Command, args []string) error {
63-
b, err := os.ReadFile("go.mod")
64-
if err != nil {
65-
return errors.Wrap(err, "find go.mod in working directory")
66-
}
67-
mf, err := modfile.ParseLax("go.mod", b, nil)
68-
if err != nil {
69-
return err
70-
}
71-
if len(outputPath) == 0 {
72-
dot, err := dotlekko.ReadDotLekko()
73-
if err != nil {
74-
return err
75-
}
76-
outputPath = dot.LekkoPath
77-
}
78-
if len(repoPath) == 0 {
79-
repoPath, err = repo.PrepareGithubRepo()
80-
if err != nil {
81-
return err
82-
}
83-
}
84-
var namespaces []string
85-
if len(ns) == 0 {
86-
files, err := os.ReadDir(outputPath)
87-
if err != nil {
88-
log.Fatal(err)
89-
}
90-
for _, f := range files {
91-
if f.IsDir() && f.Name() != "proto" {
92-
namespaces = append(namespaces, f.Name())
93-
}
94-
}
95-
} else {
96-
namespaces = []string{ns}
97-
}
98-
for _, n := range namespaces {
99-
if !regexp.MustCompile("[a-z]+").MatchString(n) {
100-
return errors.New("namespace must be a lowercase alphanumeric string")
101-
}
102-
generator, err := gen.NewGoGenerator(mf.Module.Mod.Path, outputPath, outputPath, repoPath, n)
103-
if err != nil {
104-
return errors.Wrap(err, "initialize code generator")
105-
}
106-
if initMode {
107-
err = generator.Init(cmd.Context())
108-
if err != nil {
109-
return err
110-
}
111-
}
112-
err = generator.Gen(cmd.Context())
113-
if err != nil {
114-
return err
115-
}
116-
}
117-
return nil
85+
return genNative(cmd.Context(), native.GO, lekkoPath, repoPath, ns, initMode)
11886
},
11987
}
88+
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
89+
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
12090
cmd.Flags().StringVarP(&ns, "namespace", "n", "", "namespace to generate code from")
121-
cmd.Flags().StringVarP(&outputPath, "output-path", "o", "", "path to write generated directories and Go files under, autodetects if not set")
122-
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, autodetects if not set")
12391
cmd.Flags().BoolVar(&initMode, "init", false, "pass 'init' to generate boilerplate code for a Lekko namespace")
12492
return cmd
12593
}
@@ -132,19 +100,12 @@ func genTSCmd() *cobra.Command {
132100
Use: "ts",
133101
Short: "generate typescript library code from configs",
134102
RunE: func(cmd *cobra.Command, args []string) error {
135-
if len(lekkoPath) == 0 {
136-
dot := try.To1(dotlekko.ReadDotLekko())
137-
lekkoPath = dot.LekkoPath
138-
}
139-
if len(repoPath) == 0 {
140-
repoPath = try.To1(repo.PrepareGithubRepo())
141-
}
142-
return gen.GenFormattedTS(cmd.Context(), repoPath, ns, filepath.Join(lekkoPath, ns+".ts"))
103+
return genNative(cmd.Context(), native.TS, lekkoPath, repoPath, ns, false)
143104
},
144105
}
145-
cmd.Flags().StringVarP(&ns, "namespace", "n", "default", "namespace to generate code from")
146106
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "path to Lekko native config files, will use autodetect if not set")
147107
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
108+
cmd.Flags().StringVarP(&ns, "namespace", "n", "default", "namespace to generate code from")
148109
return cmd
149110
}
150111

cmd/lekko/repo.go

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ import (
2929
"github.com/AlecAivazis/survey/v2"
3030
"github.com/go-git/go-git/v5"
3131
"github.com/go-git/go-git/v5/plumbing"
32+
"github.com/lainio/err2"
3233
"github.com/lainio/err2/try"
3334
"github.com/lekkodev/cli/pkg/dotlekko"
35+
"github.com/lekkodev/cli/pkg/gen"
3436
"github.com/lekkodev/cli/pkg/gh"
3537
"github.com/lekkodev/cli/pkg/gitcli"
3638
"github.com/lekkodev/cli/pkg/lekko"
3739
"github.com/lekkodev/cli/pkg/logging"
40+
"github.com/lekkodev/cli/pkg/native"
3841
"github.com/lekkodev/cli/pkg/repo"
3942
"github.com/lekkodev/cli/pkg/secrets"
4043
"github.com/lekkodev/cli/pkg/sync"
@@ -427,37 +430,20 @@ func pullCmd() *cobra.Command {
427430
cmd := &cobra.Command{
428431
Use: "pull",
429432
Short: "Pull remote changes and merge them with local changes.",
430-
RunE: func(cmd *cobra.Command, args []string) error {
431-
dot, err := dotlekko.ReadDotLekko()
432-
if err != nil {
433-
return errors.Wrap(err, "read Lekko configuration file")
434-
}
433+
RunE: func(cmd *cobra.Command, args []string) (err error) {
434+
defer err2.Handle(&err)
435435

436-
nativeLang, err := sync.DetectNativeLang()
437-
if err != nil {
438-
return err
439-
}
436+
dot := try.To1(dotlekko.ReadDotLekko())
437+
nativeLang := try.To1(native.DetectNativeLang())
440438

441-
repoPath, err := repo.PrepareGithubRepo()
442-
if err != nil {
443-
return err
444-
}
445-
gitRepo, err := git.PlainOpen(repoPath)
446-
if err != nil {
447-
return errors.Wrap(err, "open git repo")
448-
}
439+
repoPath := try.To1(repo.PrepareGithubRepo())
440+
gitRepo := try.To1(git.PlainOpen(repoPath))
449441
// this should be safe as we generate all changes from native lang
450442
// git reset --hard
451443
// git clean -fd
452-
err = repo.ResetAndClean(gitRepo)
453-
if err != nil {
454-
return errors.Wrap(err, "reset and clean")
455-
}
444+
try.To(repo.ResetAndClean(gitRepo))
456445

457-
worktree, err := gitRepo.Worktree()
458-
if err != nil {
459-
return errors.Wrap(err, "get worktree")
460-
}
446+
worktree := try.To1(gitRepo.Worktree())
461447
err = worktree.Checkout(&git.CheckoutOptions{
462448
Branch: plumbing.NewBranchReferenceName("main"),
463449
})
@@ -466,22 +452,13 @@ func pullCmd() *cobra.Command {
466452
}
467453

468454
// pull from remote
469-
remotes, err := gitRepo.Remotes()
470-
if err != nil {
471-
return errors.Wrap(err, "get remotes")
472-
}
455+
remotes := try.To1(gitRepo.Remotes())
473456
if len(remotes) == 0 {
474457
return errors.New("No remote found, please finish setup instructions")
475458
}
476459
fmt.Printf("Pulling from %s\n", remotes[0].Config().URLs[0])
477-
err = gitcli.Pull(repoPath)
478-
if err != nil {
479-
return errors.Wrap(err, "git pull")
480-
}
481-
newHead, err := gitRepo.Head()
482-
if err != nil {
483-
return err
484-
}
460+
try.To(gitcli.Pull(repoPath))
461+
newHead := try.To1(gitRepo.Head())
485462

486463
lekkoPath := dot.LekkoPath
487464
if len(dot.LockSHA) == 0 || force {
@@ -498,17 +475,7 @@ func pullCmd() *cobra.Command {
498475
return fmt.Errorf("please commit or stash changes in '%s' before pulling", lekkoPath)
499476
}
500477
}
501-
nativeFiles, err := repo.ListNativeConfigFiles(lekkoPath, nativeLang.Ext())
502-
if err != nil {
503-
return err
504-
}
505-
for _, f := range nativeFiles {
506-
ns := nativeLang.GetNamespace(f)
507-
err := sync.GenNative(cmd.Context(), nativeLang, dot.LekkoPath, repoPath, ns, ".")
508-
if err != nil {
509-
return err
510-
}
511-
}
478+
try.To(gen.GenNative(cmd.Context(), nativeLang, dot.LekkoPath, repoPath, nil, ".", false))
512479

513480
dot.LockSHA = newHead.Hash().String()
514481
if err := dot.WriteBack(); err != nil {
@@ -525,14 +492,14 @@ func pullCmd() *cobra.Command {
525492
fmt.Printf("Rebasing from %s to %s\n\n", dot.LockSHA, newHead.Hash().String())
526493

527494
switch nativeLang {
528-
case sync.TS:
495+
case native.TS:
529496
tsPullCmd := exec.Command("npx", "lekko-repo-pull", "--lekko-dir", lekkoPath)
530497
output, err := tsPullCmd.CombinedOutput()
531498
fmt.Println(string(output))
532499
if err != nil {
533500
return errors.Wrap(err, "ts pull")
534501
}
535-
case sync.GO:
502+
case native.GO:
536503
files, err := sync.BisyncGo(cmd.Context(), lekkoPath, lekkoPath, repoPath)
537504
if err != nil {
538505
return errors.Wrap(err, "go bisync")
@@ -559,12 +526,13 @@ func pullCmd() *cobra.Command {
559526
return cmd
560527
}
561528

562-
func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) error {
563-
nativeLang, err := sync.NativeLangFromExt(filename)
529+
func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) (err error) {
530+
defer err2.Handle(&err)
531+
nativeLang, err := native.NativeLangFromExt(filename)
564532
if err != nil {
565533
return err
566534
}
567-
ns := nativeLang.GetNamespace(filename)
535+
ns := try.To1(nativeLang.GetNamespace(filename))
568536

569537
fileBytes, err := os.ReadFile(filename)
570538
if err != nil {
@@ -608,7 +576,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) err
608576
return errors.Wrap(err, "create temp dir")
609577
}
610578
defer os.RemoveAll(baseDir)
611-
err = sync.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, ns, baseDir)
579+
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, []string{ns}, baseDir, false)
612580
if err != nil {
613581
return errors.Wrap(err, "gen native")
614582
}
@@ -644,7 +612,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) err
644612
return errors.Wrap(err, "create temp dir")
645613
}
646614
defer os.RemoveAll(remoteDir)
647-
err = sync.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, ns, remoteDir)
615+
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, []string{ns}, remoteDir, false)
648616
if err != nil {
649617
return errors.Wrap(err, "gen native")
650618
}

0 commit comments

Comments
 (0)