feat: 增加命令行工具的干运行模式和输出目录选项
This commit is contained in:
26
cmd/new.go
26
cmd/new.go
@@ -5,20 +5,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CommandInit(root *cobra.Command) {
|
func CommandInit(root *cobra.Command) {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "new [project|module]",
|
Use: "new [project|module]",
|
||||||
Short: "new project/module",
|
Short: "new project/module",
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.PersistentFlags().BoolP("force", "f", false, "Force init project if exists")
|
cmd.PersistentFlags().BoolP("force", "f", false, "Force overwrite existing files or directories")
|
||||||
|
cmd.PersistentFlags().Bool("dry-run", false, "Preview actions without writing files")
|
||||||
|
cmd.PersistentFlags().String("dir", ".", "Base directory for outputs")
|
||||||
|
|
||||||
cmds := []func(*cobra.Command){
|
cmds := []func(*cobra.Command){
|
||||||
CommandNewProject,
|
CommandNewProject,
|
||||||
// deprecate CommandNewModule,
|
// deprecate CommandNewModule,
|
||||||
CommandNewProvider,
|
CommandNewProvider,
|
||||||
CommandNewEvent,
|
CommandNewEvent,
|
||||||
CommandNewJob,
|
CommandNewJob,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cmds {
|
for _, c := range cmds {
|
||||||
c(cmd)
|
c(cmd)
|
||||||
|
|||||||
127
cmd/new_event.go
127
cmd/new_event.go
@@ -1,11 +1,12 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"text/template"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
||||||
"go.ipao.vip/atomctl/templates"
|
"go.ipao.vip/atomctl/templates"
|
||||||
@@ -15,23 +16,30 @@ import (
|
|||||||
|
|
||||||
// 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,
|
||||||
}
|
}
|
||||||
|
|
||||||
root.AddCommand(cmd)
|
cmd.Flags().String("only", "", "仅生成: publisher 或 subscriber")
|
||||||
|
|
||||||
|
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])
|
||||||
|
|
||||||
publisherPath := "app/events/publishers"
|
// shared flags
|
||||||
subscriberPath := "app/events/subscribers"
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||||
|
baseDir, _ := cmd.Flags().GetString("dir")
|
||||||
|
only, _ := cmd.Flags().GetString("only")
|
||||||
|
|
||||||
|
publisherPath := filepath.Join(baseDir, "app/events/publishers")
|
||||||
|
subscriberPath := filepath.Join(baseDir, "app/events/subscribers")
|
||||||
|
|
||||||
path, err := os.Getwd()
|
path, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -44,13 +52,17 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(publisherPath, os.ModePerm); err != nil {
|
if dryRun {
|
||||||
return err
|
fmt.Printf("[dry-run] mkdir -p %s\n", publisherPath)
|
||||||
}
|
fmt.Printf("[dry-run] mkdir -p %s\n", subscriberPath)
|
||||||
|
} else {
|
||||||
if err := os.MkdirAll(subscriberPath, 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 {
|
||||||
|
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 {
|
||||||
@@ -65,42 +77,59 @@ 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" {
|
||||||
destPath = filepath.Join(publisherPath, snakeName+".go")
|
if only == "subscriber" { return nil }
|
||||||
} else if relPath == "subscriber.go.tpl" {
|
destPath = filepath.Join(publisherPath, snakeName+".go")
|
||||||
destPath = filepath.Join(subscriberPath, snakeName+".go")
|
} else if relPath == "subscriber.go.tpl" {
|
||||||
}
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
destFile, err := os.Create(destPath)
|
if dryRun {
|
||||||
if err != nil {
|
fmt.Printf("[dry-run] render > %s\n", destPath)
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
defer destFile.Close()
|
|
||||||
|
|
||||||
return tmpl.Execute(destFile, map[string]string{
|
destFile, err := os.Create(destPath)
|
||||||
"Name": camelName,
|
if err != nil {
|
||||||
"ModuleName": gomod.GetModuleName(),
|
return err
|
||||||
})
|
}
|
||||||
})
|
defer destFile.Close()
|
||||||
|
|
||||||
topicStr := fmt.Sprintf("const Topic%s = %q\n", camelName, snakeName)
|
return tmpl.Execute(destFile, map[string]string{
|
||||||
// 写入到 app/events/topic.go
|
"Name": camelName,
|
||||||
topicFile, err := os.OpenFile("app/events/topics.go", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
"ModuleName": gomod.GetModuleName(),
|
||||||
if err != nil {
|
})
|
||||||
return err
|
})
|
||||||
}
|
|
||||||
defer topicFile.Close()
|
|
||||||
|
|
||||||
_, err = topicFile.WriteString(topicStr)
|
// 写入或追加 topic 常量,避免重复。
|
||||||
if err != nil {
|
topicsPath := filepath.Join(baseDir, "app/events/topics.go")
|
||||||
return err
|
topicLine := fmt.Sprintf("const Topic%s = %q\n", camelName, snakeName)
|
||||||
}
|
|
||||||
|
if dryRun {
|
||||||
|
fmt.Printf("[dry-run] ensure topics file and add constant > %s\n", topicsPath)
|
||||||
|
} else {
|
||||||
|
// ensure file exists with basic header
|
||||||
|
if _, statErr := os.Stat(topicsPath); os.IsNotExist(statErr) {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(topicsPath), os.ModePerm); err != nil { return err }
|
||||||
|
header := "package events\n\n// topics generated by atomctl\n\n"
|
||||||
|
if err := os.WriteFile(topicsPath, []byte(header), 0o644); err != nil { return err }
|
||||||
|
}
|
||||||
|
// check duplicate
|
||||||
|
content, _ := os.ReadFile(topicsPath)
|
||||||
|
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
|
||||||
|
|||||||
@@ -26,10 +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])
|
||||||
|
|
||||||
destPath := "app/jobs"
|
// shared flags
|
||||||
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||||
|
baseDir, _ := cmd.Flags().GetString("dir")
|
||||||
|
|
||||||
|
basePath := filepath.Join(baseDir, "app/jobs")
|
||||||
|
|
||||||
path, err := os.Getwd()
|
path, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,9 +46,13 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
|
if dryRun {
|
||||||
return err
|
fmt.Printf("[dry-run] mkdir -p %s\n", basePath)
|
||||||
}
|
} else {
|
||||||
|
if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
|
||||||
|
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 {
|
||||||
@@ -55,22 +63,27 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
destPath := filepath.Join(destPath, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
destFile, err := os.Create(destPath)
|
if dryRun {
|
||||||
if err != nil {
|
fmt.Printf("[dry-run] render > %s\n", filePath)
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
defer destFile.Close()
|
|
||||||
|
|
||||||
return tmpl.Execute(destFile, map[string]string{
|
destFile, err := os.Create(filePath)
|
||||||
"Name": camelName,
|
if err != nil {
|
||||||
"ModuleName": gomod.GetModuleName(),
|
return err
|
||||||
})
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
return tmpl.Execute(destFile, map[string]string{
|
||||||
|
"Name": camelName,
|
||||||
|
"ModuleName": gomod.GetModuleName(),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -36,10 +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
|
||||||
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||||
|
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 {
|
||||||
@@ -72,22 +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 {
|
||||||
rootDir = projectInfo.ProjectName
|
// honor base dir when creating a new project
|
||||||
if _, err := os.Stat(rootDir); err == nil {
|
rootDir = filepath.Join(baseDir, projectInfo.ProjectName)
|
||||||
if !force {
|
if _, err := os.Stat(rootDir); err == nil {
|
||||||
return fmt.Errorf("project directory %s already exists", rootDir)
|
if !force {
|
||||||
}
|
return fmt.Errorf("project directory %s already exists", rootDir)
|
||||||
log.Warnf("强制删除已存在的目录: %s", rootDir)
|
}
|
||||||
if err := os.RemoveAll(rootDir); err != nil {
|
log.Warnf("强制删除已存在的目录: %s", rootDir)
|
||||||
return fmt.Errorf("failed to remove existing directory: %v", err)
|
if dryRun {
|
||||||
}
|
log.Infof("[dry-run] 将删除目录: %s", rootDir)
|
||||||
}
|
} else if err := os.RemoveAll(rootDir); err != nil {
|
||||||
if err := os.MkdirAll(rootDir, 0o755); err != nil {
|
return fmt.Errorf("failed to remove existing directory: %v", err)
|
||||||
return fmt.Errorf("failed to create project directory: %v", err)
|
}
|
||||||
}
|
}
|
||||||
}
|
if dryRun {
|
||||||
|
log.Infof("[dry-run] 将创建目录: %s", rootDir)
|
||||||
|
} else if err := os.MkdirAll(rootDir, 0o755); err != nil {
|
||||||
|
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 {
|
||||||
@@ -106,10 +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)
|
||||||
return os.MkdirAll(targetPath, 0o755)
|
if dryRun {
|
||||||
}
|
log.Infof("[dry-run] mkdir -p %s", targetPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return os.MkdirAll(targetPath, 0o755)
|
||||||
|
}
|
||||||
|
|
||||||
content, err := templates.Project.ReadFile(path)
|
content, err := templates.Project.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -151,20 +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)
|
||||||
f, err := os.Create(targetPath)
|
if dryRun {
|
||||||
if err != nil {
|
log.Infof("[dry-run] render > %s", targetPath)
|
||||||
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
|
return nil
|
||||||
}
|
}
|
||||||
defer f.Close()
|
f, err := os.Create(targetPath)
|
||||||
return tmpl.Execute(f, projectInfo)
|
if err != nil {
|
||||||
}
|
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
return tmpl.Execute(f, projectInfo)
|
||||||
|
}
|
||||||
|
|
||||||
log.Infof("[复制] 文件: %s", targetPath)
|
log.Infof("[复制] 文件: %s", targetPath)
|
||||||
return os.WriteFile(targetPath, content, 0o644)
|
if dryRun {
|
||||||
}); err != nil {
|
log.Infof("[dry-run] write > %s", targetPath)
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
return os.WriteFile(targetPath, content, 0o644)
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if inPlace {
|
if inPlace {
|
||||||
log.Info("🎉 项目初始化成功 (当前目录)!")
|
log.Info("🎉 项目初始化成功 (当前目录)!")
|
||||||
|
|||||||
@@ -27,16 +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]
|
||||||
targetPath := filepath.Join("providers", providerName)
|
// shared flags
|
||||||
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
||||||
|
baseDir, _ := cmd.Flags().GetString("dir")
|
||||||
|
|
||||||
if _, err := os.Stat(targetPath); err == nil {
|
targetPath := filepath.Join(baseDir, "providers", providerName)
|
||||||
return fmt.Errorf("目录 %s 已存在", targetPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
|
if _, err := os.Stat(targetPath); err == nil {
|
||||||
return err
|
return fmt.Errorf("目录 %s 已存在", targetPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dryRun {
|
||||||
|
fmt.Printf("[dry-run] mkdir -p %s\n", targetPath)
|
||||||
|
} else {
|
||||||
|
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
|
||||||
|
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 {
|
||||||
@@ -51,26 +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 err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
|
if dryRun {
|
||||||
return err
|
fmt.Printf("[dry-run] mkdir -p %s\n", filepath.Dir(destPath))
|
||||||
}
|
} else {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
destFile, err := os.Create(destPath)
|
if dryRun {
|
||||||
if err != nil {
|
fmt.Printf("[dry-run] render > %s\n", destPath)
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
defer destFile.Close()
|
|
||||||
|
|
||||||
return tmpl.Execute(destFile, map[string]string{
|
destFile, err := os.Create(destPath)
|
||||||
"Name": providerName,
|
if err != nil {
|
||||||
"CamelName": strcase.ToCamel(providerName),
|
return err
|
||||||
})
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
return tmpl.Execute(destFile, map[string]string{
|
||||||
|
"Name": providerName,
|
||||||
|
"CamelName": strcase.ToCamel(providerName),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("渲染 provider 模板失败")
|
return errors.New("渲染 provider 模板失败")
|
||||||
|
|||||||
Reference in New Issue
Block a user