优化生成工具逻辑

This commit is contained in:
fancl 2024-12-13 16:24:43 +08:00
parent 84c9206d22
commit e7fc3614a8
1 changed files with 16 additions and 18 deletions

View File

@ -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("创建项目成功")
}