Skip to content

Commit 972134f

Browse files
authored
Revert "gne: autodetect language, unify logic to list namespaces (#394)" (#396)
This reverts commit e27d8dd.
1 parent e27d8dd commit 972134f

File tree

9 files changed

+308
-293
lines changed

9 files changed

+308
-293
lines changed

cmd/lekko/bisync.go

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

2020
"github.com/lainio/err2/try"
2121
"github.com/lekkodev/cli/pkg/dotlekko"
22-
"github.com/lekkodev/cli/pkg/native"
2322
"github.com/lekkodev/cli/pkg/repo"
2423
"github.com/lekkodev/cli/pkg/sync"
2524
"github.com/pkg/errors"
@@ -36,7 +35,7 @@ func bisyncCmd() *cobra.Command {
3635
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.
3736
This may affect ordering of functions/parameters and formatting.`,
3837
RunE: func(cmd *cobra.Command, args []string) error {
39-
nativeLang := try.To1(native.DetectNativeLang())
38+
nativeLang := try.To1(sync.DetectNativeLang())
4039
return bisync(context.Background(), nativeLang, lekkoPath, repoPath)
4140
},
4241
}
@@ -47,7 +46,7 @@ This may affect ordering of functions/parameters and formatting.`,
4746
return cmd
4847
}
4948

50-
func bisync(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath string) error {
49+
func bisync(ctx context.Context, nativeLang sync.NativeLang, lekkoPath, repoPath string) error {
5150
if len(lekkoPath) == 0 {
5251
dot := try.To1(dotlekko.ReadDotLekko())
5352
lekkoPath = dot.LekkoPath
@@ -56,9 +55,9 @@ func bisync(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPa
5655
repoPath = try.To1(repo.PrepareGithubRepo())
5756
}
5857
switch nativeLang {
59-
case native.GO:
58+
case sync.GO:
6059
_ = try.To1(sync.BisyncGo(ctx, lekkoPath, lekkoPath, repoPath))
61-
case native.TS:
60+
case sync.TS:
6261
try.To(sync.BisyncTS(lekkoPath, repoPath))
6362
default:
6463
return errors.New("unsupported language")
@@ -72,7 +71,7 @@ func bisyncGoCmd() *cobra.Command {
7271
Use: "go",
7372
Short: "Lekko bisync for Go. Should be run from project root.",
7473
RunE: func(cmd *cobra.Command, args []string) error {
75-
return bisync(context.Background(), native.GO, lekkoPath, repoPath)
74+
return bisync(context.Background(), sync.GO, lekkoPath, repoPath)
7675
},
7776
}
7877
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
@@ -86,7 +85,7 @@ func bisyncTSCmd() *cobra.Command {
8685
Use: "ts",
8786
Short: "Lekko bisync for Typescript. Should be run from project root.",
8887
RunE: func(cmd *cobra.Command, args []string) error {
89-
return bisync(context.Background(), native.TS, lekkoPath, repoPath)
88+
return bisync(context.Background(), sync.TS, lekkoPath, repoPath)
9089
},
9190
}
9291
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: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ package main
1717
import (
1818
"context"
1919
"fmt"
20+
"log"
21+
"os"
2022
"path"
2123
"path/filepath"
24+
"regexp"
2225
"strings"
2326

2427
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
28+
"golang.org/x/mod/modfile"
2529

26-
"github.com/lainio/err2"
2730
"github.com/lainio/err2/try"
2831
"github.com/lekkodev/cli/pkg/dotlekko"
2932
"github.com/lekkodev/cli/pkg/feature"
3033
"github.com/lekkodev/cli/pkg/gen"
31-
"github.com/lekkodev/cli/pkg/native"
3234
"github.com/lekkodev/cli/pkg/repo"
3335
"github.com/lekkodev/cli/pkg/star"
3436
"github.com/lekkodev/cli/pkg/star/static"
@@ -39,51 +41,85 @@ import (
3941
)
4042

4143
func genCmd() *cobra.Command {
42-
var lekkoPath, repoPath, ns string
43-
var initMode bool
4444
cmd := &cobra.Command{
4545
Use: "gen",
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-
},
46+
Short: "generate library code from configs",
5147
}
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")
5648
cmd.AddCommand(genGoCmd())
5749
cmd.AddCommand(genTSCmd())
5850
cmd.AddCommand(genStarlarkCmd())
5951
return cmd
6052
}
6153

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-
return gen.GenNative(ctx, nativeLang, lekkoPath, repoPath, ns, ".", initMode)
72-
}
73-
7454
func genGoCmd() *cobra.Command {
75-
var lekkoPath, repoPath, ns string
55+
var ns string
56+
var outputPath string
57+
var repoPath string
7658
var initMode bool
7759
cmd := &cobra.Command{
7860
Use: "go",
7961
Short: "generate Go library code from configs",
8062
RunE: func(cmd *cobra.Command, args []string) error {
81-
return genNative(cmd.Context(), native.GO, lekkoPath, repoPath, ns, initMode)
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
82118
},
83119
}
84-
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
85-
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
86120
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")
87123
cmd.Flags().BoolVar(&initMode, "init", false, "pass 'init' to generate boilerplate code for a Lekko namespace")
88124
return cmd
89125
}
@@ -96,12 +132,19 @@ func genTSCmd() *cobra.Command {
96132
Use: "ts",
97133
Short: "generate typescript library code from configs",
98134
RunE: func(cmd *cobra.Command, args []string) error {
99-
return genNative(cmd.Context(), native.TS, lekkoPath, repoPath, ns, false)
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"))
100143
},
101144
}
145+
cmd.Flags().StringVarP(&ns, "namespace", "n", "default", "namespace to generate code from")
102146
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "path to Lekko native config files, will use autodetect if not set")
103147
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
104-
cmd.Flags().StringVarP(&ns, "namespace", "n", "default", "namespace to generate code from")
105148
return cmd
106149
}
107150

cmd/lekko/repo.go

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ 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"
3332
"github.com/lainio/err2/try"
3433
"github.com/lekkodev/cli/pkg/dotlekko"
35-
"github.com/lekkodev/cli/pkg/gen"
3634
"github.com/lekkodev/cli/pkg/gh"
3735
"github.com/lekkodev/cli/pkg/gitcli"
3836
"github.com/lekkodev/cli/pkg/lekko"
3937
"github.com/lekkodev/cli/pkg/logging"
40-
"github.com/lekkodev/cli/pkg/native"
4138
"github.com/lekkodev/cli/pkg/repo"
4239
"github.com/lekkodev/cli/pkg/secrets"
4340
"github.com/lekkodev/cli/pkg/sync"
@@ -430,20 +427,37 @@ func pullCmd() *cobra.Command {
430427
cmd := &cobra.Command{
431428
Use: "pull",
432429
Short: "Pull remote changes and merge them with local changes.",
433-
RunE: func(cmd *cobra.Command, args []string) (err error) {
434-
defer err2.Handle(&err)
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+
}
435435

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

439-
repoPath := try.To1(repo.PrepareGithubRepo())
440-
gitRepo := try.To1(git.PlainOpen(repoPath))
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+
}
441449
// this should be safe as we generate all changes from native lang
442450
// git reset --hard
443451
// git clean -fd
444-
try.To(repo.ResetAndClean(gitRepo))
452+
err = repo.ResetAndClean(gitRepo)
453+
if err != nil {
454+
return errors.Wrap(err, "reset and clean")
455+
}
445456

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

454468
// pull from remote
455-
remotes := try.To1(gitRepo.Remotes())
469+
remotes, err := gitRepo.Remotes()
470+
if err != nil {
471+
return errors.Wrap(err, "get remotes")
472+
}
456473
if len(remotes) == 0 {
457474
return errors.New("No remote found, please finish setup instructions")
458475
}
459476
fmt.Printf("Pulling from %s\n", remotes[0].Config().URLs[0])
460-
try.To(gitcli.Pull(repoPath))
461-
newHead := try.To1(gitRepo.Head())
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+
}
462485

463486
lekkoPath := dot.LekkoPath
464487
if len(dot.LockSHA) == 0 || force {
@@ -475,7 +498,17 @@ func pullCmd() *cobra.Command {
475498
return fmt.Errorf("please commit or stash changes in '%s' before pulling", lekkoPath)
476499
}
477500
}
478-
try.To(gen.GenNative(cmd.Context(), nativeLang, dot.LekkoPath, repoPath, "", ".", false))
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+
}
479512

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

494527
switch nativeLang {
495-
case native.TS:
528+
case sync.TS:
496529
tsPullCmd := exec.Command("npx", "lekko-repo-pull", "--lekko-dir", lekkoPath)
497530
output, err := tsPullCmd.CombinedOutput()
498531
fmt.Println(string(output))
499532
if err != nil {
500533
return errors.Wrap(err, "ts pull")
501534
}
502-
case native.GO:
535+
case sync.GO:
503536
files, err := sync.BisyncGo(cmd.Context(), lekkoPath, lekkoPath, repoPath)
504537
if err != nil {
505538
return errors.Wrap(err, "go bisync")
@@ -526,13 +559,12 @@ func pullCmd() *cobra.Command {
526559
return cmd
527560
}
528561

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

537569
fileBytes, err := os.ReadFile(filename)
538570
if err != nil {
@@ -576,7 +608,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) (er
576608
return errors.Wrap(err, "create temp dir")
577609
}
578610
defer os.RemoveAll(baseDir)
579-
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, ns, baseDir, false)
611+
err = sync.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, ns, baseDir)
580612
if err != nil {
581613
return errors.Wrap(err, "gen native")
582614
}
@@ -612,7 +644,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) (er
612644
return errors.Wrap(err, "create temp dir")
613645
}
614646
defer os.RemoveAll(remoteDir)
615-
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, ns, remoteDir, false)
647+
err = sync.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, ns, remoteDir)
616648
if err != nil {
617649
return errors.Wrap(err, "gen native")
618650
}

0 commit comments

Comments
 (0)