Skip to content

Commit

Permalink
move function generate templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vlasic committed Sep 30, 2021
1 parent d96f8c6 commit bd46c5c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions generate/function.go → cli/cmd/generate/function.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package generate

type Function struct {
type function struct {
Name string
ImportPath string
}

type Method struct {
type method struct {
Name string
FunctionName string
}

type Test struct {
type test struct {
Name string
ImportPath string
Methods []string
}

var APIDefaultTemplate = `
var apiDefaultTemplate = `
package {{ .Name | toLower }}
import (
Expand All @@ -37,7 +37,7 @@ func ({{ .Name | first | toLower }} *{{ .Name | title }}) Default(ctx context.Co
}
`

var APIMethodTemplate = `
var apiMethodTemplate = `
package {{ .FunctionName | toLower }}
import (
Expand All @@ -52,7 +52,7 @@ func ({{ .FunctionName | first | toLower }} *{{ .FunctionName | title }}) {{ .Na
}
`

var APIFunctionMainTemplate = `
var apiFunctionMainTemplate = `
// Code generated by mantil DO NOT EDIT
package main
Expand All @@ -67,7 +67,7 @@ func main() {
}
`

var APIFunctionTestInit = `
var apiFunctionTestInit = `
package main
import (
Expand All @@ -91,7 +91,7 @@ func init() {
}
`

var APIFunctionTestTemplate = `
var apiFunctionTestTemplate = `
package main
import (
Expand Down
24 changes: 12 additions & 12 deletions cli/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func generateFunctionMain(functionName, importPath, projectPath string) error {
}
log.Info("generating %s", relativePath(projectPath, mainFile))
if err := generate.GenerateFromTemplate(
generate.APIFunctionMainTemplate,
&generate.Function{
apiFunctionMainTemplate,
&function{
Name: functionName,
ImportPath: importPath,
},
Expand Down Expand Up @@ -105,26 +105,26 @@ func generateApiDefault(projectPath, functionName string) error {
}
log.Info("generating %s", relativePath(projectPath, defaultFile))
err := generate.GenerateFromTemplate(
generate.APIDefaultTemplate,
&generate.Function{Name: functionName},
apiDefaultTemplate,
&function{Name: functionName},
defaultFile,
)
return err
}

func generateApiMethods(projectPath, functionName string, methods []string) error {
functionApi := filepath.Join(projectPath, "api", functionName)
for _, method := range methods {
methodFile := filepath.Join(functionApi, fmt.Sprintf("%s.go", method))
for _, m := range methods {
methodFile := filepath.Join(functionApi, fmt.Sprintf("%s.go", m))
if fileExists(methodFile) {
log.Info("%s already exists", relativePath(projectPath, methodFile))
continue
}
log.Info("generating %s", relativePath(projectPath, methodFile))
if err := generate.GenerateFromTemplate(
generate.APIMethodTemplate,
&generate.Method{
Name: method,
apiMethodTemplate,
&method{
Name: m,
FunctionName: functionName,
},
methodFile,
Expand Down Expand Up @@ -153,7 +153,7 @@ func generateApiTestInit(projectPath string) error {
}
log.Info("generating %s", relativePath(projectPath, initTest))
if err := generate.GenerateFile(
generate.APIFunctionTestInit,
apiFunctionTestInit,
initTest,
); err != nil {
return err
Expand All @@ -169,8 +169,8 @@ func generateApiTest(importPath, projectPath, functionName string, methods []str
}
log.Info("generating %s", relativePath(projectPath, apiTest))
if err := generate.GenerateFromTemplate(
generate.APIFunctionTestTemplate,
&generate.Test{
apiFunctionTestTemplate,
&test{
Name: functionName,
ImportPath: importPath,
Methods: methods,
Expand Down

0 comments on commit bd46c5c

Please sign in to comment.