Skip to content

Commit 20d47ee

Browse files
Generate for all namespaces that have directories in go (#377)
1 parent b3aab33 commit 20d47ee

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

cmd/lekko/gen.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"io"
21+
"log"
2122
"os"
2223
"path"
2324
"path/filepath"
@@ -27,7 +28,6 @@ import (
2728
featurev1beta1 "buf.build/gen/go/lekkodev/cli/protocolbuffers/go/lekko/feature/v1beta1"
2829
"golang.org/x/mod/modfile"
2930

30-
"github.com/AlecAivazis/survey/v2"
3131
"github.com/lekkodev/cli/pkg/dotlekko"
3232
"github.com/lekkodev/cli/pkg/feature"
3333
"github.com/lekkodev/cli/pkg/gen"
@@ -81,26 +81,40 @@ func genGoCmd() *cobra.Command {
8181
return err
8282
}
8383
}
84+
var namespaces []string
8485
if len(ns) == 0 {
85-
if err := survey.AskOne(&survey.Input{
86-
Message: "Namespace:",
87-
Help: "Lekko namespace to generate code for, determines Go package name",
88-
}, &ns); err != nil {
89-
return errors.Wrap(err, "namespace prompt")
86+
files, err := os.ReadDir(outputPath)
87+
if err != nil {
88+
log.Fatal(err)
9089
}
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}
9197
}
92-
// TODO: Change this to a survey validator so it can keep re-asking
93-
if !regexp.MustCompile("[a-z]+").MatchString(ns) {
94-
return errors.New("namespace must be a lowercase alphanumeric string")
95-
}
96-
generator, err := gen.NewGoGenerator(mf.Module.Mod.Path, outputPath, outputPath, repoPath, ns)
97-
if err != nil {
98-
return errors.Wrap(err, "initialize code generator")
99-
}
100-
if initMode {
101-
return generator.Init(cmd.Context())
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+
}
102116
}
103-
return generator.Gen(cmd.Context())
117+
return nil
104118
},
105119
}
106120
cmd.Flags().StringVarP(&ns, "namespace", "n", "", "namespace to generate code from")

0 commit comments

Comments
 (0)