goctl added

This commit is contained in:
kim
2020-07-29 17:11:41 +08:00
parent b1975d29a7
commit 121323b8c3
142 changed files with 10690 additions and 0 deletions

35
tools/goctl/util/env.go Normal file
View File

@@ -0,0 +1,35 @@
package util
import (
"fmt"
"os"
"path"
"strings"
)
func GetFullPackage(pkg string) (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
pkgPath := path.Join(dir, pkg)
info, err := os.Stat(pkgPath)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("%s is not a directory", pkg)
}
gopath := os.Getenv("GOPATH")
parent := path.Join(gopath, "src")
pos := strings.Index(pkgPath, parent)
if pos < 0 {
return "", fmt.Errorf("%s is not a correct package", pkg)
}
// skip slash
return pkgPath[len(parent)+1:], nil
}

97
tools/goctl/util/file.go Normal file
View File

@@ -0,0 +1,97 @@
package util
import (
"bufio"
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/logrusorgru/aurora"
"zero/core/logx"
)
func CreateIfNotExist(file string) (*os.File, error) {
_, err := os.Stat(file)
if !os.IsNotExist(err) {
return nil, fmt.Errorf("%s already exist", file)
}
return os.Create(file)
}
func RemoveIfExist(filename string) error {
if !FileExists(filename) {
return nil
}
return os.Remove(filename)
}
func RemoveOrQuit(filename string) error {
if !FileExists(filename) {
return nil
}
fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...",
aurora.BgRed(aurora.Bold(filename)))
bufio.NewReader(os.Stdin).ReadBytes('\n')
return os.Remove(filename)
}
func FileExists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
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)
}

53
tools/goctl/util/path.go Normal file
View File

@@ -0,0 +1,53 @@
package util
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"zero/tools/goctl/vars"
)
func MkdirIfNotExist(dir string) error {
if len(dir) == 0 {
return nil
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, os.ModePerm)
}
return nil
}
func PathFromGoSrc() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
gopath := os.Getenv("GOPATH")
parent := path.Join(gopath, "src", vars.ProjectName)
pos := strings.Index(dir, parent)
if pos < 0 {
return "", fmt.Errorf("%s is not in GOPATH", dir)
}
// skip slash
return dir[len(parent)+1:], nil
}
func GetParentPackage(dir string) (string, error) {
absDir, err := filepath.Abs(dir)
if err != nil {
return "", err
}
pos := strings.Index(absDir, vars.ProjectName)
if pos < 0 {
return "", fmt.Errorf("error dir:[%s],please make sure that your project is in the %s directory", vars.ProjectName, dir)
}
return absDir[pos:], nil
}

View File

@@ -0,0 +1,19 @@
package util
import "strings"
func Title(s string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}
func Untitle(s string) string {
if len(s) == 0 {
return s
}
return strings.ToLower(s[:1]) + s[1:]
}