feat: api 完成

This commit is contained in:
2022-01-06 22:32:03 +08:00
parent 4bfb639866
commit fb2fddba93
10 changed files with 232 additions and 97 deletions
+48
View File
@@ -0,0 +1,48 @@
package util
import (
"bufio"
"fmt"
"github.com/fatih/color"
"os"
"strings"
)
func ReadBool() bool {
read := bufio.NewReader(os.Stdin)
ret, _ := read.ReadByte()
switch ret {
case 'y':
fallthrough
case 'Y':
return true
case 'n':
fallthrough
case 'N':
return false
}
return false
}
func ReadBoolWithMessage(message string) bool {
NoNewLinePrint(color.HiRedString, message)
return ReadBool()
}
func ReadLine() string {
read := bufio.NewReader(os.Stdin)
s, _ := read.ReadString('\n')
return s[:len(s)-1]
}
func ReadLineWithMessage(message string) string {
NoNewLinePrint(color.HiRedString, message)
return ReadLine()
}
func NoNewLinePrint(f func(format string, a ...interface{}) string, message string) {
if strings.HasSuffix(message, "\n") {
message = message[:len(message)-1]
}
fmt.Printf(f(message))
}
+28
View File
@@ -31,3 +31,31 @@ func TemplateToFile(desFile string, templateContent string, data interface{}) er
}
return nil
}
func WriteFile(filePath string, data []byte) error {
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(data)
return err
}
func WriteFileWithNotEdit(filePath string, data []byte) error {
notEdit := "# Code generated by wujian-develop_tool. DO NOT EDIT.\n\n"
return WriteFile(filePath, []byte(notEdit+string(data)))
}
// ExistFile 文件是否存在
func ExistFile(filePath string) bool {
info, err := os.Stat(filePath)
return err == nil && !info.IsDir()
}
// ExistDir 目录是否存在
func ExistDir(dirPath string) bool {
info, err := os.Stat(dirPath)
return err == nil && info.IsDir()
}