rpc service generation (#26)

* add execute files

* add protoc-osx

* add rpc generation

* add rpc generation

* add: rpc template generation

* update usage

* fixed env prepare for project in go path

* optimize gomod cache

* add README.md

* format error

* reactor templatex.go

* remove waste code
This commit is contained in:
Keson
2020-08-28 19:24:58 +08:00
committed by GitHub
parent 71bbf91a63
commit db16115037
36 changed files with 2021 additions and 79 deletions

View File

@@ -2,6 +2,7 @@ package console
import (
"fmt"
"os"
"github.com/logrusorgru/aurora"
)
@@ -9,8 +10,11 @@ import (
type (
Console interface {
Success(format string, a ...interface{})
Info(format string, a ...interface{})
Warning(format string, a ...interface{})
Error(format string, a ...interface{})
Fatalln(format string, a ...interface{})
Must(err error)
}
colorConsole struct {
}
@@ -30,6 +34,11 @@ func NewColorConsole() *colorConsole {
return &colorConsole{}
}
func (c *colorConsole) Info(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Println(msg)
}
func (c *colorConsole) Success(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Println(aurora.Green(msg))
@@ -45,10 +54,26 @@ func (c *colorConsole) Error(format string, a ...interface{}) {
fmt.Println(aurora.Red(msg))
}
func (c *colorConsole) Fatalln(format string, a ...interface{}) {
c.Error(format, a...)
os.Exit(1)
}
func (c *colorConsole) Must(err error) {
if err != nil {
c.Fatalln("%+v", err)
}
}
func NewIdeaConsole() *ideaConsole {
return &ideaConsole{}
}
func (i *ideaConsole) Info(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Println(msg)
}
func (i *ideaConsole) Success(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Println("[SUCCESS]: ", msg)
@@ -63,3 +88,14 @@ func (i *ideaConsole) Error(format string, a ...interface{}) {
msg := fmt.Sprintf(format, a...)
fmt.Println("[ERROR]: ", msg)
}
func (i *ideaConsole) Fatalln(format string, a ...interface{}) {
i.Error(format, a...)
os.Exit(1)
}
func (i *ideaConsole) Must(err error) {
if err != nil {
i.Fatalln("%+v", err)
}
}

View File

@@ -2,18 +2,12 @@ package util
import (
"bufio"
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/core/logx"
)
func CreateIfNotExist(file string) (*os.File, error) {
@@ -53,44 +47,3 @@ func FileExists(file string) bool {
func FileNameWithoutExt(file string) string {
return strings.TrimSuffix(file, filepath.Ext(file))
}
func CreateTemplateAndExecute(filename, text string, arg map[string]interface{}, forceUpdate bool, disableFormatCodeArgs ...bool) error {
if FileExists(filename) && !forceUpdate {
return nil
}
var buffer = new(bytes.Buffer)
templateName := fmt.Sprintf("%d", time.Now().UnixNano())
t, err := template.New(templateName).Parse(text)
if err != nil {
return err
}
err = t.Execute(buffer, arg)
if err != nil {
return err
}
var disableFormatCode bool
for _, f := range disableFormatCodeArgs {
disableFormatCode = f
}
var bts = buffer.Bytes()
s := buffer.String()
logx.Info(s)
if !disableFormatCode {
bts, err = format.Source(buffer.Bytes())
if err != nil {
return err
}
}
return ioutil.WriteFile(filename, bts, os.ModePerm)
}
func FormatCodeAndWrite(filename string, code []byte) error {
if FileExists(filename) {
return nil
}
bts, err := format.Source(code)
if err != nil {
return err
}
return ioutil.WriteFile(filename, bts, os.ModePerm)
}

11
tools/goctl/util/head.go Normal file
View File

@@ -0,0 +1,11 @@
package util
var headTemplate = `// Code generated by goctl. DO NOT EDIT.
// Source: {{.source}}`
func GetHead(source string) string {
buffer, _ := With("head").Parse(headTemplate).Execute(map[string]interface{}{
"source": source,
})
return buffer.String()
}

View File

@@ -1,4 +1,4 @@
package templatex
package util
import (
"bytes"
@@ -32,7 +32,10 @@ func (t *defaultTemplate) GoFmt(format bool) *defaultTemplate {
return t
}
func (t *defaultTemplate) SaveTo(data interface{}, path string) error {
func (t *defaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
if FileExists(path) && !forceUpdate {
return nil
}
output, err := t.execute(data)
if err != nil {
return err