Skip to content

Commit

Permalink
make gen work in the backend (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
passichenko authored May 13, 2024
1 parent 706a279 commit cb3e2cf
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 60 deletions.
4 changes: 2 additions & 2 deletions cmd/lekko/bisync.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func bisyncCmd() *cobra.Command {
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.
This may affect ordering of functions/parameters and formatting.`,
RunE: func(cmd *cobra.Command, args []string) error {
nativeLang := try.To1(native.DetectNativeLang())
_, nativeLang := try.To2(native.DetectNativeLang(""))
return bisync(context.Background(), nativeLang, lekkoPath, repoPath)
},
}
Expand All @@ -49,7 +49,7 @@ This may affect ordering of functions/parameters and formatting.`,

func bisync(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath string) error {
if len(lekkoPath) == 0 {
dot := try.To1(dotlekko.ReadDotLekko())
dot := try.To1(dotlekko.ReadDotLekko(""))
lekkoPath = dot.LekkoPath
}
if len(repoPath) == 0 {
Expand Down
21 changes: 12 additions & 9 deletions cmd/lekko/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func genCmd() *cobra.Command {
Use: "gen",
Short: "generate Lekko config functions from a local config repository",
RunE: func(cmd *cobra.Command, args []string) error {
nativeLang := try.To1(native.DetectNativeLang())
return genNative(context.Background(), nativeLang, lekkoPath, repoPath, ns, initMode)
meta, nativeLang := try.To2(native.DetectNativeLang(""))
return genNative(context.Background(), meta, nativeLang, lekkoPath, repoPath, ns, initMode)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
Expand All @@ -59,20 +59,23 @@ func genCmd() *cobra.Command {
return cmd
}

func genNative(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath, ns string, initMode bool) (err error) {
func genNative(ctx context.Context, nativeMetadata native.Metadata, nativeLang native.NativeLang, lekkoPath, repoPath, ns string, initMode bool) (err error) {
defer err2.Handle(&err)
if len(lekkoPath) == 0 {
dot := try.To1(dotlekko.ReadDotLekko())
dot := try.To1(dotlekko.ReadDotLekko(""))
lekkoPath = dot.LekkoPath
}
if len(repoPath) == 0 {
repoPath = try.To1(repo.PrepareGithubRepo())
}
var namespaces []string
opts := gen.GenOptions{
InitMode: initMode,
NativeMetadata: nativeMetadata,
}
if len(ns) > 0 {
namespaces = append(namespaces, ns)
opts.Namespaces = []string{ns}
}
return gen.GenNative(ctx, nativeLang, lekkoPath, repoPath, namespaces, ".", initMode)
return gen.GenNative(ctx, nativeLang, lekkoPath, repoPath, opts)
}

func genGoCmd() *cobra.Command {
Expand All @@ -82,7 +85,7 @@ func genGoCmd() *cobra.Command {
Use: "go",
Short: "generate Go library code from configs",
RunE: func(cmd *cobra.Command, args []string) error {
return genNative(cmd.Context(), native.GO, lekkoPath, repoPath, ns, initMode)
return genNative(cmd.Context(), nil, native.GO, lekkoPath, repoPath, ns, initMode)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
Expand All @@ -100,7 +103,7 @@ func genTSCmd() *cobra.Command {
Use: "ts",
Short: "generate typescript library code from configs",
RunE: func(cmd *cobra.Command, args []string) error {
return genNative(cmd.Context(), native.TS, lekkoPath, repoPath, ns, false)
return genNative(cmd.Context(), nil, native.TS, lekkoPath, repoPath, ns, false)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "path to Lekko native config files, will use autodetect if not set")
Expand Down
35 changes: 20 additions & 15 deletions cmd/lekko/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func remoteCmd() *cobra.Command {
Use: "remote",
Short: "Show the remote Lekko repo currently in use",
RunE: func(cmd *cobra.Command, args []string) error {
dot, err := dotlekko.ReadDotLekko()
dot, err := dotlekko.ReadDotLekko("")
if err != nil {
return err
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func pushCmd() *cobra.Command {
Use: "push",
Short: "Push local changes to remote Lekko repo.",
RunE: func(cmd *cobra.Command, args []string) error {
dot, err := dotlekko.ReadDotLekko()
dot, err := dotlekko.ReadDotLekko("")
if err != nil {
return errors.Wrap(err, "read Lekko configuration file")
}
Expand All @@ -384,7 +384,7 @@ func pathCmd() *cobra.Command {
Use: "path",
Short: "Show the local repo path currently in use",
RunE: func(cmd *cobra.Command, args []string) error {
dot := try.To1(dotlekko.ReadDotLekko())
dot := try.To1(dotlekko.ReadDotLekko(""))
repoPath := try.To1(repo.GetRepoPath(dot))
fmt.Println(repoPath)
return nil
Expand Down Expand Up @@ -433,8 +433,8 @@ func pullCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) (err error) {
defer err2.Handle(&err)

dot := try.To1(dotlekko.ReadDotLekko())
nativeLang := try.To1(native.DetectNativeLang())
dot := try.To1(dotlekko.ReadDotLekko(""))
nativeMetadata, nativeLang := try.To2(native.DetectNativeLang(""))

repoPath := try.To1(repo.PrepareGithubRepo())
gitRepo := try.To1(git.PlainOpen(repoPath))
Expand Down Expand Up @@ -475,7 +475,7 @@ func pullCmd() *cobra.Command {
return fmt.Errorf("please commit or stash changes in '%s' before pulling", lekkoPath)
}
}
try.To(gen.GenNative(cmd.Context(), nativeLang, dot.LekkoPath, repoPath, nil, ".", false))
try.To(gen.GenNative(cmd.Context(), nativeLang, dot.LekkoPath, repoPath, gen.GenOptions{NativeMetadata: nativeMetadata}))

dot.LockSHA = newHead.Hash().String()
if err := dot.WriteBack(); err != nil {
Expand Down Expand Up @@ -505,10 +505,7 @@ func pullCmd() *cobra.Command {
return errors.Wrap(err, "go bisync")
}
for _, f := range files {
err = mergeFile(cmd.Context(), f, dot)
if err != nil {
return errors.Wrapf(err, "merge file %s", f)
}
try.To(mergeFile(cmd.Context(), f, dot, nativeMetadata))
}
default:
return fmt.Errorf("unsupported language: %s", nativeLang)
Expand All @@ -526,7 +523,7 @@ func pullCmd() *cobra.Command {
return cmd
}

func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) (err error) {
func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nativeMetadata native.Metadata) (err error) {
defer err2.Handle(&err)
nativeLang, err := native.NativeLangFromExt(filename)
if err != nil {
Expand Down Expand Up @@ -576,7 +573,11 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) (er
return errors.Wrap(err, "create temp dir")
}
defer os.RemoveAll(baseDir)
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, []string{ns}, baseDir, false)
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, gen.GenOptions{
CodeRepoPath: baseDir,
Namespaces: []string{ns},
NativeMetadata: nativeMetadata,
})
if err != nil {
return errors.Wrap(err, "gen native")
}
Expand Down Expand Up @@ -612,7 +613,11 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko) (er
return errors.Wrap(err, "create temp dir")
}
defer os.RemoveAll(remoteDir)
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, []string{ns}, remoteDir, false)
err = gen.GenNative(ctx, nativeLang, dot.LekkoPath, repoPath, gen.GenOptions{
CodeRepoPath: remoteDir,
Namespaces: []string{ns},
NativeMetadata: nativeMetadata,
})
if err != nil {
return errors.Wrap(err, "gen native")
}
Expand Down Expand Up @@ -655,11 +660,11 @@ func mergeFileCmd() *cobra.Command {
"Assumes that the repo is up-to-date. Typescript only."),
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
dot, err := dotlekko.ReadDotLekko()
dot, err := dotlekko.ReadDotLekko("")
if err != nil {
return errors.Wrap(err, "open Lekko configuration file")
}
return mergeFile(cmd.Context(), tsFilename, dot)
return mergeFile(cmd.Context(), tsFilename, dot, nil)
},
}
cmd.Flags().StringVarP(&tsFilename, "filename", "f", "", "path to ts file to pull changes into")
Expand Down
2 changes: 1 addition & 1 deletion cmd/lekko/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func setupCmd() *cobra.Command {
}
fmt.Println()

dot, err := dotlekko.ReadDotLekko()
dot, err := dotlekko.ReadDotLekko("")
if err != nil {
fmt.Println("Lekko is not detected in this project.")
doInit := false
Expand Down
6 changes: 3 additions & 3 deletions cmd/lekko/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ func isSame(ctx context.Context, existing map[string]map[string]*featurev1beta1.
if err != nil {
return false, err
}
dot := try.To1(dotlekko.ReadDotLekko())
nativeLang := try.To1(native.DetectNativeLang())
dot := try.To1(dotlekko.ReadDotLekko(""))
_, nativeLang := try.To2(native.DetectNativeLang(""))
files := try.To1(native.ListNativeConfigFiles(dot.LekkoPath, nativeLang))
var notEqual bool
for _, f := range files {
Expand Down Expand Up @@ -303,7 +303,7 @@ func isSameTS(ctx context.Context, existing map[string]map[string]*featurev1beta
if err != nil {
return false, err
}
dot := try.To1(dotlekko.ReadDotLekko())
dot := try.To1(dotlekko.ReadDotLekko(""))
cmd := exec.Command("npx", "ts-to-proto", "--lekko-dir", dot.LekkoPath) // #nosec G204
cmd.Dir = root
nsString, err := cmd.CombinedOutput()
Expand Down
8 changes: 4 additions & 4 deletions pkg/dotlekko/dotlekko.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func NewDotLekko(lekkoPath string) *DotLekko {

// Looks for .lekko or .lekko.(yaml|yml) in the working directory
// Returns the parsed configuration object and the path to the file.
func ReadDotLekko() (*DotLekko, error) {
func ReadDotLekko(codeRepoPath string) (*DotLekko, error) {
var bareMissing, yamlMissing, ymlMissing bool
var path string
barePath := ".lekko"
yamlPath := ".lekko.yaml"
ymlPath := ".lekko.yml"
barePath := filepath.Join(codeRepoPath, ".lekko")
yamlPath := filepath.Join(codeRepoPath, ".lekko.yaml")
ymlPath := filepath.Join(codeRepoPath, ".lekko.yml")

if _, err := os.Stat(barePath); err != nil {
bareMissing = true
Expand Down
54 changes: 39 additions & 15 deletions pkg/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,71 @@ import (

"github.com/lainio/err2"
"github.com/lainio/err2/try"
"github.com/lekkodev/cli/pkg/native"
"github.com/pkg/errors"

"github.com/lekkodev/cli/pkg/dotlekko"
"github.com/lekkodev/cli/pkg/native"
)

func GenNative(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath string, namespaces []string, nativeRepoPath string, initMode bool) (err error) {
type GenOptions struct {
CodeRepoPath string
Namespaces []string
InitMode bool
NativeMetadata native.Metadata
}

func GenAuto(ctx context.Context, configRepoPath, codeRepoPath string) (err error) {
defer err2.Handle(&err)
nativeMetadata, nativeLang := try.To2(native.DetectNativeLang(codeRepoPath))
dot := try.To1(dotlekko.ReadDotLekko(codeRepoPath))
return GenNative(ctx, nativeLang, dot.LekkoPath, configRepoPath, GenOptions{CodeRepoPath: codeRepoPath, NativeMetadata: nativeMetadata})
}

func GenNative(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath string, opts GenOptions) (err error) {
defer err2.Handle(&err)

absLekkoPath := filepath.Join(nativeRepoPath, lekkoPath)
absLekkoPath := filepath.Join(opts.CodeRepoPath, lekkoPath)
err = os.MkdirAll(absLekkoPath, 0770)
if err != nil {
return errors.Wrap(err, "create output dir")
}

if len(namespaces) == 0 {
namespaces = try.To1(native.ListNamespaces(absLekkoPath, nativeLang))
if len(opts.Namespaces) == 0 {
opts.Namespaces = try.To1(native.ListNamespaces(absLekkoPath, nativeLang))
}

switch nativeLang {
case native.TS:
if initMode {
if opts.InitMode {
return errors.New("init mode not supported for TS")
}
for _, ns := range namespaces {
for _, ns := range opts.Namespaces {
outFilename := filepath.Join(absLekkoPath, ns+nativeLang.Ext())
try.To(GenFormattedTS(ctx, repoPath, ns, outFilename))
try.To(genFormattedTS(ctx, repoPath, ns, outFilename))
}
return nil
case native.GO:
return genFormattedGo(ctx, namespaces, repoPath, absLekkoPath, lekkoPath, initMode)
return genFormattedGo(ctx, repoPath, lekkoPath, opts)
default:
return errors.New("Unsupported language")
}
}

func genFormattedGo(ctx context.Context, namespaces []string, repoPath, outputPath, lekkoPath string, initMode bool) (err error) {
func genFormattedGo(ctx context.Context, repoPath, lekkoPath string, opts GenOptions) (err error) {
defer err2.Handle(&err)
b := try.To1(os.ReadFile("go.mod"))
mf := try.To1(modfile.ParseLax("go.mod", b, nil))
for _, namespace := range namespaces {
generator := try.To1(NewGoGenerator(mf.Module.Mod.Path, outputPath, lekkoPath, repoPath, namespace))
if initMode {
var moduleRoot string
switch m := opts.NativeMetadata.(type) {
case native.GoMetadata:
moduleRoot = m.ModulePath
default:
b := try.To1(os.ReadFile(filepath.Join(opts.CodeRepoPath, "go.mod")))
mf := try.To1(modfile.ParseLax("go.mod", b, nil))
moduleRoot = mf.Module.Mod.Path
}
outputPath := filepath.Join(opts.CodeRepoPath, lekkoPath)
for _, namespace := range opts.Namespaces {
generator := try.To1(NewGoGenerator(moduleRoot, outputPath, lekkoPath, repoPath, namespace))
if opts.InitMode {
try.To(generator.Init(ctx))
} else {
try.To(generator.Gen(ctx))
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func getTSParameters(d protoreflect.MessageDescriptor) string {
}

// Pipe `GenTS` to prettier
func GenFormattedTS(ctx context.Context, repoPath, ns, outFilename string) error {
func genFormattedTS(ctx context.Context, repoPath, ns, outFilename string) error {
prettierCmd := exec.Command("npx", "prettier", "--parser", "typescript")
stdinPipe, err := prettierCmd.StdinPipe()
if err != nil {
Expand Down
26 changes: 20 additions & 6 deletions pkg/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,39 @@ import (
"regexp"
"strings"

"golang.org/x/mod/modfile"

"github.com/lainio/err2"
"github.com/lainio/err2/try"
)

type Metadata interface {
isMetadata()
}

type GoMetadata struct {
ModulePath string
}

func (GoMetadata) isMetadata() {}

type NativeLang string

var (
GO NativeLang = "go"
TS NativeLang = "ts"
)

func DetectNativeLang() (NativeLang, error) {
func DetectNativeLang(codeRepoPath string) (Metadata, NativeLang, error) {
// naive check for "known" project types
if _, err := os.Stat("go.mod"); err == nil {
return GO, nil
} else if _, err = os.Stat("package.json"); err == nil {
return TS, nil
if _, err := os.Stat(filepath.Join(codeRepoPath, "go.mod")); err == nil {
b := try.To1(os.ReadFile(filepath.Join(codeRepoPath, "go.mod")))
mf := try.To1(modfile.ParseLax("go.mod", b, nil))
return GoMetadata{ModulePath: mf.Module.Mod.Path}, GO, nil
} else if _, err = os.Stat(filepath.Join(codeRepoPath, "package.json")); err == nil {
return nil, TS, nil
}
return "", errors.New("unknown project type, Lekko currently supports Go and NPM projects")
return nil, "", errors.New("unknown project type, Lekko currently supports Go and NPM projects")
}

func NativeLangFromExt(filename string) (NativeLang, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/repo/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func GetRepoPath(dot *dotlekko.DotLekko) (string, error) {
}

func PrepareGithubRepo() (string, error) {
dot, err := dotlekko.ReadDotLekko()
dot, err := dotlekko.ReadDotLekko("")
if err != nil || len(dot.Repository) == 0 {
return InitIfNotExists("")
}
Expand Down
Loading

0 comments on commit cb3e2cf

Please sign in to comment.