提交 d4c997cc 编写于 作者: chai2010's avatar chai2010

完善 wa init 命令参数检查

上级 02345b4c
......@@ -7,8 +7,10 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"unicode"
"wa-lang.org/wa/waroot"
)
......@@ -17,6 +19,13 @@ func InitApp(name, pkgpath string, update bool) error {
if name == "" {
return fmt.Errorf("init failed: <%s> is empty", name)
}
if !isValidAppName(name) {
return fmt.Errorf("init failed: <%s> is invalid name", name)
}
if !isValidPkgpath(pkgpath) {
return fmt.Errorf("init failed: <%s> is invalid pkgpath", pkgpath)
}
if !update {
if _, err := os.Lstat(name); err == nil {
......@@ -114,3 +123,37 @@ func InitApp(name, pkgpath string, update bool) error {
return nil
}
func isValidAppName(s string) bool {
if s == "" || s[0] == '_' || (s[0] >= '0' && s[0] <= '9') {
return false
}
for _, c := range []rune(s) {
if c == '_' || (c >= '0' && c <= '9') || unicode.IsLetter(c) {
continue
}
return false
}
return true
}
func isValidPkgpath(s string) bool {
if s == "" || s[0] == '_' || (s[0] >= '0' && s[0] <= '9') {
return false
}
for _, c := range []rune(s) {
if c == '_' || c == '.' || c == '/' || (c >= '0' && c <= '9') {
continue
}
if unicode.IsLetter(c) {
continue
}
return false
}
var pkgname = s
if i := strings.LastIndex(s, "/"); i >= 0 {
pkgname = s[i+1:]
}
return isValidAppName(pkgname)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册