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

@@ -10,7 +10,9 @@ func CommandInit(root *cobra.Command) {
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){
CommandNewProject,

View File

@@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"go.ipao.vip/atomctl/pkg/utils/gomod"
@@ -23,6 +24,8 @@ func CommandNewEvent(root *cobra.Command) {
RunE: commandNewEventE,
}
cmd.Flags().String("only", "", "仅生成: publisher 或 subscriber")
root.AddCommand(cmd)
}
@@ -30,8 +33,13 @@ 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 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 {
@@ -67,16 +79,23 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
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
}
if dryRun {
fmt.Printf("[dry-run] render > %s\n", destPath)
return nil
}
destFile, err := os.Create(destPath)
if err != nil {
return err
@@ -89,17 +108,27 @@ func commandNewEventE(cmd *cobra.Command, args []string) error {
})
})
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()
// 写入或追加 topic 常量,避免重复。
topicsPath := filepath.Join(baseDir, "app/events/topics.go")
topicLine := fmt.Sprintf("const Topic%s = %q\n", camelName, snakeName)
_, err = topicFile.WriteString(topicStr)
if err != nil {
return err
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)

View File

@@ -29,7 +29,11 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
snakeName := lo.SnakeCase(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()
if err != nil {
@@ -42,9 +46,13 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
return err
}
if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
if dryRun {
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 {
if err != nil {
@@ -55,13 +63,18 @@ func commandNewJobE(cmd *cobra.Command, args []string) error {
return nil
}
destPath := filepath.Join(destPath, snakeName+".go")
filePath := filepath.Join(basePath, snakeName+".go")
tmpl, err := template.ParseFS(templates.Jobs, path)
if err != nil {
return err
}
destFile, err := os.Create(destPath)
if dryRun {
fmt.Printf("[dry-run] render > %s\n", filePath)
return nil
}
destFile, err := os.Create(filePath)
if err != nil {
return err
}

View File

@@ -41,6 +41,10 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
inPlace bool
)
// shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir")
if len(args) == 0 {
if _, err := os.Stat("go.mod"); err == nil {
pwd, _ := os.Getwd()
@@ -74,17 +78,22 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
rootDir := "."
if !inPlace {
rootDir = projectInfo.ProjectName
// honor base dir when creating a new project
rootDir = filepath.Join(baseDir, projectInfo.ProjectName)
if _, err := os.Stat(rootDir); err == nil {
if !force {
return fmt.Errorf("project directory %s already exists", rootDir)
}
log.Warnf("强制删除已存在的目录: %s", rootDir)
if err := os.RemoveAll(rootDir); err != nil {
if dryRun {
log.Infof("[dry-run] 将删除目录: %s", rootDir)
} else if err := os.RemoveAll(rootDir); err != nil {
return fmt.Errorf("failed to remove existing directory: %v", err)
}
}
if err := os.MkdirAll(rootDir, 0o755); err != nil {
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)
}
}
@@ -108,6 +117,10 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
targetPath := filepath.Join(rootDir, relPath)
if d.IsDir() {
log.Infof("创建目录: %s", targetPath)
if dryRun {
log.Infof("[dry-run] mkdir -p %s", targetPath)
return nil
}
return os.MkdirAll(targetPath, 0o755)
}
@@ -152,6 +165,10 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
return err
}
log.Infof("[渲染] 文件: %s", targetPath)
if dryRun {
log.Infof("[dry-run] render > %s", targetPath)
return nil
}
f, err := os.Create(targetPath)
if err != nil {
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
@@ -161,6 +178,10 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
}
log.Infof("[复制] 文件: %s", targetPath)
if dryRun {
log.Infof("[dry-run] write > %s", targetPath)
return nil
}
return os.WriteFile(targetPath, content, 0o644)
}); err != nil {
return err

View File

@@ -28,15 +28,23 @@ func CommandNewProvider(root *cobra.Command) {
func commandNewProviderE(cmd *cobra.Command, args []string) error {
providerName := args[0]
targetPath := filepath.Join("providers", providerName)
// shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir")
targetPath := filepath.Join(baseDir, "providers", providerName)
if _, err := os.Stat(targetPath); err == nil {
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 {
if err != nil {
@@ -52,8 +60,12 @@ func commandNewProviderE(cmd *cobra.Command, args []string) error {
}
destPath := filepath.Join(targetPath, strings.TrimSuffix(relPath, ".tpl"))
if dryRun {
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)
@@ -61,6 +73,11 @@ func commandNewProviderE(cmd *cobra.Command, args []string) error {
return err
}
if dryRun {
fmt.Printf("[dry-run] render > %s\n", destPath)
return nil
}
destFile, err := os.Create(destPath)
if err != nil {
return err