Skip to content

Commit 299a574

Browse files
committed
Refactor Go gen lib function signatures
1 parent 0a36771 commit 299a574

File tree

10 files changed

+262
-210
lines changed

10 files changed

+262
-210
lines changed

cmd/lekko/bisync.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
func bisyncCmd() *cobra.Command {
30-
var lekkoPath, repoPath string
30+
var lekkoPath, repoOwner, repoName, repoPath string
3131
cmd := &cobra.Command{
3232
Use: "bisync",
3333
Short: "bi-directionally sync Lekko config code from a project to a local config repository",
@@ -37,27 +37,33 @@ Files at the provided path that contain valid Lekko config functions will first
3737
This may affect ordering of functions/parameters and formatting.`,
3838
RunE: func(cmd *cobra.Command, args []string) error {
3939
nlProject := try.To1(native.DetectNativeLang(""))
40-
return bisync(context.Background(), nlProject, lekkoPath, repoPath)
40+
return bisync(context.Background(), nlProject, lekkoPath, repoOwner, repoName, repoPath)
4141
},
4242
}
4343
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
44+
cmd.Flags().StringVar(&repoOwner, "repo-owner", "", "GitHub owner of config repository, will use autodetect if not set")
45+
cmd.Flags().StringVar(&repoName, "repo-name", "", "GitHub name of config repository, will use autodetect if not set")
4446
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
4547
cmd.AddCommand(bisyncGoCmd())
4648
cmd.AddCommand(bisyncTSCmd())
4749
return cmd
4850
}
4951

50-
func bisync(ctx context.Context, project *native.Project, lekkoPath, repoPath string) error {
52+
func bisync(ctx context.Context, project *native.Project, lekkoPath, repoOwner, repoName, repoPath string) error {
5153
if len(lekkoPath) == 0 {
5254
dot := try.To1(dotlekko.ReadDotLekko(""))
5355
lekkoPath = dot.LekkoPath
5456
}
57+
if len(repoOwner) == 0 || len(repoName) == 0 {
58+
dot := try.To1(dotlekko.ReadDotLekko(""))
59+
repoOwner, repoName = dot.GetRepoInfo()
60+
}
5561
if len(repoPath) == 0 {
5662
repoPath = try.To1(repo.PrepareGithubRepo())
5763
}
5864
switch project.Language {
5965
case native.LangGo:
60-
_ = try.To1(sync.BisyncGo(ctx, lekkoPath, lekkoPath, repoPath))
66+
_ = try.To1(sync.BisyncGo(ctx, lekkoPath, lekkoPath, repoOwner, repoName, repoPath))
6167
case native.LangTypeScript:
6268
_ = try.To1(sync.BisyncTS(ctx, lekkoPath, repoPath))
6369
default:
@@ -67,7 +73,7 @@ func bisync(ctx context.Context, project *native.Project, lekkoPath, repoPath st
6773
}
6874

6975
func bisyncGoCmd() *cobra.Command {
70-
var lekkoPath, repoPath string
76+
var lekkoPath, repoOwner, repoName, repoPath string
7177
cmd := &cobra.Command{
7278
Use: "go",
7379
Short: "Lekko bisync for Go. Should be run from project root.",
@@ -76,16 +82,18 @@ func bisyncGoCmd() *cobra.Command {
7682
if nlProject.Language != native.LangGo {
7783
return errors.Errorf("not a Go project, detected %v instead", nlProject.Language)
7884
}
79-
return bisync(context.Background(), nlProject, lekkoPath, repoPath)
85+
return bisync(context.Background(), nlProject, lekkoPath, repoOwner, repoName, repoPath)
8086
},
8187
}
8288
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
89+
cmd.Flags().StringVar(&repoOwner, "repo-owner", "", "GitHub owner of config repository, will use autodetect if not set")
90+
cmd.Flags().StringVar(&repoName, "repo-name", "", "GitHub name of config repository, will use autodetect if not set")
8391
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
8492
return cmd
8593
}
8694

8795
func bisyncTSCmd() *cobra.Command {
88-
var lekkoPath, repoPath string
96+
var lekkoPath, repoOwner, repoName, repoPath string
8997
cmd := &cobra.Command{
9098
Use: "ts",
9199
Short: "Lekko bisync for Typescript. Should be run from project root.",
@@ -94,10 +102,12 @@ func bisyncTSCmd() *cobra.Command {
94102
if nlProject.Language != native.LangTypeScript {
95103
return errors.Errorf("not a TypeScript project, detected %v instead", nlProject.Language)
96104
}
97-
return bisync(context.Background(), nlProject, lekkoPath, repoPath)
105+
return bisync(context.Background(), nlProject, lekkoPath, repoOwner, repoName, repoPath)
98106
},
99107
}
100108
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
109+
cmd.Flags().StringVar(&repoOwner, "repo-owner", "", "GitHub owner of config repository, will use autodetect if not set")
110+
cmd.Flags().StringVar(&repoName, "repo-name", "", "GitHub name of config repository, will use autodetect if not set")
101111
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
102112
return cmd
103113
}

cmd/lekko/gen.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,50 @@ import (
3939
)
4040

4141
func genCmd() *cobra.Command {
42-
var lekkoPath, repoPath, ns string
43-
var initMode bool
42+
var lekkoPath, repoOwner, repoName, repoPath, ns string
4443
cmd := &cobra.Command{
4544
Use: "gen",
4645
Short: "generate Lekko config functions from a local config repository",
4746
RunE: func(cmd *cobra.Command, args []string) error {
4847
nlProject := try.To1(native.DetectNativeLang(""))
49-
return genNative(context.Background(), nlProject, lekkoPath, repoPath, ns, initMode)
48+
return genNative(context.Background(), nlProject, lekkoPath, repoOwner, repoName, repoPath, ns)
5049
},
5150
}
52-
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
51+
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "relative path to Lekko native config files, will use autodetect if not set")
52+
cmd.Flags().StringVar(&repoOwner, "repo-owner", "", "GitHub owner of config repository, will use autodetect if not set")
53+
cmd.Flags().StringVar(&repoName, "repo-name", "", "GitHub name of config repository, will use autodetect if not set")
5354
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
5455
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")
5656
cmd.AddCommand(genGoCmd())
5757
cmd.AddCommand(genTSCmd())
5858
cmd.AddCommand(genStarlarkCmd())
5959
return cmd
6060
}
6161

62-
func genNative(ctx context.Context, project *native.Project, lekkoPath, repoPath, ns string, initMode bool) (err error) {
62+
// TODO: Clean up/consolidate different native gen functions and how dotlekko gets used in them
63+
func genNative(ctx context.Context, project *native.Project, lekkoPath, repoOwner, repoName, repoPath, ns string) (err error) {
6364
defer err2.Handle(&err)
6465
if len(lekkoPath) == 0 {
6566
dot := try.To1(dotlekko.ReadDotLekko(""))
6667
lekkoPath = dot.LekkoPath
6768
}
69+
if len(repoOwner) == 0 || len(repoName) == 0 {
70+
dot := try.To1(dotlekko.ReadDotLekko(""))
71+
repoOwner, repoName = dot.GetRepoInfo()
72+
}
6873
if len(repoPath) == 0 {
6974
repoPath = try.To1(repo.PrepareGithubRepo())
7075
}
71-
opts := gen.GenOptions{
72-
InitMode: initMode,
73-
}
76+
opts := gen.GenOptions{}
7477
if len(ns) > 0 {
7578
opts.Namespaces = []string{ns}
7679
}
77-
return gen.GenNative(ctx, project, lekkoPath, repoPath, opts)
80+
return gen.GenNative(ctx, project, lekkoPath, repoOwner, repoName, repoPath, opts)
7881
}
7982

8083
// TODO: Add option to read encoded repo contents like gen ts
8184
func genGoCmd() *cobra.Command {
82-
var lekkoPath, repoPath, ns string
83-
var initMode bool
85+
var lekkoPath, repoOwner, repoName, repoPath, ns string
8486
cmd := &cobra.Command{
8587
Use: "go",
8688
Short: "generate Go library code from lekkos",
@@ -89,18 +91,19 @@ func genGoCmd() *cobra.Command {
8991
if nlProject.Language != native.LangGo {
9092
return errors.Errorf("not a Go project, detected %v instead", nlProject.Language)
9193
}
92-
return genNative(cmd.Context(), nlProject, lekkoPath, repoPath, ns, initMode)
94+
return genNative(cmd.Context(), nlProject, lekkoPath, repoOwner, repoName, repoPath, ns)
9395
},
9496
}
95-
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
97+
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "relative path to Lekko native config files, will use autodetect if not set")
98+
cmd.Flags().StringVar(&repoOwner, "repo-owner", "", "GitHub owner of config repository, will use autodetect if not set")
99+
cmd.Flags().StringVar(&repoName, "repo-name", "", "GitHub name of config repository, will use autodetect if not set")
96100
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
97101
cmd.Flags().StringVarP(&ns, "namespace", "n", "", "namespace to generate code from")
98-
cmd.Flags().BoolVar(&initMode, "init", false, "pass 'init' to generate boilerplate code for a Lekko namespace")
99102
return cmd
100103
}
101104

102105
func genTSCmd() *cobra.Command {
103-
var lekkoPath, repoPath, encodedRepoContents, ns string
106+
var lekkoPath, repoOwner, repoName, repoPath, encodedRepoContents, ns string
104107
cmd := &cobra.Command{
105108
Use: "ts",
106109
Short: "generate TypeScript library code from lekkos",
@@ -129,10 +132,12 @@ func genTSCmd() *cobra.Command {
129132
if nlProject.Language != native.LangTypeScript {
130133
return errors.Errorf("not a TypeScript project, detected %v instead", nlProject.Language)
131134
}
132-
return genNative(ctx, nlProject, lekkoPath, repoPath, ns, false)
135+
return genNative(ctx, nlProject, lekkoPath, repoOwner, repoName, repoPath, ns)
133136
},
134137
}
135-
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "path to Lekko native config files, will use autodetect if not set")
138+
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "relative path to Lekko native config files, will use autodetect if not set")
139+
cmd.Flags().StringVar(&repoOwner, "repo-owner", "", "GitHub owner of config repository, will use autodetect if not set")
140+
cmd.Flags().StringVar(&repoName, "repo-name", "", "GitHub name of config repository, will use autodetect if not set")
136141
cmd.Flags().StringVarP(&repoPath, "repo-path", "r", "", "path to config repository, will use autodetect if not set")
137142
cmd.Flags().StringVarP(&encodedRepoContents, "repo-contents", "R", "", "base64-encoded serialized repository contents, will use repo-path if not set")
138143
cmd.Flags().StringVarP(&ns, "namespace", "n", "default", "namespace to generate code from")

cmd/lekko/init.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
)
4141

4242
func initCmd() *cobra.Command {
43-
var lekkoPath, repoName string
43+
var lekkoPath, fullRepoName string
4444
cmd := &cobra.Command{
4545
Use: "init",
4646
Short: "initialize Lekko in your project",
@@ -129,7 +129,7 @@ func initCmd() *cobra.Command {
129129
return errors.Wrapf(err, "use Lekko team %s", teamName)
130130
}
131131
}
132-
if repoName == "" {
132+
if fullRepoName == "" {
133133
repoCmd := repo.NewRepoCmd(bff, ws)
134134
repos, err := repoCmd.List(ctx)
135135
if err != nil {
@@ -138,7 +138,7 @@ func initCmd() *cobra.Command {
138138
if len(repos) == 0 {
139139
fmt.Printf("No Lekko repositories found for team %s. Please finish onboarding at https://app.lekko.com to create a Lekko repository.\n", logging.Bold(teamName))
140140
}
141-
repoName = fmt.Sprintf("%s/%s", repos[0].Owner, repos[0].RepoName)
141+
fullRepoName = fmt.Sprintf("%s/%s", repos[0].Owner, repos[0].RepoName)
142142
if len(repos) > 1 {
143143
repoNames := make([]string, len(repos))
144144
for i, repo := range repos {
@@ -148,7 +148,7 @@ func initCmd() *cobra.Command {
148148
Message: "Choose Lekko repository:",
149149
Help: fmt.Sprintf("These are machine-managed repositories owned by your Lekko team %s.", teamName),
150150
Options: repoNames,
151-
}, &repoName); err != nil {
151+
}, &fullRepoName); err != nil {
152152
return errors.Wrap(err, "choose Lekko repository")
153153
}
154154
}
@@ -186,7 +186,7 @@ func initCmd() *cobra.Command {
186186
})))
187187
}
188188

189-
dot := dotlekko.NewDotLekko(lekkoPath, repoName)
189+
dot := dotlekko.NewDotLekko(lekkoPath, fullRepoName)
190190
try.To(dot.WriteBack())
191191
fmt.Printf("%s Successfully added %s.\n", successCheck, dot.GetPath())
192192

@@ -375,7 +375,8 @@ func initCmd() *cobra.Command {
375375
// Codegen
376376
spin.Suffix = " Running codegen..."
377377
spin.Start()
378-
if err := gen.GenNative(ctx, nlProject, lekkoPath, repoPath, gen.GenOptions{}); err != nil {
378+
repoOwner, repoName := dot.GetRepoInfo()
379+
if err := gen.GenNative(ctx, nlProject, lekkoPath, repoOwner, repoName, repoPath, gen.GenOptions{}); err != nil {
379380
return errors.Wrap(err, "codegen for default namespace")
380381
}
381382
spin.Stop()
@@ -435,7 +436,7 @@ func initCmd() *cobra.Command {
435436
},
436437
}
437438
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Location for Lekko files (relative to project root)")
438-
cmd.Flags().StringVarP(&repoName, "repo-name", "r", "", "Config repository name, for example `my-org/lekko-configs`")
439+
cmd.Flags().StringVarP(&fullRepoName, "repo-name", "r", "", "Config repository name, for example `my-org/lekko-configs`")
439440
return cmd
440441
}
441442

cmd/lekko/repo.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,8 @@ func pullCmd() *cobra.Command {
479479
try.To1(gitcli.Pull(repoPath))
480480
newHead := try.To1(gitRepo.Head())
481481

482-
if err := gen.GenNative(ctx, nlProject, dot.LekkoPath, repoPath, gen.GenOptions{}); err != nil {
482+
repoOwner, repoName := dot.GetRepoInfo()
483+
if err := gen.GenNative(ctx, nlProject, dot.LekkoPath, repoOwner, repoName, repoPath, gen.GenOptions{}); err != nil {
483484
return errors.Wrap(err, "gen")
484485
}
485486
fmt.Printf("Generated lekkos from %s\n", newHead.Hash().String())
@@ -528,6 +529,8 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nlP
528529
return false, errors.Wrap(err, "get worktree")
529530
}
530531

532+
repoOwner, repoName := dot.GetRepoInfo()
533+
531534
// base
532535
err = worktree.Checkout(&git.CheckoutOptions{
533536
Hash: plumbing.NewHash(dot.LockSHA),
@@ -541,7 +544,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nlP
541544
return false, errors.Wrap(err, "create temp dir")
542545
}
543546
defer os.RemoveAll(baseDir)
544-
err = gen.GenNative(ctx, nlProject, dot.LekkoPath, repoPath, gen.GenOptions{
547+
err = gen.GenNative(ctx, nlProject, dot.LekkoPath, repoOwner, repoName, repoPath, gen.GenOptions{
545548
CodeRepoPath: baseDir,
546549
Namespaces: []string{ns},
547550
})
@@ -580,7 +583,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nlP
580583
return false, errors.Wrap(err, "create temp dir")
581584
}
582585
defer os.RemoveAll(remoteDir)
583-
err = gen.GenNative(ctx, nlProject, dot.LekkoPath, repoPath, gen.GenOptions{
586+
err = gen.GenNative(ctx, nlProject, dot.LekkoPath, repoOwner, repoName, repoPath, gen.GenOptions{
584587
CodeRepoPath: remoteDir,
585588
Namespaces: []string{ns},
586589
})

cmd/lekko/sync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,15 @@ func goToGo(ctx context.Context, filePath string) string {
532532
//fmt.Printf("%+v\n", registry.Types)
533533
//fmt.Print("ON TO GENERATION\n")
534534
// code gen based off that namespace object
535-
g, err := gen.NewGoGenerator("", "/tmp", "", repoContents)
535+
g, err := gen.NewGoGenerator("", "", "", "", repoContents)
536536
if err != nil {
537537
panic(err)
538538
}
539-
_, privateFile, err := g.GenNamespaceFiles(ctx, namespace.Name, namespace.Features, nil)
539+
gn, err := g.GenNamespaceFiles(ctx, namespace.Name, nil)
540540
if err != nil {
541541
panic(err)
542542
}
543-
return privateFile
543+
return gn.Private
544544
}
545545

546546
func ProtoJSONToTS(nsString []byte, fdString []byte) (string, error) {

cmd/lekko/sync_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,22 @@ func TestGoSyncToGenToSync(t *testing.T) {
166166
}
167167
defer os.RemoveAll(tmpd)
168168

169-
g, err := gen.NewGoGenerator("test", tmpd, "", r1)
169+
g, err := gen.NewGoGenerator("test", "", "test-owner", "test-repo", r1)
170170
if err != nil {
171171
t.Fatalf("initialize gen: %v", err)
172172
}
173173
namespace := r1.Namespaces[0]
174-
_, private, err := g.GenNamespaceFiles(ctx, namespace.Name, namespace.Features, nil)
174+
gn, err := g.GenNamespaceFiles(ctx, namespace.Name, nil)
175175
if err != nil {
176176
t.Fatalf("gen: %v", err)
177177
}
178178
privatePath := filepath.Join(tmpd, fmt.Sprintf("%s.go", namespace.Name))
179-
if err := os.WriteFile(privatePath, []byte(private), 0600); err != nil {
179+
if err := os.WriteFile(privatePath, []byte(gn.Private), 0600); err != nil {
180180
t.Fatalf("write private %s: %v", privatePath, err)
181181
}
182182

183-
if string(orig) != private {
184-
diff, err := DiffStyleOutput(string(orig), private)
183+
if string(orig) != gn.Private {
184+
diff, err := DiffStyleOutput(string(orig), gn.Private)
185185
if err != nil {
186186
t.Fatalf("diff: %v", err)
187187
}

pkg/gen/gen.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ func GenAuto(ctx context.Context, configRepoPath, codeRepoPath string) (err erro
4343
defer err2.Handle(&err)
4444
project := try.To1(native.DetectNativeLang(codeRepoPath))
4545
dot := try.To1(dotlekko.ReadDotLekko(codeRepoPath))
46-
return GenNative(ctx, project, dot.LekkoPath, configRepoPath, GenOptions{CodeRepoPath: codeRepoPath})
46+
repoOwner, repoName := dot.GetRepoInfo()
47+
return GenNative(ctx, project, dot.LekkoPath, repoOwner, repoName, configRepoPath, GenOptions{CodeRepoPath: codeRepoPath})
4748
}
4849

49-
func GenNative(ctx context.Context, project *native.Project, lekkoPath, repoPath string, opts GenOptions) (err error) {
50+
func GenNative(ctx context.Context, project *native.Project, lekkoPath, repoOwner, repoName, repoPath string, opts GenOptions) (err error) {
5051
defer err2.Handle(&err)
5152

5253
absLekkoPath := filepath.Join(opts.CodeRepoPath, lekkoPath)
@@ -85,14 +86,14 @@ func GenNative(ctx context.Context, project *native.Project, lekkoPath, repoPath
8586
}
8687
return nil
8788
case native.LangGo:
88-
return genFormattedGo(ctx, project, repoPath, lekkoPath, opts)
89+
return genFormattedGo(ctx, project, repoPath, lekkoPath, repoOwner, repoName, opts)
8990
default:
9091
return errors.New("Unsupported language")
9192
}
9293
}
9394

9495
// TODO: move to golang and split repo/repoless
95-
func genFormattedGo(ctx context.Context, project *native.Project, repoPath, lekkoPath string, opts GenOptions) (err error) {
96+
func genFormattedGo(ctx context.Context, project *native.Project, repoPath, lekkoPath, repoOwner, repoName string, opts GenOptions) (err error) {
9697
defer err2.Handle(&err)
9798
var moduleRoot string
9899
switch m := project.Metadata.(type) {
@@ -104,14 +105,9 @@ func genFormattedGo(ctx context.Context, project *native.Project, repoPath, lekk
104105
moduleRoot = mf.Module.Mod.Path
105106
}
106107
outputPath := filepath.Join(opts.CodeRepoPath, lekkoPath)
107-
generator := try.To1(NewGoGeneratorFromLocal(ctx, moduleRoot, outputPath, lekkoPath, repoPath))
108-
for _, namespace := range opts.Namespaces {
109-
if opts.InitMode {
110-
try.To(generator.Init(ctx, namespace))
111-
} else {
112-
try.To(generator.Gen(ctx, namespace))
113-
}
114-
}
108+
generator := try.To1(NewGoGeneratorFromLocal(ctx, moduleRoot, lekkoPath, repoOwner, repoName, repoPath))
109+
generated := try.To1(generator.Gen(ctx, opts.Namespaces...))
110+
try.To(generated.WriteFiles(outputPath))
115111
return nil
116112
}
117113

0 commit comments

Comments
 (0)