-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
171 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/atoz-technology/mantil-cli/internal/generate" | ||
"github.com/atoz-technology/mantil-cli/internal/mantil" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// generateApiCmd represents the generateApi command | ||
var generateApiCmd = &cobra.Command{ | ||
Use: "generateApi", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
name := args[0] | ||
methods, err := cmd.Flags().GetStringSlice("methods") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
path, err := mantil.FindProjectRoot(".") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
generateRoot(name, path) | ||
for _, method := range methods { | ||
generateMethod(method, name, path) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(generateApiCmd) | ||
generateApiCmd.Flags().StringSliceP("methods", "m", nil, "Specify additional function methods, if left empty only the root method will be created.") | ||
} | ||
|
||
func generateRoot(name, path string) { | ||
rootFile := fmt.Sprintf("%s/functions/%s/root.go", path, name) | ||
if err := generate.GenerateFromTemplate( | ||
generate.APIRootTemplate, | ||
&generate.Function{Name: name}, | ||
rootFile, | ||
); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func generateMethod(name, functionName, path string) { | ||
methodFile := fmt.Sprintf("%s/functions/%s/%s.go", path, functionName, name) | ||
if err := generate.GenerateFromTemplate( | ||
generate.APIMethodTemplate, | ||
&generate.Method{ | ||
Name: name, | ||
FunctionName: functionName, | ||
}, | ||
methodFile, | ||
); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,52 @@ | ||
package generate | ||
|
||
type Function struct { | ||
Name string | ||
} | ||
|
||
type Method struct { | ||
Name string | ||
FunctionName string | ||
} | ||
|
||
var APIRootTemplate = ` | ||
package {{ .Name | toLower }} | ||
import ( | ||
"context" | ||
) | ||
type {{ .Name | title }} struct{} | ||
type RootRequest struct{} | ||
type RootResponse struct{} | ||
func (h *{{ .Name | title }}) Init(ctx context.Context) {} | ||
func (h *{{ .Name | title }}) Invoke(ctx context.Context, req *RootRequest) (*RootResponse, error) { | ||
return h.Root(ctx, req) | ||
} | ||
func (h *{{ .Name | title }}) Root(ctx context.Context, req *RootRequest) (*RootResponse, error) { | ||
panic("not implemented") | ||
} | ||
func New() *{{ .Name | title }} { | ||
return &{{ .Name | title }}{} | ||
} | ||
` | ||
|
||
var APIMethodTemplate = ` | ||
package {{ .FunctionName | toLower }} | ||
import ( | ||
"context" | ||
) | ||
type {{ .Name | title }}Request struct{} | ||
type {{ .Name | title }}Response struct{} | ||
func (h *{{ .FunctionName | title }}) {{ .Name | title }}(ctx context.Context, req *{{ .Name | title }}Request) (*{{ .Name | title }}Response, error) { | ||
panic("not implemented") | ||
} | ||
` |
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,58 @@ | ||
package generate | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func GenerateFromTemplate(tplDef string, data interface{}, outPath string) error { | ||
out, err := runTemplate(tplDef, data) | ||
if err != nil { | ||
return err | ||
} | ||
out, err = format(out) | ||
if err != nil { | ||
return err | ||
} | ||
return save(out, outPath) | ||
} | ||
|
||
func runTemplate(tplDef string, data interface{}) ([]byte, error) { | ||
fcs := template.FuncMap{ | ||
"join": strings.Join, | ||
"toLower": strings.ToLower, | ||
"title": strings.Title, | ||
} | ||
tpl := template.Must(template.New("").Funcs(fcs).Parse(tplDef)) | ||
buf := bytes.NewBuffer(nil) | ||
if err := tpl.Execute(buf, data); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func format(in []byte) ([]byte, error) { | ||
cmd := exec.Command("gofmt") | ||
cmd.Stdin = strings.NewReader(string(in)) | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
|
||
func save(in []byte, path string) error { | ||
dir := filepath.Dir(path) | ||
if err := os.MkdirAll(dir, os.ModePerm); err != nil { | ||
return err | ||
} | ||
if err := ioutil.WriteFile(path, in, 0644); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |