feat: 增加命令行工具的干运行模式和输出目录选项

This commit is contained in:
Rogee
2025-09-10 14:30:16 +08:00
parent a714d4a3a9
commit 3b804b83da
5 changed files with 221 additions and 139 deletions

View File

@@ -1,11 +1,12 @@
package cmd
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"text/template"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"go.ipao.vip/atomctl/templates"
@@ -15,23 +16,30 @@ import (
// CommandNewProvider 注册 new_provider 命令
func CommandNewEvent(root *cobra.Command) {
cmd := &cobra.Command{
Use: "event",
Aliases: []string{"e"},
Short: "创建新的 event publish & subscriber",
Args: cobra.ExactArgs(1),
RunE: commandNewEventE,
}
cmd := &cobra.Command{
Use: "event",
Aliases: []string{"e"},
Short: "创建新的 event publish & subscriber",
Args: cobra.ExactArgs(1),
RunE: commandNewEventE,
}
root.AddCommand(cmd)
cmd.Flags().String("only", "", "仅生成: publisher 或 subscriber")
root.AddCommand(cmd)
}
func commandNewEventE(cmd *cobra.Command, args []string) error {
snakeName := lo.SnakeCase(args[0])
camelName := lo.PascalCase(args[0])
publisherPath := "app/events/publishers"
subscriberPath := "app/events/subscribers"
// shared flags
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()
if err != nil {
@@ -44,13 +52,17 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
return err
}
if err := os.MkdirAll(publisherPath, os.ModePerm); err != nil {
return err
}
if err := os.MkdirAll(subscriberPath, os.ModePerm); err != nil {
return err
}
if dryRun {
fmt.Printf("[dry-run] mkdir -p %s\n", publisherPath)
fmt.Printf("[dry-run] mkdir -p %s\n", subscriberPath)
} else {
if err := os.MkdirAll(publisherPath, os.ModePerm); err != nil {
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 {
if err != nil {
@@ -65,42 +77,59 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
return err
}
var destPath string
if relPath == "publisher.go.tpl" {
destPath = filepath.Join(publisherPath, snakeName+".go")
} else if relPath == "subscriber.go.tpl" {
destPath = filepath.Join(subscriberPath, snakeName+".go")
}
var destPath string
if relPath == "publisher.go.tpl" {
if only == "subscriber" { return nil }
destPath = filepath.Join(publisherPath, 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)
if err != nil {
return err
}
destFile, err := os.Create(destPath)
if err != nil {
return err
}
defer destFile.Close()
if dryRun {
fmt.Printf("[dry-run] render > %s\n", destPath)
return nil
}
return tmpl.Execute(destFile, map[string]string{
"Name": camelName,
"ModuleName": gomod.GetModuleName(),
})
})
destFile, err := os.Create(destPath)
if err != nil {
return err
}
defer destFile.Close()
topicStr := fmt.Sprintf("const Topic%s = %q\n", camelName, snakeName)
// 写入到 app/events/topic.go
topicFile, err := os.OpenFile("app/events/topics.go", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer topicFile.Close()
return tmpl.Execute(destFile, map[string]string{
"Name": camelName,
"ModuleName": gomod.GetModuleName(),
})
})
_, err = topicFile.WriteString(topicStr)
if err != nil {
return err
}
// 写入或追加 topic 常量,避免重复。
topicsPath := filepath.Join(baseDir, "app/events/topics.go")
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)
return nil