feat: update .gitignore to exclude 'atomctl' binary and AGENTS.md

This commit is contained in:
Rogee
2025-09-10 14:04:42 +08:00
parent 3759295afa
commit 1fac55115d
2 changed files with 131 additions and 98 deletions

4
.gitignore vendored
View File

@@ -21,4 +21,6 @@
# Go workspace file # Go workspace file
go.work go.work
test/* tests/*
atomctl
AGENTS.md

View File

@@ -1,18 +1,19 @@
package cmd package cmd
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"text/template" "text/template"
"go.ipao.vip/atomctl/templates" "go.ipao.vip/atomctl/templates"
"github.com/pkg/errors" "go.ipao.vip/atomctl/pkg/utils/gomod"
log "github.com/sirupsen/logrus" "github.com/pkg/errors"
"github.com/spf13/cobra" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
) )
// 验证包名是否合法:支持域名、路径分隔符和常见字符 // 验证包名是否合法:支持域名、路径分隔符和常见字符
@@ -34,107 +35,137 @@ func CommandNewProject(root *cobra.Command) {
} }
func commandNewProjectE(cmd *cobra.Command, args []string) error { func commandNewProjectE(cmd *cobra.Command, args []string) error {
moduleName := args[0] var (
if !isValidGoPackageName(moduleName) { moduleName string
return fmt.Errorf("invalid module name: %s, should be a valid go package name", moduleName) inPlace bool
} )
log.Info("创建项目: ", moduleName) if len(args) == 0 {
if _, err := os.Stat("go.mod"); err == nil {
pwd, _ := os.Getwd()
if err := gomod.Parse(filepath.Join(pwd, "go.mod")); err != nil {
return fmt.Errorf("parse go.mod failed: %v", err)
}
moduleName = gomod.GetModuleName()
inPlace = true
} else {
return fmt.Errorf("module name required or run inside an existing module (go.mod)")
}
} else {
moduleName = args[0]
if !isValidGoPackageName(moduleName) {
return fmt.Errorf("invalid module name: %s, should be a valid go package name", moduleName)
}
}
var projectInfo struct { log.Info("创建项目: ", moduleName)
ModuleName string
ProjectName string
}
projectInfo.ModuleName = moduleName var projectInfo struct {
moduleSplitInfo := strings.Split(projectInfo.ModuleName, "/") ModuleName string
projectInfo.ProjectName = moduleSplitInfo[len(moduleSplitInfo)-1] ProjectName string
}
// 检查目录是否存在 projectInfo.ModuleName = moduleName
force, _ := cmd.Flags().GetBool("force") moduleSplitInfo := strings.Split(projectInfo.ModuleName, "/")
if _, err := os.Stat(projectInfo.ProjectName); err == nil { projectInfo.ProjectName = moduleSplitInfo[len(moduleSplitInfo)-1]
if !force {
return fmt.Errorf("project directory %s already exists", projectInfo.ProjectName)
}
log.Warnf("强制删除已存在的目录: %s", projectInfo.ProjectName)
if err := os.RemoveAll(projectInfo.ProjectName); err != nil {
return fmt.Errorf("failed to remove existing directory: %v", err)
}
}
// 创建项目根目录 force, _ := cmd.Flags().GetBool("force")
if err := os.MkdirAll(projectInfo.ProjectName, 0o755); err != nil {
return fmt.Errorf("failed to create project directory: %v", err)
}
// 遍历和处理模板文件 rootDir := "."
if err := fs.WalkDir(templates.Project, "project", func(path string, d fs.DirEntry, err error) error { if !inPlace {
if err != nil { rootDir = projectInfo.ProjectName
return err 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 {
return fmt.Errorf("failed to remove existing directory: %v", err)
}
}
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 {
relPath, err := filepath.Rel("project", path) if err != nil {
if err != nil { return err
return err }
}
// 如果是隐藏文件模板,将文件名中的前缀 "-" 替换为 "." relPath, err := filepath.Rel("project", path)
fileName := filepath.Base(relPath) if err != nil {
if strings.HasPrefix(fileName, "-") { return err
fileName = "." + strings.TrimPrefix(fileName, "-") }
relPath = filepath.Join(filepath.Dir(relPath), fileName)
}
targetPath := filepath.Join(projectInfo.ProjectName, relPath) fileName := filepath.Base(relPath)
if d.IsDir() { if strings.HasPrefix(fileName, "-") {
log.Infof("创建目录: %s", targetPath) fileName = "." + strings.TrimPrefix(fileName, "-")
return os.MkdirAll(targetPath, 0o755) relPath = filepath.Join(filepath.Dir(relPath), fileName)
} }
// 读取模板内容 targetPath := filepath.Join(rootDir, relPath)
content, err := templates.Project.ReadFile(path) if d.IsDir() {
if err != nil { log.Infof("创建目录: %s", targetPath)
return err return os.MkdirAll(targetPath, 0o755)
} }
// 处理模板文件 content, err := templates.Project.ReadFile(path)
if strings.HasSuffix(path, ".tpl") { if err != nil {
tmpl, err := template.New(filepath.Base(path)).Parse(string(content)) return err
if err != nil { }
return err
}
// 创建目标文件(去除.tpl后缀 if strings.HasSuffix(path, ".tpl") {
targetPath = strings.TrimSuffix(targetPath, ".tpl") if inPlace && strings.HasSuffix(path, string(os.PathSeparator)+"go.mod.tpl") {
log.Infof("创建文件: %s", targetPath) log.Infof("跳过已有文件: %s", filepath.Join(rootDir, "go.mod"))
f, err := os.Create(targetPath) return nil
if err != nil { }
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
}
defer f.Close()
return tmpl.Execute(f, projectInfo) tmpl, err := template.New(filepath.Base(path)).Parse(string(content))
} if err != nil {
return err
}
// 处理模板文件 targetPath = strings.TrimSuffix(targetPath, ".tpl")
if strings.HasSuffix(path, ".raw") { if !force {
// 创建目标文件(去除.tpl后缀 if _, err := os.Stat(targetPath); err == nil {
targetPath = strings.TrimSuffix(targetPath, ".raw") log.Warnf("文件已存在,跳过: %s", targetPath)
log.Infof("创建文件: %s", targetPath) return nil
} }
}
log.Infof("创建文件: %s", targetPath)
f, err := os.Create(targetPath)
if err != nil {
return errors.Wrapf(err, "创建文件失败 %s", targetPath)
}
defer f.Close()
return tmpl.Execute(f, projectInfo)
}
// 复制非模板文件 if strings.HasSuffix(path, ".raw") {
return os.WriteFile(targetPath, content, 0o644) targetPath = strings.TrimSuffix(targetPath, ".raw")
}); err != nil { }
return err
}
// 添加成功提示 if !force {
log.Info("🎉 项目创建成功!") if _, err := os.Stat(targetPath); err == nil {
log.Info("后续步骤:") log.Warnf("文件已存在,跳过: %s", targetPath)
log.Infof(" cd %s", projectInfo.ProjectName) return nil
log.Info(" go mod tidy") }
}
log.Infof("创建文件: %s", targetPath)
return os.WriteFile(targetPath, content, 0o644)
}); err != nil {
return err
}
return nil if inPlace {
log.Info("🎉 项目初始化成功 (当前目录)!")
} else {
log.Info("🎉 项目创建成功!")
log.Info("后续步骤:")
log.Infof(" cd %s", projectInfo.ProjectName)
}
log.Info(" go mod tidy")
return nil
} }