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