Code optimized (#523)
* optimized markdown generator * optimized markdown generator * optimized markdown generator * add more comment * add comment * add comment * add comments for rpc tool * add comments for model tool * add comments for model tool * add comments for model tool * add comments for config tool * add comments for config tool * add comments * add comments * add comments * add comments * add comment * remove rpc main head info * add comment * optimized Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
This commit is contained in:
@@ -28,7 +28,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// NewConsole returns a instance of Console
|
||||
// NewConsole returns an instance of Console
|
||||
func NewConsole(idea bool) Console {
|
||||
if idea {
|
||||
return NewIdeaConsole()
|
||||
@@ -36,7 +36,7 @@ func NewConsole(idea bool) Console {
|
||||
return NewColorConsole()
|
||||
}
|
||||
|
||||
// NewColorConsole returns a instance of colorConsole
|
||||
// NewColorConsole returns an instance of colorConsole
|
||||
func NewColorConsole() Console {
|
||||
return &colorConsole{}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -10,8 +11,13 @@ import (
|
||||
"github.com/logrusorgru/aurora"
|
||||
)
|
||||
|
||||
const NL = "\n"
|
||||
// NL defines a new line
|
||||
const (
|
||||
NL = "\n"
|
||||
goctlDir = ".goctl"
|
||||
)
|
||||
|
||||
// CreateIfNotExist creates a file if it is not exists
|
||||
func CreateIfNotExist(file string) (*os.File, error) {
|
||||
_, err := os.Stat(file)
|
||||
if !os.IsNotExist(err) {
|
||||
@@ -21,6 +27,7 @@ func CreateIfNotExist(file string) (*os.File, error) {
|
||||
return os.Create(file)
|
||||
}
|
||||
|
||||
// RemoveIfExist deletes the specficed file if it is exists
|
||||
func RemoveIfExist(filename string) error {
|
||||
if !FileExists(filename) {
|
||||
return nil
|
||||
@@ -29,6 +36,7 @@ func RemoveIfExist(filename string) error {
|
||||
return os.Remove(filename)
|
||||
}
|
||||
|
||||
// RemoveOrQuit deletes the specficed file if read a permit command from stdin
|
||||
func RemoveOrQuit(filename string) error {
|
||||
if !FileExists(filename) {
|
||||
return nil
|
||||
@@ -41,11 +49,105 @@ func RemoveOrQuit(filename string) error {
|
||||
return os.Remove(filename)
|
||||
}
|
||||
|
||||
// FileExists returns true if the specficed file is exists
|
||||
func FileExists(file string) bool {
|
||||
_, err := os.Stat(file)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// FileNameWithoutExt returns a file name without suffix
|
||||
func FileNameWithoutExt(file string) string {
|
||||
return strings.TrimSuffix(file, filepath.Ext(file))
|
||||
}
|
||||
|
||||
// GetGoctlHome returns the path value of the goctl home where Join $HOME with .goctl
|
||||
func GetGoctlHome() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, goctlDir), nil
|
||||
}
|
||||
|
||||
// GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome
|
||||
func GetTemplateDir(category string) (string, error) {
|
||||
goctlHome, err := GetGoctlHome()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(goctlHome, category), nil
|
||||
}
|
||||
|
||||
// InitTemplates creates template files GoctlHome where could get it by GetGoctlHome
|
||||
func InitTemplates(category string, templates map[string]string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := MkdirIfNotExist(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range templates {
|
||||
if err := createTemplate(filepath.Join(dir, k), v, false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateTemplate writes template into file even it is exists
|
||||
func CreateTemplate(category, name, content string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return createTemplate(filepath.Join(dir, name), content, true)
|
||||
}
|
||||
|
||||
// Clean deletes all templates and removes the parent directory
|
||||
func Clean(category string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.RemoveAll(dir)
|
||||
}
|
||||
|
||||
// LoadTemplate gets template content by the specified file
|
||||
func LoadTemplate(category, file, builtin string) (string, error) {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
file = filepath.Join(dir, file)
|
||||
if !FileExists(file) {
|
||||
return builtin, nil
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
func createTemplate(file, content string, force bool) error {
|
||||
if FileExists(file) && !force {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Create(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(content)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const goctlDir = ".goctl"
|
||||
|
||||
func GetGoctlHome() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, goctlDir), nil
|
||||
}
|
||||
|
||||
func GetTemplateDir(category string) (string, error) {
|
||||
goctlHome, err := GetGoctlHome()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(goctlHome, category), nil
|
||||
}
|
||||
|
||||
func InitTemplates(category string, templates map[string]string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := MkdirIfNotExist(dir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for k, v := range templates {
|
||||
if err := createTemplate(filepath.Join(dir, k), v, false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateTemplate(category, name, content string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return createTemplate(filepath.Join(dir, name), content, true)
|
||||
}
|
||||
|
||||
func Clean(category string) error {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.RemoveAll(dir)
|
||||
}
|
||||
|
||||
func LoadTemplate(category, file, builtin string) (string, error) {
|
||||
dir, err := GetTemplateDir(category)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
file = filepath.Join(dir, file)
|
||||
if !FileExists(file) {
|
||||
return builtin, nil
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
func createTemplate(file, content string, force bool) error {
|
||||
if FileExists(file) && !force {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Create(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(content)
|
||||
return err
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package util
|
||||
var headTemplate = `// Code generated by goctl. DO NOT EDIT!
|
||||
// Source: {{.source}}`
|
||||
|
||||
// GetHead returns a code head string with source filename
|
||||
func GetHead(source string) string {
|
||||
buffer, _ := With("head").Parse(headTemplate).Execute(map[string]interface{}{
|
||||
"source": source,
|
||||
|
||||
@@ -15,10 +15,12 @@ const (
|
||||
goModeIdentifier = "go.mod"
|
||||
)
|
||||
|
||||
// JoinPackages calls strings.Join and returns
|
||||
func JoinPackages(pkgs ...string) string {
|
||||
return strings.Join(pkgs, pkgSep)
|
||||
}
|
||||
|
||||
// MkdirIfNotExist makes directories if the input path is not exists
|
||||
func MkdirIfNotExist(dir string) error {
|
||||
if len(dir) == 0 {
|
||||
return nil
|
||||
@@ -31,6 +33,7 @@ func MkdirIfNotExist(dir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PathFromGoSrc returns the path whihout slash where has been trim the prefix $GOPATH
|
||||
func PathFromGoSrc() (string, error) {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
@@ -48,6 +51,8 @@ func PathFromGoSrc() (string, error) {
|
||||
return dir[len(parent)+1:], nil
|
||||
}
|
||||
|
||||
// FindGoModPath returns the path in project where has file go.mod, it maybe return empty string if
|
||||
// there is no go.mod file in project
|
||||
func FindGoModPath(dir string) (string, bool) {
|
||||
absDir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
@@ -80,6 +85,7 @@ func FindGoModPath(dir string) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// FindProjectPath returns the parent directory where has file go.mod in project
|
||||
func FindProjectPath(loc string) (string, bool) {
|
||||
var dir string
|
||||
if strings.IndexByte(loc, '/') == 0 {
|
||||
|
||||
@@ -2,6 +2,8 @@ package util
|
||||
|
||||
import "strings"
|
||||
|
||||
// Title returns a string value with s[0] which has been convert into upper case that
|
||||
// there are not empty input text
|
||||
func Title(s string) string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
@@ -10,6 +12,8 @@ func Title(s string) string {
|
||||
return strings.ToUpper(s[:1]) + s[1:]
|
||||
}
|
||||
|
||||
// Untitle returns a string value with s[0] which has been convert into lower case that
|
||||
// there are not empty input text
|
||||
func Untitle(s string) string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
@@ -18,6 +22,7 @@ func Untitle(s string) string {
|
||||
return strings.ToLower(s[:1]) + s[1:]
|
||||
}
|
||||
|
||||
// Index returns the index where the item equal,it will return -1 if mismatched
|
||||
func Index(slice []string, item string) int {
|
||||
for i := range slice {
|
||||
if slice[i] == item {
|
||||
|
||||
Reference in New Issue
Block a user