Skip to content

Commit

Permalink
feat: add remote repo for render and middlewares
Browse files Browse the repository at this point in the history
Signed-off-by: shawn <[email protected]>

chore(generic): add generic base using gopkg base (#1482)

fix(gonet): adjust gonet server read timeout to avoid read error (#1481)

fix: remove redundant line

Signed-off-by: shawn <[email protected]>

fix: fix single command

Signed-off-by: shawn <[email protected]>

feat: add userDefinedMiddleware

Signed-off-by: shawn <[email protected]>
  • Loading branch information
ShawnJeffersonWang committed Aug 7, 2024
1 parent e335067 commit 4e4e8de
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 163 deletions.
7 changes: 2 additions & 5 deletions tool/cmd/kitex/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,11 @@ func (a *Arguments) checkIDL(files []string) error {
}

func (a *Arguments) checkServiceName() error {
if a.ServiceName == "" && a.TemplateDir == "" && a.RenderTplDir == "" {
if a.ServiceName == "" && a.TemplateDir == "" {
if a.Use != "" {
return fmt.Errorf("-use must be used with -service or -template-dir or template render")
return fmt.Errorf("-use must be used with -service or -template-dir")
}
}
if a.TemplateDir != "" && a.RenderTplDir != "" {
return fmt.Errorf("template render and -template-dir cannot be used at the same time")
}
if a.ServiceName != "" && a.TemplateDir != "" {
return fmt.Errorf("-template-dir and -service cannot be specified at the same time")
}
Expand Down
151 changes: 136 additions & 15 deletions tool/cmd/kitex/args/tpl_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,82 @@ func (a *Arguments) Init(cmd *util.Command, args []string) error {
return nil
}

func (a *Arguments) checkTplArgs() error {
if a.TemplateDir != "" && a.RenderTplDir != "" {
return fmt.Errorf("template render --dir and -template-dir cannot be used at the same time")
}
if a.RenderTplDir != "" && len(a.TemplateFiles) > 0 {
return fmt.Errorf("template render --dir and --file option cannot be specified at the same time")
}
return nil
}

func (a *Arguments) Root(cmd *util.Command, args []string) error {
curpath, err := filepath.Abs(".")
if err != nil {
return fmt.Errorf("get current path failed: %s", err.Error())
}
log.Verbose = a.Verbose

for _, e := range a.extends {
err := e.Check(a)
if err != nil {
return err
}
}

err = a.checkIDL(args)
if err != nil {
return err
}
err = a.checkServiceName()
if err != nil {
return err
}
err = a.checkTplArgs()
if err != nil {
return err
}
// todo finish protobuf
if a.IDLType != "thrift" {
a.GenPath = generator.KitexGenPath
}
return a.checkPath(curpath)
}

func (a *Arguments) Template(cmd *util.Command, args []string) error {
curpath, err := filepath.Abs(".")
if err != nil {
return fmt.Errorf("get current path failed: %s", err.Error())
}
log.Verbose = a.Verbose

for _, e := range a.extends {
err := e.Check(a)
if err != nil {
return err
}
}

err = a.checkIDL(args)
if err != nil {
return err
}
err = a.checkServiceName()
if err != nil {
return err
}
err = a.checkTplArgs()
if err != nil {
return err
}
// todo finish protobuf
if a.IDLType != "thrift" {
a.GenPath = generator.KitexGenPath
}
return a.checkPath(curpath)
}

func (a *Arguments) Render(cmd *util.Command, args []string) error {
curpath, err := filepath.Abs(".")
if err != nil {
Expand All @@ -189,6 +265,10 @@ func (a *Arguments) Render(cmd *util.Command, args []string) error {
if err != nil {
return err
}
err = a.checkTplArgs()
if err != nil {
return err
}
// todo finish protobuf
if a.IDLType != "thrift" {
a.GenPath = generator.KitexGenPath
Expand Down Expand Up @@ -233,10 +313,12 @@ func (a *Arguments) TemplateArgs(version string) error {
kitexCmd := &util.Command{
Use: "kitex",
Short: "Kitex command",
RunE: a.Root,
}
templateCmd := &util.Command{
Use: "template",
Short: "Template command",
RunE: a.Template,
}
initCmd := &util.Command{
Use: "init",
Expand All @@ -253,8 +335,12 @@ func (a *Arguments) TemplateArgs(version string) error {
Short: "Clean command",
RunE: a.Clean,
}
kitexCmd.Flags().StringVar(&a.GenPath, "gen-path", generator.KitexGenPath,
"Specify a code gen path.")
templateCmd.Flags().StringVar(&a.GenPath, "gen-path", generator.KitexGenPath,
"Specify a code gen path.")
initCmd.Flags().StringVarP(&a.InitOutputDir, "output", "o", ".", "Specify template init path (default current directory)")
initCmd.Flags().StringVar(&a.InitType, "type", "", "Specify template init type")
initCmd.Flags().StringVarP(&a.InitType, "type", "t", "", "Specify template init type")
renderCmd.Flags().StringVar(&a.RenderTplDir, "dir", "", "Use custom template to generate codes.")
renderCmd.Flags().StringVar(&a.ModuleName, "module", "",
"Specify the Go module name to generate go.mod.")
Expand All @@ -263,23 +349,58 @@ func (a *Arguments) TemplateArgs(version string) error {
"Specify a code gen path.")
renderCmd.Flags().StringArrayVar(&a.TemplateFiles, "file", []string{}, "Specify single template path")
renderCmd.Flags().BoolVar(&a.DebugTpl, "debug", false, "turn on debug for template")
renderCmd.Flags().VarP(&a.Includes, "Includes", "I", "Add IDL search path and template search path for includes.")
initCmd.SetUsageFunc(func() {
fmt.Fprintf(os.Stderr, `Version %s
Usage: kitex template init [flags]
`, version)
renderCmd.Flags().StringVarP(&a.IncludesTpl, "Includes", "I", "", "Add IDL search path and template search path for includes.")
renderCmd.Flags().StringVar(&a.MetaFlags, "meta", "", "Meta data in key=value format, keys separated by ';' values separated by ',' ")
templateCmd.SetHelpFunc(func(*util.Command, []string) {
fmt.Fprintln(os.Stderr, `
Template operation
Usage:
kitex template [command]
Available Commands:
init Initialize the templates according to the type
render Render the template files
clean Clean the debug templates
`)
})
initCmd.SetHelpFunc(func(*util.Command, []string) {
fmt.Fprintln(os.Stderr, `
Initialize the templates according to the type
Usage:
kitex template init [flags]
Flags:
-o, --output string Output directory
-t, --type string The init type of the template
`)
})
renderCmd.SetUsageFunc(func() {
fmt.Fprintf(os.Stderr, `Version %s
Usage: template render --dir [template dir_path] [flags] IDL
`, version)
renderCmd.SetHelpFunc(func(*util.Command, []string) {
fmt.Fprintln(os.Stderr, `
Render the template files
Usage:
kitex template render [flags]
Flags:
--dir string Output directory
--debug bool Turn on the debug mode
--file stringArray Specify multiple files for render
-I, --Includes string Add an template git search path for includes.
--meta string Specify meta data for render
--module string Specify the Go module name to generate go.mod.
-t, --type string The init type of the template
`)
})
cleanCmd.SetUsageFunc(func() {
fmt.Fprintf(os.Stderr, `Version %s
Usage: kitex template clean
`, version)
cleanCmd.SetHelpFunc(func(*util.Command, []string) {
fmt.Fprintln(os.Stderr, `
Clean the debug templates
Usage:
kitex template clean
`)
})
// renderCmd.PrintUsage()
templateCmd.AddCommand(initCmd, renderCmd, cleanCmd)
kitexCmd.AddCommand(templateCmd)
if _, err := kitexCmd.ExecuteC(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tool/cmd/kitex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func main() {
log.Warn("Get current path failed:", err.Error())
os.Exit(1)
}
if os.Args[1] == "template" {
if len(os.Args) > 1 && os.Args[1] == "template" {
err = args.TemplateArgs(kitex.Version)
} else if !strings.HasPrefix(os.Args[1], "-") {
} else if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") {
err = fmt.Errorf("unknown command %q", os.Args[1])
} else {
// run as kitex
Expand Down
Loading

0 comments on commit 4e4e8de

Please sign in to comment.