Skip to content

Commit

Permalink
lekko init improvements (#417)
Browse files Browse the repository at this point in the history
* Consolidate native lang project detection

* Output detected project information

* Update success messages to add doc links
  • Loading branch information
DavidSGK authored Jun 20, 2024
1 parent 9cbcf5c commit c372f31
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 253 deletions.
24 changes: 16 additions & 8 deletions cmd/lekko/bisync.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ 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.To2(native.DetectNativeLang(""))
return bisync(context.Background(), nativeLang, lekkoPath, repoPath)
nlProject := try.To1(native.DetectNativeLang(""))
return bisync(context.Background(), nlProject, lekkoPath, repoPath)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
Expand All @@ -47,18 +47,18 @@ This may affect ordering of functions/parameters and formatting.`,
return cmd
}

func bisync(ctx context.Context, nativeLang native.NativeLang, lekkoPath, repoPath string) error {
func bisync(ctx context.Context, project *native.Project, lekkoPath, repoPath string) error {
if len(lekkoPath) == 0 {
dot := try.To1(dotlekko.ReadDotLekko(""))
lekkoPath = dot.LekkoPath
}
if len(repoPath) == 0 {
repoPath = try.To1(repo.PrepareGithubRepo())
}
switch nativeLang {
case native.GO:
switch project.Language {
case native.LangGo:
_ = try.To1(sync.BisyncGo(ctx, lekkoPath, lekkoPath, repoPath))
case native.TS:
case native.LangTypeScript:
try.To(sync.BisyncTS(lekkoPath, repoPath))
default:
return errors.New("unsupported language")
Expand All @@ -72,7 +72,11 @@ func bisyncGoCmd() *cobra.Command {
Use: "go",
Short: "Lekko bisync for Go. Should be run from project root.",
RunE: func(cmd *cobra.Command, args []string) error {
return bisync(context.Background(), native.GO, lekkoPath, repoPath)
nlProject := try.To1(native.DetectNativeLang(""))
if nlProject.Language != native.LangGo {
return errors.Errorf("not a Go project, detected %v instead", nlProject.Language)
}
return bisync(context.Background(), nlProject, lekkoPath, repoPath)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
Expand All @@ -86,7 +90,11 @@ func bisyncTSCmd() *cobra.Command {
Use: "ts",
Short: "Lekko bisync for Typescript. Should be run from project root.",
RunE: func(cmd *cobra.Command, args []string) error {
return bisync(context.Background(), native.TS, lekkoPath, repoPath)
nlProject := try.To1(native.DetectNativeLang(""))
if nlProject.Language != native.LangTypeScript {
return errors.Errorf("not a TypeScript project, detected %v instead", nlProject.Language)
}
return bisync(context.Background(), nlProject, lekkoPath, repoPath)
},
}
cmd.Flags().StringVarP(&lekkoPath, "lekko-path", "p", "", "Path to Lekko native config files, will use autodetect if not set")
Expand Down
23 changes: 15 additions & 8 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 {
meta, nativeLang := try.To2(native.DetectNativeLang(""))
return genNative(context.Background(), meta, nativeLang, lekkoPath, repoPath, ns, initMode)
nlProject := try.To1(native.DetectNativeLang(""))
return genNative(context.Background(), nlProject, 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,7 +59,7 @@ func genCmd() *cobra.Command {
return cmd
}

func genNative(ctx context.Context, nativeMetadata native.Metadata, nativeLang native.NativeLang, lekkoPath, repoPath, ns string, initMode bool) (err error) {
func genNative(ctx context.Context, project *native.Project, lekkoPath, repoPath, ns string, initMode bool) (err error) {
defer err2.Handle(&err)
if len(lekkoPath) == 0 {
dot := try.To1(dotlekko.ReadDotLekko(""))
Expand All @@ -69,13 +69,12 @@ func genNative(ctx context.Context, nativeMetadata native.Metadata, nativeLang n
repoPath = try.To1(repo.PrepareGithubRepo())
}
opts := gen.GenOptions{
InitMode: initMode,
NativeMetadata: nativeMetadata,
InitMode: initMode,
}
if len(ns) > 0 {
opts.Namespaces = []string{ns}
}
return gen.GenNative(ctx, nativeLang, lekkoPath, repoPath, opts)
return gen.GenNative(ctx, project, lekkoPath, repoPath, opts)
}

func genGoCmd() *cobra.Command {
Expand All @@ -85,7 +84,11 @@ 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(), nil, native.GO, lekkoPath, repoPath, ns, initMode)
nlProject := try.To1(native.DetectNativeLang(""))
if nlProject.Language != native.LangGo {
return errors.Errorf("not a Go project, detected %v instead", nlProject.Language)
}
return genNative(cmd.Context(), nlProject, 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 @@ -103,7 +106,11 @@ 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(), nil, native.TS, lekkoPath, repoPath, ns, false)
nlProject := try.To1(native.DetectNativeLang(""))
if nlProject.Language != native.LangTypeScript {
return errors.Errorf("not a Go project, detected %v instead", nlProject.Language)
}
return genNative(cmd.Context(), nlProject, 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
Loading

0 comments on commit c372f31

Please sign in to comment.