-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modulegen: use cobra instead of native golang flag (#1550)
* modulegen: Use cobra instead of native golang flag Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> * chore: define flag names as constant --------- Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com> Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
- Loading branch information
1 parent
e25b7e0
commit f4433dd
Showing
28 changed files
with
388 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package modules | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/testcontainers/testcontainers-go/modulegen/internal" | ||
) | ||
|
||
var newExampleCmd = &cobra.Command{ | ||
Use: "example", | ||
Short: "Create a new Example", | ||
Long: "Create a new Example", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return internal.Generate(*exampleVar, false) | ||
}, | ||
} | ||
|
||
func init() { | ||
newExampleCmd.Flags().StringVarP(&exampleVar.Name, nameFlag, "n", "", "Name of the example. Only alphabetical characters are allowed.") | ||
newExampleCmd.Flags().StringVarP(&exampleVar.NameTitle, titleFlag, "t", "", "(Optional) Title of the example name, used to override the name in the case of mixed casing (Mongodb -> MongoDB). Use camel-case when needed. Only alphabetical characters are allowed.") | ||
newExampleCmd.Flags().StringVarP(&exampleVar.Image, imageFlag, "i", "", "Fully-qualified name of the Docker image to be used by the example") | ||
|
||
_ = newExampleCmd.MarkFlagRequired(imageFlag) | ||
_ = newExampleCmd.MarkFlagRequired(nameFlag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package modules | ||
|
||
const ( | ||
imageFlag = "image" | ||
nameFlag = "name" | ||
titleFlag = "title" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package modules | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/testcontainers/testcontainers-go/modulegen/internal/context" | ||
) | ||
|
||
var exampleVar = &context.ExampleVar{} | ||
|
||
var NewCmd = &cobra.Command{ | ||
Use: "new", | ||
Short: "Create a new Example or Module", | ||
Long: "Create a new Example or Module", | ||
} | ||
|
||
func init() { | ||
NewCmd.AddCommand(newExampleCmd) | ||
NewCmd.AddCommand(newModuleCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package modules | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/testcontainers/testcontainers-go/modulegen/internal" | ||
) | ||
|
||
var newModuleCmd = &cobra.Command{ | ||
Use: "module", | ||
Short: "Create a new Module", | ||
Long: "Create a new Module", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return internal.Generate(*exampleVar, true) | ||
}, | ||
} | ||
|
||
func init() { | ||
newModuleCmd.Flags().StringVarP(&exampleVar.Name, nameFlag, "n", "", "Name of the module. Only alphabetical characters are allowed.") | ||
newModuleCmd.Flags().StringVarP(&exampleVar.NameTitle, titleFlag, "t", "", "(Optional) Title of the module name, used to override the name in the case of mixed casing (Mongodb -> MongoDB). Use camel-case when needed. Only alphabetical characters are allowed.") | ||
newModuleCmd.Flags().StringVarP(&exampleVar.Image, imageFlag, "i", "", "Fully-qualified name of the Docker image to be used by the module") | ||
|
||
_ = newModuleCmd.MarkFlagRequired(imageFlag) | ||
_ = newModuleCmd.MarkFlagRequired(nameFlag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/testcontainers/testcontainers-go/modulegen/cmd/modules" | ||
) | ||
|
||
var NewRootCmd = &cobra.Command{ | ||
Use: "modulegen", | ||
Short: "Management tool for testcontainers-go", | ||
Long: "Management tool for testcontainers-go", | ||
} | ||
|
||
func init() { | ||
NewRootCmd.AddCommand(modules.NewCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package context | ||
|
||
type ExampleVar struct { | ||
Name string | ||
NameTitle string | ||
Image string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package context | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/testcontainers/testcontainers-go/modulegen/internal/context" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/dependabot" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/make" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/module" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/tools" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/vscode" | ||
"github.com/testcontainers/testcontainers-go/modulegen/internal/workflow" | ||
) | ||
|
||
func Generate(exampleVar context.ExampleVar, isModule bool) error { | ||
ctx, err := context.GetRootContext() | ||
if err != nil { | ||
return fmt.Errorf(">> could not get the root dir: %w", err) | ||
} | ||
|
||
example := context.Example{ | ||
Image: exampleVar.Image, | ||
IsModule: isModule, | ||
Name: exampleVar.Name, | ||
TitleName: exampleVar.NameTitle, | ||
} | ||
|
||
err = GenerateFiles(ctx, example) | ||
if err != nil { | ||
return fmt.Errorf(">> error generating the example: %w", err) | ||
} | ||
|
||
cmdDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower()) | ||
err = tools.GoModTidy(cmdDir) | ||
if err != nil { | ||
return fmt.Errorf(">> error synchronizing the dependencies: %w", err) | ||
} | ||
err = tools.GoVet(cmdDir) | ||
if err != nil { | ||
return fmt.Errorf(">> error checking generated code: %w", err) | ||
} | ||
|
||
fmt.Println("Please go to", cmdDir, "directory to check the results, where 'go mod tidy' and 'go vet' was executed to synchronize the dependencies") | ||
fmt.Println("Commit the modified files and submit a pull request to include them into the project") | ||
fmt.Println("Thanks!") | ||
return nil | ||
} | ||
|
||
func GenerateFiles(ctx *context.Context, example context.Example) error { | ||
if err := example.Validate(); err != nil { | ||
return err | ||
} | ||
// creates Makefile for example | ||
err := make.GenerateMakefile(ctx, example) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = module.GenerateGoModule(ctx, example) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// update github ci workflow | ||
err = workflow.GenerateWorkflow(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
// update examples in mkdocs | ||
err = mkdocs.GenerateMkdocs(ctx, example) | ||
if err != nil { | ||
return err | ||
} | ||
// update examples in dependabot | ||
err = dependabot.GenerateDependabotUpdates(ctx, example) | ||
if err != nil { | ||
return err | ||
} | ||
// generate vscode workspace | ||
err = vscode.GenerateVSCodeWorkspace(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.