Update setup tool.

This commit is contained in:
Twilight 2024-06-13 19:25:03 +08:00
parent 83e8de8d02
commit 6ab34fb594

35
main.go
View File

@ -12,6 +12,8 @@ import (
"strings"
)
const frameworkModuleName = "framework_v2"
func main() {
flag.Parse()
@ -24,25 +26,30 @@ func main() {
}
func setup() {
fmt.Println("Do you want to setup the project? (y/n)")
var answer string
_, err := fmt.Scanln(&answer)
if err != nil {
fmt.Printf("Error reading user input: %v\n", err)
return
}
if answer != "y" {
return
}
// 读取 go.mod 中的 module 名称
modName, err := getModuleName("go.mod")
if err != nil {
fmt.Printf("Error reading go.mod: %v\n", err)
return
os.Exit(1)
}
fmt.Printf("Module name found: %s\n", modName)
if modName == frameworkModuleName {
fmt.Printf("Please update go.mod module to a different name.\n")
os.Exit(1)
}
fmt.Printf("Do you want to setup the project to %s? (y/n)", modName)
var answer string
_, err = fmt.Scanln(&answer)
if err != nil {
fmt.Printf("Error reading user input: %v\n", err)
os.Exit(1)
}
if answer != "y" {
fmt.Printf("Aborting setup.\n")
}
fmt.Printf("Module name found: %s\n", modName)
// 遍历当前文件夹(排除 vendor、setup.go 和版本控制文件夹)
err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
@ -59,7 +66,7 @@ func setup() {
// 处理文件
if !info.IsDir() {
err := replaceInFile(path, `"framework_v2`, fmt.Sprintf(`"%s`, modName))
err := replaceInFile(path, `"`+frameworkModuleName, fmt.Sprintf(`"%s`, modName))
if err != nil {
fmt.Printf("Error replacing in file %s: %v\n", path, err)
}