Skip to content

Commit

Permalink
implement api generation
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Aug 10, 2021
1 parent 3081907 commit 1070b83
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/mantil/cmd/generateApi.go
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)
}
}
52 changes: 52 additions & 0 deletions internal/generate/function.go
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")
}
`
58 changes: 58 additions & 0 deletions internal/generate/template.go
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
}

0 comments on commit 1070b83

Please sign in to comment.