From e7fc3614a81fa583cf7b509170c2aba1f5c99ed2 Mon Sep 17 00:00:00 2001 From: fancl Date: Fri, 13 Dec 2024 16:24:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=9F=E6=88=90=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generator/main.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/generator/main.go b/generator/main.go index 9d971d6..7a2df6a 100644 --- a/generator/main.go +++ b/generator/main.go @@ -7,6 +7,7 @@ import ( "errors" "flag" "fmt" + "git.nobla.cn/golang/kos/util/env" "io" "net/http" "os" @@ -18,8 +19,8 @@ import ( ) var ( - packageNameFlag = flag.String("package-name", "github.com/golang/example", "Application package name") - versionFlag = flag.String("version", "v0.0.1", "Application package version") + packageNameFlag = flag.String("package-name", "", "应用程序包名称") + versionFlag = flag.String("version", env.Get("MOTO_VERSION", "v0.0.1"), "模板版本号,可以使用环境变量 MOTO_VERSION") ) var ( @@ -70,29 +71,21 @@ func extractFile(file string, dirname string) (err error) { if header.Typeflag == tar.TypeXGlobalHeader { continue } - fileInfo := header.FileInfo() if prefix == "" && header.Typeflag == tar.TypeDir { prefix = header.Name continue } filename := path.Join(dirname, strings.TrimPrefix(header.Name, prefix)) if header.Typeflag == tar.TypeDir { - if err = os.MkdirAll(filename, fileInfo.Mode().Perm()); err != nil { + if err = os.MkdirAll(filename, 0755); err != nil { return } - if runtime.GOOS != "windows" { - if err = os.Chmod(filename, fileInfo.Mode().Perm()); err != nil { - return - } - } } else { if err = writeFile(filename, tarReader); err != nil { return err } if runtime.GOOS != "windows" { - if err = os.Chmod(filename, fileInfo.Mode().Perm()); err != nil { - return - } + os.Chmod(filename, 0644) } } } @@ -163,28 +156,33 @@ func main() { ) flag.Parse() packageName = *packageNameFlag + if packageName == "" { + if len(os.Args) > 1 { + packageName = os.Args[1] + } + } pos = strings.LastIndexByte(packageName, '/') if pos == -1 { - fmt.Println("package name invalid") + fmt.Println("应用包名称是无效的") os.Exit(1) } appname = packageName[pos:] if dirname, err = os.Getwd(); err != nil { - fmt.Println(err.Error()) + fmt.Println("获取应用目录失败:" + err.Error()) os.Exit(1) } dirname = path.Join(dirname, appname) if err = os.MkdirAll(dirname, 0755); err != nil { - fmt.Println(err.Error()) + fmt.Println("创建应用目录失败:" + err.Error()) os.Exit(1) } if err = downloadPackage(*versionFlag, dirname); err != nil { - fmt.Println("download file error:" + err.Error()) + fmt.Println("下载模板文件失败:" + err.Error()) os.Exit(1) } if err = replaceFiles(dirname, oldPackageName, packageName); err != nil { - fmt.Println(err.Error()) + fmt.Println("生产应用文件失败:" + err.Error()) os.Exit(1) } - fmt.Println("Create project successful !!!") + fmt.Println("创建项目成功") }