Add setup.go

This commit is contained in:
Twilight 2024-06-13 09:32:28 +08:00
parent 45ef118947
commit 59421d2c81
3 changed files with 96 additions and 73 deletions

View File

@ -0,0 +1,5 @@
# 使用步骤
1. 修改 go.mod 中的模块名字
2. 运行 go run setup.go 初始化项目
3. 到 providerscmd/migrate.go 中初始化 ent

73
go.mod
View File

@ -1,73 +0,0 @@
module framework_v2
go 1.22.3
require (
ariga.io/atlas v0.23.0
entgo.io/ent v0.13.1
github.com/MicahParks/keyfunc/v3 v3.3.3
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/hibiken/asynq v0.24.1
github.com/joho/godotenv v1.5.1
github.com/kos-v/dsnparser v1.1.0
github.com/lib/pq v1.10.9
github.com/meilisearch/meilisearch-go v0.26.3
github.com/mitchellh/mapstructure v1.5.0
github.com/pgvector/pgvector-go v0.1.1
github.com/redis/go-redis/v9 v9.5.3
github.com/spf13/cobra v1.8.0
go.uber.org/zap v1.27.0
golang.org/x/net v0.26.0
google.golang.org/grpc v1.64.0
gorm.io/gorm v1.25.10
leafdev.top/leaf/leaf-library v0.0.0-20240530030500-b59e2fc3aeb6
)
require (
github.com/MicahParks/jwkset v0.5.18 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.5+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.54.0 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect
google.golang.org/protobuf v1.34.1 // indirect
)

91
setup.go Normal file
View File

@ -0,0 +1,91 @@
package main
import (
"bufio"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
func main() {
// 读取 go.mod 中的 module 名称
modName, err := getModuleName("go.mod")
if err != nil {
fmt.Printf("Error reading go.mod: %v\n", err)
return
}
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 {
return err
}
// 条件排除
if info.IsDir() && (info.Name() == "vendor" || info.Name() == ".git") {
return filepath.SkipDir
}
if !info.IsDir() && info.Name() == "setup.go" {
return nil
}
// 处理文件
if !info.IsDir() {
err := replaceInFile(path, `"leafdev.top/leaf/framework_v2`, fmt.Sprintf(`"%s`, modName))
if err != nil {
fmt.Printf("Error replacing in file %s: %v\n", path, err)
}
}
return nil
})
if err != nil {
fmt.Printf("Error walking the path: %v\n", err)
}
}
// 读取 go.mod 文件中的 module 名称
func getModuleName(modFilePath string) (string, error) {
file, err := os.Open(modFilePath)
if err != nil {
return "", err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Printf("Error closing file: %v\n", err)
}
}(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "module ") {
return strings.TrimSpace(strings.TrimPrefix(line, "module ")), nil
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("module name not found in go.mod")
}
// 在文件中替换指定的字符串
func replaceInFile(filePath string, old string, new string) error {
input, err := os.ReadFile(filePath)
if err != nil {
return err
}
output := strings.ReplaceAll(string(input), old, new)
if err = os.WriteFile(filePath, []byte(output), 0666); err != nil {
return err
}
return nil
}