@@ -17,18 +17,20 @@ package main
17
17
import (
18
18
"context"
19
19
"fmt"
20
+ "log"
21
+ "os"
20
22
"path"
21
23
"path/filepath"
24
+ "regexp"
22
25
"strings"
23
26
24
27
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
28
+ "golang.org/x/mod/modfile"
25
29
26
- "github.com/lainio/err2"
27
30
"github.com/lainio/err2/try"
28
31
"github.com/lekkodev/cli/pkg/dotlekko"
29
32
"github.com/lekkodev/cli/pkg/feature"
30
33
"github.com/lekkodev/cli/pkg/gen"
31
- "github.com/lekkodev/cli/pkg/native"
32
34
"github.com/lekkodev/cli/pkg/repo"
33
35
"github.com/lekkodev/cli/pkg/star"
34
36
"github.com/lekkodev/cli/pkg/star/static"
@@ -39,51 +41,85 @@ import (
39
41
)
40
42
41
43
func genCmd () * cobra.Command {
42
- var lekkoPath , repoPath , ns string
43
- var initMode bool
44
44
cmd := & cobra.Command {
45
45
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" ,
51
47
}
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" )
56
48
cmd .AddCommand (genGoCmd ())
57
49
cmd .AddCommand (genTSCmd ())
58
50
cmd .AddCommand (genStarlarkCmd ())
59
51
return cmd
60
52
}
61
53
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
-
74
54
func genGoCmd () * cobra.Command {
75
- var lekkoPath , repoPath , ns string
55
+ var ns string
56
+ var outputPath string
57
+ var repoPath string
76
58
var initMode bool
77
59
cmd := & cobra.Command {
78
60
Use : "go" ,
79
61
Short : "generate Go library code from configs" ,
80
62
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
82
118
},
83
119
}
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" )
86
120
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" )
87
123
cmd .Flags ().BoolVar (& initMode , "init" , false , "pass 'init' to generate boilerplate code for a Lekko namespace" )
88
124
return cmd
89
125
}
@@ -96,12 +132,19 @@ func genTSCmd() *cobra.Command {
96
132
Use : "ts" ,
97
133
Short : "generate typescript library code from configs" ,
98
134
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" ))
100
143
},
101
144
}
145
+ cmd .Flags ().StringVarP (& ns , "namespace" , "n" , "default" , "namespace to generate code from" )
102
146
cmd .Flags ().StringVarP (& lekkoPath , "lekko-path" , "p" , "" , "path to Lekko native config files, will use autodetect if not set" )
103
147
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" )
105
148
return cmd
106
149
}
107
150
0 commit comments