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

@@ -13,8 +13,8 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"go.ipao.vip/atomctl/templates"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"go.ipao.vip/atomctl/v2/templates"
)
// 验证包名是否合法:支持域名、路径分隔符和常见字符
@@ -36,14 +36,14 @@ func CommandNewProject(root *cobra.Command) {
}
func commandNewProjectE(cmd *cobra.Command, args []string) error {
var (
moduleName string
inPlace bool
)
var (
moduleName string
inPlace bool
)
// shared flags
dryRun, _ := cmd.Flags().GetBool("dry-run")
baseDir, _ := cmd.Flags().GetString("dir")
// 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 {
@@ -76,27 +76,27 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
force, _ := cmd.Flags().GetBool("force")
rootDir := "."
if !inPlace {
// 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 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 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)
}
}
rootDir := "."
if !inPlace {
// 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 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 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 != nil {
@@ -115,14 +115,14 @@ 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)
}
if d.IsDir() {
log.Infof("创建目录: %s", targetPath)
if dryRun {
log.Infof("[dry-run] mkdir -p %s", targetPath)
return nil
}
return os.MkdirAll(targetPath, 0o755)
}
content, err := templates.Project.ReadFile(path)
if err != nil {
@@ -164,28 +164,28 @@ func commandNewProjectE(cmd *cobra.Command, args []string) error {
if err != nil {
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)
}
defer f.Close()
return tmpl.Execute(f, projectInfo)
}
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)
}
defer f.Close()
return tmpl.Execute(f, projectInfo)
}
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
}
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
}
if inPlace {
log.Info("🎉 项目初始化成功 (当前目录)!")