feat: 更新模块路径为 v2,修正文档和代码中的相关引用

This commit is contained in:
Rogee
2025-09-10 14:54:16 +08:00
parent 8bcc2cbdad
commit 0d05d70fb1
18 changed files with 328 additions and 308 deletions

View File

@@ -3,7 +3,7 @@
## Install ## Install
``` ```
go install go.ipao.vip/atomctl@latest go install go.ipao.vip/atomctl/v2@latest
``` ```
## 命令列表 ## 命令列表
@@ -11,45 +11,49 @@ go install go.ipao.vip/atomctl@latest
### 生成命令 ### 生成命令
#### gen model (别名m) #### gen model (别名m)
- 描述生成jet模型
- 描述:生成 jet 模型
- 功能: - 功能:
- 从PostgreSQL数据库生成模型 - PostgreSQL 数据库生成模型
- 使用database/transform.yaml配置文件进行类型转换 - 使用 database/transform.yaml 配置文件进行类型转换
- 支持忽略特定表和枚举 - 支持忽略特定表和枚举
- 生成JSON标签 - 生成 JSON 标签
- 支持自定义字段类型映射 - 支持自定义字段类型映射
- 自动生成数据库schema文件 - 自动生成数据库 schema 文件
#### gen provider (别名p) #### gen provider (别名p)
- 描述生成provider
- 描述:生成 provider
- 参数: - 参数:
- path可选指定生成路径默认当前目录 - path可选指定生成路径默认当前目录
- 功能: - 功能:
- 解析指定目录下的.go文件 - 解析指定目录下的.go 文件
- 查找带有@provider注释的结构体 - 查找带有@provider 注释的结构体
- 支持 `@provider(grpc|event|job):[except|only] [returnType] [group]` 注释 - 支持 `@provider(grpc|event|job):[except|only] [returnType] [group]` 注释
- 自动生成provider文件 - 自动生成 provider 文件
- 支持分组生成 - 支持分组生成
#### gen route #### gen route
- 描述:生成路由 - 描述:生成路由
- 参数: - 参数:
- path可选指定生成路径默认当前目录 - path可选指定生成路径默认当前目录
- 功能: - 功能:
- 解析app/http目录下的controller文件 - 解析 app/http 目录下的 controller 文件
- 自动生成路由定义 - 自动生成路由定义
- 支持分组生成路由 - 支持分组生成路由
- 生成完成后自动执行gen provider命令 - 生成完成后自动执行 gen provider 命令
### 数据库命令 ### 数据库命令
#### migrate (别名m) #### migrate (别名m)
- 描述:数据库迁移 - 描述:数据库迁移
- 参数: - 参数:
- action必选迁移操作up|up-by-one|up-to|create|down|down-to|fix|redo|reset|status|version - action必选迁移操作up|up-by-one|up-to|create|down|down-to|fix|redo|reset|status|version
- args可选操作参数 - args可选操作参数
- 选项: - 选项:
- -c/--config指定数据库配置文件默认config.toml - -c/--config指定数据库配置文件默认 config.toml
- 功能: - 功能:
- 执行数据库迁移 - 执行数据库迁移
- 支持创建迁移文件 - 支持创建迁移文件
@@ -59,9 +63,10 @@ go install go.ipao.vip/atomctl@latest
### 新建命令 ### 新建命令
#### new project (别名p) #### new project (别名p)
- 描述:创建新项目 - 描述:创建新项目
- 参数: - 参数:
- moduleName必选项目模块名需符合Go包名规范 - moduleName必选项目模块名需符合 Go 包名规范)
- 选项: - 选项:
- --force强制覆盖已存在项目 - --force强制覆盖已存在项目
- 功能: - 功能:
@@ -71,11 +76,12 @@ go install go.ipao.vip/atomctl@latest
- 生成完成后提示后续步骤 - 生成完成后提示后续步骤
#### new provider #### new provider
- 描述创建新的provider
- 描述:创建新的 provider
- 参数: - 参数:
- providerName必选provider名称 - providerName必选provider 名称
- 功能: - 功能:
- 在providers目录下创建新的provider - providers 目录下创建新的 provider
- 自动生成provider模板文件 - 自动生成 provider 模板文件
- 支持模板渲染 - 支持模板渲染
- 自动处理命名转换(如驼峰命名) - 自动处理命名转换(如驼峰命名)

View File

@@ -10,8 +10,8 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/pkg/utils" "go.ipao.vip/atomctl/v2/pkg/utils"
"go.ipao.vip/atomctl/pkg/utils/generator" "go.ipao.vip/atomctl/v2/pkg/utils/generator"
) )
func CommandGenEnum(root *cobra.Command) { func CommandGenEnum(root *cobra.Command) {

View File

@@ -6,7 +6,7 @@ import (
"regexp" "regexp"
"strings" "strings"
astModel "go.ipao.vip/atomctl/pkg/ast/model" astModel "go.ipao.vip/atomctl/v2/pkg/ast/model"
"github.com/go-jet/jet/v2/generator/metadata" "github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/postgres" "github.com/go-jet/jet/v2/generator/postgres"
@@ -19,22 +19,22 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.ipao.vip/atomctl/pkg/ast/model" "go.ipao.vip/atomctl/v2/pkg/ast/model"
pgDatabase "go.ipao.vip/atomctl/pkg/postgres" pgDatabase "go.ipao.vip/atomctl/v2/pkg/postgres"
"go.ipao.vip/atomctl/pkg/utils/gomod" "go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
func CommandGenModel(root *cobra.Command) { func CommandGenModel(root *cobra.Command) {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "model", Use: "model",
Aliases: []string{"m"}, Aliases: []string{"m"},
Short: "Generate jet models", Short: "Generate jet models",
RunE: commandGenModelE, RunE: commandGenModelE,
} }
cmd.Flags().String("schema", "", "Override database schema") cmd.Flags().String("schema", "", "Override database schema")
cmd.Flags().Bool("rename-schemas", true, "Rename generated database/<db> to database/schemas") cmd.Flags().Bool("rename-schemas", true, "Rename generated database/<db> to database/schemas")
cmd.Flags().String("schemas-out", "database/schemas", "Schemas output directory when renaming") cmd.Flags().String("schemas-out", "database/schemas", "Schemas output directory when renaming")
root.AddCommand(cmd) root.AddCommand(cmd)
} }
@@ -44,15 +44,15 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "parse go.mod") return errors.Wrap(err, "parse go.mod")
} }
_, dbConf, err := pgDatabase.GetDB(cmd.Flag("config").Value.String()) _, dbConf, err := pgDatabase.GetDB(cmd.Flag("config").Value.String())
if err != nil { if err != nil {
return errors.Wrap(err, "get db") return errors.Wrap(err, "get db")
} }
// optional schema override // optional schema override
if s := cmd.Flag("schema").Value.String(); s != "" { if s := cmd.Flag("schema").Value.String(); s != "" {
dbConf.Schema = s dbConf.Schema = s
} }
v := viper.New() v := viper.New()
v.SetConfigType("yaml") v.SetConfigType("yaml")
@@ -175,16 +175,16 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return err return err
} }
if rename, _ := cmd.Flags().GetBool("rename-schemas"); rename { if rename, _ := cmd.Flags().GetBool("rename-schemas"); rename {
out := cmd.Flag("schemas-out").Value.String() out := cmd.Flag("schemas-out").Value.String()
if err := os.RemoveAll(out); err != nil { if err := os.RemoveAll(out); err != nil {
return err return err
} }
dataPath := fmt.Sprintf("database/%s", cfg.Database) dataPath := fmt.Sprintf("database/%s", cfg.Database)
if err := os.Rename(dataPath, out); err != nil { if err := os.Rename(dataPath, out); err != nil {
return err return err
} }
} }
if err := astModel.Generate(generatedTables, transformer); err != nil { if err := astModel.Generate(generatedTables, transformer); err != nil {
return err return err

View File

@@ -6,11 +6,11 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"go.ipao.vip/atomctl/pkg/ast/provider"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/pkg/ast/provider"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
func CommandGenProvider(root *cobra.Command) { func CommandGenProvider(root *cobra.Command) {

View File

@@ -10,40 +10,40 @@ import (
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/pkg/ast/route" "go.ipao.vip/atomctl/v2/pkg/ast/route"
"go.ipao.vip/atomctl/pkg/utils/gomod" "go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
func CommandGenRoute(root *cobra.Command) { func CommandGenRoute(root *cobra.Command) {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "route", Use: "route",
Short: "generate routes", Short: "generate routes",
RunE: commandGenRouteE, RunE: commandGenRouteE,
PostRunE: commandGenProviderE, PostRunE: commandGenProviderE,
} }
cmd.Flags().String("path", ".", "Base path to scan (defaults to CWD)") cmd.Flags().String("path", ".", "Base path to scan (defaults to CWD)")
root.AddCommand(cmd) root.AddCommand(cmd)
} }
// https://go.ipao.vip/atomctl/pkg/swag?tab=readme-ov-file#api-operation // https://go.ipao.vip/atomctl/v2/pkg/swag?tab=readme-ov-file#api-operation
func commandGenRouteE(cmd *cobra.Command, args []string) error { func commandGenRouteE(cmd *cobra.Command, args []string) error {
var err error var err error
var path string var path string
if len(args) > 0 { if len(args) > 0 {
path = args[0] path = args[0]
} else { } else {
path, err = os.Getwd() path, err = os.Getwd()
if err != nil { if err != nil {
return err return err
} }
} }
// allow overriding via --path flag // allow overriding via --path flag
if f := cmd.Flag("path"); f != nil && f.Value.String() != "." && f.Value.String() != "" { if f := cmd.Flag("path"); f != nil && f.Value.String() != "." && f.Value.String() != "" {
path = f.Value.String() path = f.Value.String()
} }
path, _ = filepath.Abs(path) path, _ = filepath.Abs(path)

View File

@@ -3,24 +3,24 @@ package cmd
import ( import (
"context" "context"
"go.ipao.vip/atomctl/pkg/postgres"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/pressly/goose/v3" "github.com/pressly/goose/v3"
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/pkg/postgres"
) )
// migrate // migrate
func CommandMigrate(root *cobra.Command) { func CommandMigrate(root *cobra.Command) {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "migrate [up|up-by-one|up-to|create|down|down-to|fix|redo|reset|status|version]", Use: "migrate [up|up-by-one|up-to|create|down|down-to|fix|redo|reset|status|version]",
Aliases: []string{"m"}, Aliases: []string{"m"},
RunE: commandMigrate, RunE: commandMigrate,
} }
cmd.Flags().StringP("config", "c", "config.toml", "database config file") cmd.Flags().StringP("config", "c", "config.toml", "database config file")
cmd.Flags().String("dir", "database/migrations", "migrations directory") cmd.Flags().String("dir", "database/migrations", "migrations directory")
cmd.Flags().String("table", "migrations", "migrations table name") cmd.Flags().String("table", "migrations", "migrations table name")
root.AddCommand(cmd) root.AddCommand(cmd)
} }
@@ -41,13 +41,13 @@ func commandMigrate(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "get db") return errors.Wrap(err, "get db")
} }
action, args := args[0], args[1:] action, args := args[0], args[1:]
log.Infof("migration action: %s args: %+v", action, args) log.Infof("migration action: %s args: %+v", action, args)
dir := cmd.Flag("dir").Value.String() dir := cmd.Flag("dir").Value.String()
table := cmd.Flag("table").Value.String() table := cmd.Flag("table").Value.String()
goose.SetTableName(table) goose.SetTableName(table)
return goose.RunContext(context.Background(), action, db, dir, args...) return goose.RunContext(context.Background(), action, db, dir, args...)
} }

View File

@@ -1,45 +1,45 @@
package cmd package cmd
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"text/template" "text/template"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"go.ipao.vip/atomctl/templates"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"go.ipao.vip/atomctl/v2/templates"
) )
// CommandNewProvider 注册 new_provider 命令 // CommandNewProvider 注册 new_provider 命令
func CommandNewEvent(root *cobra.Command) { func CommandNewEvent(root *cobra.Command) {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "event", Use: "event",
Aliases: []string{"e"}, Aliases: []string{"e"},
Short: "创建新的 event publish & subscriber", Short: "创建新的 event publish & subscriber",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: commandNewEventE, RunE: commandNewEventE,
} }
cmd.Flags().String("only", "", "仅生成: publisher 或 subscriber") cmd.Flags().String("only", "", "仅生成: publisher 或 subscriber")
root.AddCommand(cmd) root.AddCommand(cmd)
} }
func commandNewEventE(cmd *cobra.Command, args []string) error { func commandNewEventE(cmd *cobra.Command, args []string) error {
snakeName := lo.SnakeCase(args[0]) snakeName := lo.SnakeCase(args[0])
camelName := lo.PascalCase(args[0]) camelName := lo.PascalCase(args[0])
// shared flags // shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run") dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir") baseDir, _ := cmd.Flags().GetString("dir")
only, _ := cmd.Flags().GetString("only") only, _ := cmd.Flags().GetString("only")
publisherPath := filepath.Join(baseDir, "app/events/publishers") publisherPath := filepath.Join(baseDir, "app/events/publishers")
subscriberPath := filepath.Join(baseDir, "app/events/subscribers") subscriberPath := filepath.Join(baseDir, "app/events/subscribers")
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
@@ -52,17 +52,17 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
return err return err
} }
if dryRun { if dryRun {
fmt.Printf("[dry-run] mkdir -p %s\n", publisherPath) fmt.Printf("[dry-run] mkdir -p %s\n", publisherPath)
fmt.Printf("[dry-run] mkdir -p %s\n", subscriberPath) fmt.Printf("[dry-run] mkdir -p %s\n", subscriberPath)
} else { } else {
if err := os.MkdirAll(publisherPath, os.ModePerm); err != nil { if err := os.MkdirAll(publisherPath, os.ModePerm); err != nil {
return err return err
} }
if err := os.MkdirAll(subscriberPath, os.ModePerm); err != nil { if err := os.MkdirAll(subscriberPath, os.ModePerm); err != nil {
return err return err
} }
} }
err = fs.WalkDir(templates.Events, "events", func(path string, d fs.DirEntry, err error) error { err = fs.WalkDir(templates.Events, "events", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
@@ -77,59 +77,73 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
return err return err
} }
var destPath string var destPath string
if relPath == "publisher.go.tpl" { if relPath == "publisher.go.tpl" {
if only == "subscriber" { return nil } if only == "subscriber" {
destPath = filepath.Join(publisherPath, snakeName+".go") return nil
} else if relPath == "subscriber.go.tpl" { }
if only == "publisher" { return nil } destPath = filepath.Join(publisherPath, snakeName+".go")
destPath = filepath.Join(subscriberPath, snakeName+".go") } else if relPath == "subscriber.go.tpl" {
} else { return nil } if only == "publisher" {
return nil
}
destPath = filepath.Join(subscriberPath, snakeName+".go")
} else {
return nil
}
tmpl, err := template.ParseFS(templates.Events, path) tmpl, err := template.ParseFS(templates.Events, path)
if err != nil { if err != nil {
return err return err
} }
if dryRun { if dryRun {
fmt.Printf("[dry-run] render > %s\n", destPath) fmt.Printf("[dry-run] render > %s\n", destPath)
return nil return nil
} }
destFile, err := os.Create(destPath) destFile, err := os.Create(destPath)
if err != nil { if err != nil {
return err return err
} }
defer destFile.Close() defer destFile.Close()
return tmpl.Execute(destFile, map[string]string{ return tmpl.Execute(destFile, map[string]string{
"Name": camelName, "Name": camelName,
"ModuleName": gomod.GetModuleName(), "ModuleName": gomod.GetModuleName(),
}) })
}) })
// 写入或追加 topic 常量,避免重复。 // 写入或追加 topic 常量,避免重复。
topicsPath := filepath.Join(baseDir, "app/events/topics.go") topicsPath := filepath.Join(baseDir, "app/events/topics.go")
topicLine := fmt.Sprintf("const Topic%s = %q\n", camelName, snakeName) topicLine := fmt.Sprintf("const Topic%s = %q\n", camelName, snakeName)
if dryRun { if dryRun {
fmt.Printf("[dry-run] ensure topics file and add constant > %s\n", topicsPath) fmt.Printf("[dry-run] ensure topics file and add constant > %s\n", topicsPath)
} else { } else {
// ensure file exists with basic header // ensure file exists with basic header
if _, statErr := os.Stat(topicsPath); os.IsNotExist(statErr) { if _, statErr := os.Stat(topicsPath); os.IsNotExist(statErr) {
if err := os.MkdirAll(filepath.Dir(topicsPath), os.ModePerm); err != nil { return err } if err := os.MkdirAll(filepath.Dir(topicsPath), os.ModePerm); err != nil {
header := "package events\n\n// topics generated by atomctl\n\n" return err
if err := os.WriteFile(topicsPath, []byte(header), 0o644); err != nil { return err } }
} header := "package events\n\n// topics generated by atomctl\n\n"
// check duplicate if err := os.WriteFile(topicsPath, []byte(header), 0o644); err != nil {
content, _ := os.ReadFile(topicsPath) return err
if !strings.Contains(string(content), "Topic"+camelName+" ") && !strings.Contains(string(content), topicLine) { }
f, err := os.OpenFile(topicsPath, os.O_APPEND|os.O_WRONLY, 0o644) }
if err != nil { return err } // check duplicate
defer f.Close() content, _ := os.ReadFile(topicsPath)
if _, err := f.WriteString(topicLine); err != nil { return err } if !strings.Contains(string(content), "Topic"+camelName+" ") && !strings.Contains(string(content), topicLine) {
} f, err := os.OpenFile(topicsPath, os.O_APPEND|os.O_WRONLY, 0o644)
} if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(topicLine); err != nil {
return err
}
}
}
fmt.Printf("event 已创建: %s\n", snakeName) fmt.Printf("event 已创建: %s\n", snakeName)
return nil return nil

View File

@@ -7,10 +7,10 @@ import (
"path/filepath" "path/filepath"
"text/template" "text/template"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"go.ipao.vip/atomctl/templates"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"go.ipao.vip/atomctl/v2/templates"
) )
// CommandNewProvider 注册 new_provider 命令 // CommandNewProvider 注册 new_provider 命令
@@ -26,14 +26,14 @@ func CommandNewJob(root *cobra.Command) {
} }
func commandNewJobE(cmd *cobra.Command, args []string) error { func commandNewJobE(cmd *cobra.Command, args []string) error {
snakeName := lo.SnakeCase(args[0]) snakeName := lo.SnakeCase(args[0])
camelName := lo.PascalCase(args[0]) camelName := lo.PascalCase(args[0])
// shared flags // shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run") dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir") baseDir, _ := cmd.Flags().GetString("dir")
basePath := filepath.Join(baseDir, "app/jobs") basePath := filepath.Join(baseDir, "app/jobs")
path, err := os.Getwd() path, err := os.Getwd()
if err != nil { if err != nil {
@@ -46,13 +46,13 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
return err return err
} }
if dryRun { if dryRun {
fmt.Printf("[dry-run] mkdir -p %s\n", basePath) fmt.Printf("[dry-run] mkdir -p %s\n", basePath)
} else { } else {
if err := os.MkdirAll(basePath, os.ModePerm); err != nil { if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
return err return err
} }
} }
err = fs.WalkDir(templates.Jobs, "jobs", func(path string, d fs.DirEntry, err error) error { err = fs.WalkDir(templates.Jobs, "jobs", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
@@ -63,27 +63,27 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
return nil return nil
} }
filePath := filepath.Join(basePath, snakeName+".go") filePath := filepath.Join(basePath, snakeName+".go")
tmpl, err := template.ParseFS(templates.Jobs, path) tmpl, err := template.ParseFS(templates.Jobs, path)
if err != nil { if err != nil {
return err return err
} }
if dryRun { if dryRun {
fmt.Printf("[dry-run] render > %s\n", filePath) fmt.Printf("[dry-run] render > %s\n", filePath)
return nil return nil
} }
destFile, err := os.Create(filePath) destFile, err := os.Create(filePath)
if err != nil { if err != nil {
return err return err
} }
defer destFile.Close() defer destFile.Close()
return tmpl.Execute(destFile, map[string]string{ return tmpl.Execute(destFile, map[string]string{
"Name": camelName, "Name": camelName,
"ModuleName": gomod.GetModuleName(), "ModuleName": gomod.GetModuleName(),
}) })
}) })
if err != nil { if err != nil {
return err return err

View File

@@ -9,11 +9,11 @@ import (
"strings" "strings"
"text/template" "text/template"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"go.ipao.vip/atomctl/templates"
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"go.ipao.vip/atomctl/v2/templates"
) )
func CommandNewModule(root *cobra.Command) { func CommandNewModule(root *cobra.Command) {

View File

@@ -13,8 +13,8 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/pkg/utils/gomod" "go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"go.ipao.vip/atomctl/templates" "go.ipao.vip/atomctl/v2/templates"
) )
// 验证包名是否合法:支持域名、路径分隔符和常见字符 // 验证包名是否合法:支持域名、路径分隔符和常见字符
@@ -36,14 +36,14 @@ func CommandNewProject(root *cobra.Command) {
} }
func commandNewProjectE(cmd *cobra.Command, args []string) error { func commandNewProjectE(cmd *cobra.Command, args []string) error {
var ( var (
moduleName string moduleName string
inPlace bool inPlace bool
) )
// shared flags // shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run") dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir") baseDir, _ := cmd.Flags().GetString("dir")
if len(args) == 0 { if len(args) == 0 {
if _, err := os.Stat("go.mod"); err == nil { if _, err := os.Stat("go.mod"); err == nil {
@@ -76,27 +76,27 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
force, _ := cmd.Flags().GetBool("force") force, _ := cmd.Flags().GetBool("force")
rootDir := "." rootDir := "."
if !inPlace { if !inPlace {
// honor base dir when creating a new project // honor base dir when creating a new project
rootDir = filepath.Join(baseDir, projectInfo.ProjectName) rootDir = filepath.Join(baseDir, projectInfo.ProjectName)
if _, err := os.Stat(rootDir); err == nil { if _, err := os.Stat(rootDir); err == nil {
if !force { if !force {
return fmt.Errorf("project directory %s already exists", rootDir) return fmt.Errorf("project directory %s already exists", rootDir)
} }
log.Warnf("强制删除已存在的目录: %s", rootDir) log.Warnf("强制删除已存在的目录: %s", rootDir)
if dryRun { if dryRun {
log.Infof("[dry-run] 将删除目录: %s", rootDir) log.Infof("[dry-run] 将删除目录: %s", rootDir)
} else if err := os.RemoveAll(rootDir); err != nil { } else if err := os.RemoveAll(rootDir); err != nil {
return fmt.Errorf("failed to remove existing directory: %v", err) return fmt.Errorf("failed to remove existing directory: %v", err)
} }
} }
if dryRun { if dryRun {
log.Infof("[dry-run] 将创建目录: %s", rootDir) log.Infof("[dry-run] 将创建目录: %s", rootDir)
} else if err := os.MkdirAll(rootDir, 0o755); err != nil { } else if err := os.MkdirAll(rootDir, 0o755); err != nil {
return fmt.Errorf("failed to create project directory: %v", err) return fmt.Errorf("failed to create project directory: %v", err)
} }
} }
if err := fs.WalkDir(templates.Project, "project", func(path string, d fs.DirEntry, err error) error { if err := fs.WalkDir(templates.Project, "project", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
@@ -115,14 +115,14 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
} }
targetPath := filepath.Join(rootDir, relPath) targetPath := filepath.Join(rootDir, relPath)
if d.IsDir() { if d.IsDir() {
log.Infof("创建目录: %s", targetPath) log.Infof("创建目录: %s", targetPath)
if dryRun { if dryRun {
log.Infof("[dry-run] mkdir -p %s", targetPath) log.Infof("[dry-run] mkdir -p %s", targetPath)
return nil return nil
} }
return os.MkdirAll(targetPath, 0o755) return os.MkdirAll(targetPath, 0o755)
} }
content, err := templates.Project.ReadFile(path) content, err := templates.Project.ReadFile(path)
if err != nil { if err != nil {
@@ -164,28 +164,28 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
log.Infof("[渲染] 文件: %s", targetPath) log.Infof("[渲染] 文件: %s", targetPath)
if dryRun { if dryRun {
log.Infof("[dry-run] render > %s", targetPath) log.Infof("[dry-run] render > %s", targetPath)
return nil return nil
} }
f, err := os.Create(targetPath) f, err := os.Create(targetPath)
if err != nil { if err != nil {
return errors.Wrapf(err, "创建文件失败 %s", targetPath) return errors.Wrapf(err, "创建文件失败 %s", targetPath)
} }
defer f.Close() defer f.Close()
return tmpl.Execute(f, projectInfo) return tmpl.Execute(f, projectInfo)
} }
log.Infof("[复制] 文件: %s", targetPath) log.Infof("[复制] 文件: %s", targetPath)
if dryRun { if dryRun {
log.Infof("[dry-run] write > %s", targetPath) log.Infof("[dry-run] write > %s", targetPath)
return nil return nil
} }
return os.WriteFile(targetPath, content, 0o644) return os.WriteFile(targetPath, content, 0o644)
}); err != nil { }); err != nil {
return err return err
} }
if inPlace { if inPlace {
log.Info("🎉 项目初始化成功 (当前目录)!") log.Info("🎉 项目初始化成功 (当前目录)!")

View File

@@ -9,9 +9,9 @@ import (
"strings" "strings"
"text/template" "text/template"
"go.ipao.vip/atomctl/templates"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/templates"
) )
// CommandNewProvider 注册 new_provider 命令 // CommandNewProvider 注册 new_provider 命令
@@ -27,24 +27,24 @@ func CommandNewProvider(root *cobra.Command) {
} }
func commandNewProviderE(cmd *cobra.Command, args []string) error { func commandNewProviderE(cmd *cobra.Command, args []string) error {
providerName := args[0] providerName := args[0]
// shared flags // shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run") dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir") baseDir, _ := cmd.Flags().GetString("dir")
targetPath := filepath.Join(baseDir, "providers", providerName) targetPath := filepath.Join(baseDir, "providers", providerName)
if _, err := os.Stat(targetPath); err == nil { if _, err := os.Stat(targetPath); err == nil {
return fmt.Errorf("目录 %s 已存在", targetPath) return fmt.Errorf("目录 %s 已存在", targetPath)
} }
if dryRun { if dryRun {
fmt.Printf("[dry-run] mkdir -p %s\n", targetPath) fmt.Printf("[dry-run] mkdir -p %s\n", targetPath)
} else { } else {
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil { if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
return err return err
} }
} }
err := fs.WalkDir(templates.Provider, "provider", func(path string, d fs.DirEntry, err error) error { err := fs.WalkDir(templates.Provider, "provider", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
@@ -59,35 +59,35 @@ func commandNewProviderE(cmd *cobra.Command, args []string) error {
return err return err
} }
destPath := filepath.Join(targetPath, strings.TrimSuffix(relPath, ".tpl")) destPath := filepath.Join(targetPath, strings.TrimSuffix(relPath, ".tpl"))
if dryRun { if dryRun {
fmt.Printf("[dry-run] mkdir -p %s\n", filepath.Dir(destPath)) fmt.Printf("[dry-run] mkdir -p %s\n", filepath.Dir(destPath))
} else { } else {
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil { if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
return err return err
} }
} }
tmpl, err := template.ParseFS(templates.Provider, path) tmpl, err := template.ParseFS(templates.Provider, path)
if err != nil { if err != nil {
return err return err
} }
if dryRun { if dryRun {
fmt.Printf("[dry-run] render > %s\n", destPath) fmt.Printf("[dry-run] render > %s\n", destPath)
return nil return nil
} }
destFile, err := os.Create(destPath) destFile, err := os.Create(destPath)
if err != nil { if err != nil {
return err return err
} }
defer destFile.Close() defer destFile.Close()
return tmpl.Execute(destFile, map[string]string{ return tmpl.Execute(destFile, map[string]string{
"Name": providerName, "Name": providerName,
"CamelName": strcase.ToCamel(providerName), "CamelName": strcase.ToCamel(providerName),
}) })
}) })
if err != nil { if err != nil {
return errors.New("渲染 provider 模板失败") return errors.New("渲染 provider 模板失败")

2
go.mod
View File

@@ -1,4 +1,4 @@
module go.ipao.vip/atomctl module go.ipao.vip/atomctl/v2
go 1.23.2 go 1.23.2

View File

@@ -1,9 +1,9 @@
package main package main
import ( import (
"go.ipao.vip/atomctl/cmd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/v2/cmd"
) )
func main() { func main() {

View File

@@ -10,7 +10,7 @@ import (
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"go.ipao.vip/atomctl/pkg/utils/gomod" "go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
//go:embed table.go.tpl //go:embed table.go.tpl

View File

@@ -11,7 +11,7 @@ import (
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"go.ipao.vip/atomctl/pkg/utils/gomod" "go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
func getTypePkgName(typ string) string { func getTypePkgName(typ string) string {

View File

@@ -6,8 +6,8 @@ import (
"os" "os"
"strings" "strings"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"github.com/samber/lo" "github.com/samber/lo"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"golang.org/x/tools/imports" "golang.org/x/tools/imports"
) )

View File

@@ -8,10 +8,10 @@ import (
"path/filepath" "path/filepath"
"text/template" "text/template"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
"github.com/samber/lo" "github.com/samber/lo"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
//go:embed router.go.tpl //go:embed router.go.tpl

View File

@@ -7,10 +7,10 @@ import (
"go/token" "go/token"
"strings" "strings"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
) )
type RouteDefinition struct { type RouteDefinition struct {