Code optimized (#493)
This commit is contained in:
@@ -8,6 +8,9 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// Console wraps from the fmt.Sprintf,
|
||||
// by default, it implemented the colorConsole to provide the colorful output to the consle
|
||||
// and the ideaConsole to output with prefix for the plugin of intellij
|
||||
Console interface {
|
||||
Success(format string, a ...interface{})
|
||||
Info(format string, a ...interface{})
|
||||
@@ -25,6 +28,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// NewConsole returns a instance of Console
|
||||
func NewConsole(idea bool) Console {
|
||||
if idea {
|
||||
return NewIdeaConsole()
|
||||
@@ -32,7 +36,8 @@ func NewConsole(idea bool) Console {
|
||||
return NewColorConsole()
|
||||
}
|
||||
|
||||
func NewColorConsole() *colorConsole {
|
||||
// NewColorConsole returns a instance of colorConsole
|
||||
func NewColorConsole() Console {
|
||||
return &colorConsole{}
|
||||
}
|
||||
|
||||
@@ -76,7 +81,8 @@ func (c *colorConsole) Must(err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func NewIdeaConsole() *ideaConsole {
|
||||
// NewIdeaConsole returns a instace of ideaConsole
|
||||
func NewIdeaConsole() Console {
|
||||
return &ideaConsole{}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
|
||||
var errModuleCheck = errors.New("the work directory must be found in the go mod or the $GOPATH")
|
||||
|
||||
// ProjectContext is a structure for the project,
|
||||
// which contains WorkDir, Name, Path and Dir
|
||||
type ProjectContext struct {
|
||||
WorkDir string
|
||||
// Name is the root name of the project
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||
)
|
||||
|
||||
// Module contains the relative data of go module,
|
||||
// which is the result of the command go list
|
||||
type Module struct {
|
||||
Path string
|
||||
Main bool
|
||||
|
||||
@@ -10,9 +10,7 @@ import (
|
||||
"github.com/logrusorgru/aurora"
|
||||
)
|
||||
|
||||
const (
|
||||
NL = "\n"
|
||||
)
|
||||
const NL = "\n"
|
||||
|
||||
func CreateIfNotExist(file string) (*os.File, error) {
|
||||
_, err := os.Stat(file)
|
||||
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
upper
|
||||
)
|
||||
|
||||
// ErrNamingFormat defines an error for unknown fomat
|
||||
var ErrNamingFormat = errors.New("unsupported format")
|
||||
|
||||
type (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Package name provides methods to verify naming style and format naming style
|
||||
// See the method IsNamingValid, FormatFilename
|
||||
package name
|
||||
|
||||
import (
|
||||
@@ -6,11 +8,15 @@ import (
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
|
||||
)
|
||||
|
||||
// NamingStyle the type of string
|
||||
type NamingStyle = string
|
||||
|
||||
const (
|
||||
// NamingLower defines the lower spell case
|
||||
NamingLower NamingStyle = "lower"
|
||||
// NamingCamel defines the camel spell case
|
||||
NamingCamel NamingStyle = "camel"
|
||||
// NamingSnake defines the snake spell case
|
||||
NamingSnake NamingStyle = "snake"
|
||||
)
|
||||
|
||||
@@ -29,6 +35,8 @@ func IsNamingValid(namingStyle string) (NamingStyle, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// FormatFilename converts the filename string to the target
|
||||
// naming style by calling method of stringx
|
||||
func FormatFilename(filename string, style NamingStyle) string {
|
||||
switch style {
|
||||
case NamingCamel:
|
||||
|
||||
@@ -6,14 +6,17 @@ import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// String provides for coverting the source text into other spell case,like lower,snake,camel
|
||||
type String struct {
|
||||
source string
|
||||
}
|
||||
|
||||
// From converts the input text to String and returns it
|
||||
func From(data string) String {
|
||||
return String{source: data}
|
||||
}
|
||||
|
||||
// IsEmptyOrSpace returns true if the length of the string value is 0 after call strings.TrimSpace, or else returns false
|
||||
func (s String) IsEmptyOrSpace() bool {
|
||||
if len(s.source) == 0 {
|
||||
return true
|
||||
@@ -24,18 +27,22 @@ func (s String) IsEmptyOrSpace() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Lower calls the strings.ToLower
|
||||
func (s String) Lower() string {
|
||||
return strings.ToLower(s.source)
|
||||
}
|
||||
|
||||
// ReplaceAll calls the strings.ReplaceAll
|
||||
func (s String) ReplaceAll(old, new string) string {
|
||||
return strings.ReplaceAll(s.source, old, new)
|
||||
}
|
||||
|
||||
//Source returns the source string value
|
||||
func (s String) Source() string {
|
||||
return s.source
|
||||
}
|
||||
|
||||
// Title calls the strings.Title
|
||||
func (s String) Title() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
@@ -43,7 +50,7 @@ func (s String) Title() string {
|
||||
return strings.Title(s.source)
|
||||
}
|
||||
|
||||
// snake->camel(upper start)
|
||||
// ToCamel converts the input text into camel case
|
||||
func (s String) ToCamel() string {
|
||||
list := s.splitBy(func(r rune) bool {
|
||||
return r == '_'
|
||||
@@ -55,7 +62,7 @@ func (s String) ToCamel() string {
|
||||
return strings.Join(target, "")
|
||||
}
|
||||
|
||||
// camel->snake
|
||||
// ToSnake converts the input text into snake case
|
||||
func (s String) ToSnake() string {
|
||||
list := s.splitBy(unicode.IsUpper, false)
|
||||
var target []string
|
||||
@@ -65,7 +72,7 @@ func (s String) ToSnake() string {
|
||||
return strings.Join(target, "_")
|
||||
}
|
||||
|
||||
// return original string if rune is not letter at index 0
|
||||
// Untitle return the original string if rune is not letter at index 0
|
||||
func (s String) Untitle() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
@@ -77,10 +84,6 @@ func (s String) Untitle() string {
|
||||
return string(unicode.ToLower(r)) + s.source[1:]
|
||||
}
|
||||
|
||||
func (s String) Upper() string {
|
||||
return strings.ToUpper(s.source)
|
||||
}
|
||||
|
||||
// it will not ignore spaces
|
||||
func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
|
||||
@@ -9,29 +9,35 @@ import (
|
||||
|
||||
const regularPerm = 0666
|
||||
|
||||
type defaultTemplate struct {
|
||||
// DefaultTemplate is a tool to provides the text/template operations
|
||||
type DefaultTemplate struct {
|
||||
name string
|
||||
text string
|
||||
goFmt bool
|
||||
savePath string
|
||||
}
|
||||
|
||||
func With(name string) *defaultTemplate {
|
||||
return &defaultTemplate{
|
||||
// With returns a instace of DefaultTemplate
|
||||
func With(name string) *DefaultTemplate {
|
||||
return &DefaultTemplate{
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
func (t *defaultTemplate) Parse(text string) *defaultTemplate {
|
||||
|
||||
// Parse accepts a source template and returns DefaultTemplate
|
||||
func (t *DefaultTemplate) Parse(text string) *DefaultTemplate {
|
||||
t.text = text
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) GoFmt(format bool) *defaultTemplate {
|
||||
// GoFmt sets the value to goFmt and marks the generated codes will be formated or not
|
||||
func (t *DefaultTemplate) GoFmt(format bool) *DefaultTemplate {
|
||||
t.goFmt = format
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
|
||||
// SaveTo writes the codes to the target path
|
||||
func (t *DefaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error {
|
||||
if FileExists(path) && !forceUpdate {
|
||||
return nil
|
||||
}
|
||||
@@ -44,7 +50,8 @@ func (t *defaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool
|
||||
return ioutil.WriteFile(path, output.Bytes(), regularPerm)
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) Execute(data interface{}) (*bytes.Buffer, error) {
|
||||
// Execute returns the codes after the template executed
|
||||
func (t *DefaultTemplate) Execute(data interface{}) (*bytes.Buffer, error) {
|
||||
tem, err := template.New(t.name).Parse(t.text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user