feat: 增加命令行工具的参数支持,包括路径、干运行模式和输出目录选项

This commit is contained in:
Rogee
2025-09-10 14:38:53 +08:00
parent 3b804b83da
commit 7187205143
7 changed files with 174 additions and 104 deletions

View File

@@ -15,10 +15,15 @@ func CommandBuf(root *cobra.Command) {
RunE: commandBufE,
}
cmd.Flags().String("dir", ".", "Directory to run buf from")
cmd.Flags().Bool("dry-run", false, "Preview buf command without executing")
root.AddCommand(cmd)
}
func commandBufE(cmd *cobra.Command, args []string) error {
dir := cmd.Flag("dir").Value.String()
dryRun, _ := cmd.Flags().GetBool("dry-run")
if _, err := exec.LookPath("buf"); err != nil {
log.Warn("buf 命令不存在,正在安装 buf...")
log.Info("go install github.com/bufbuild/buf/cmd/buf@v1.48.0")
@@ -33,9 +38,19 @@ func commandBufE(cmd *cobra.Command, args []string) error {
}
}
// preflight: ensure buf.yaml exists
if _, err := os.Stat(filepath.Join(dir, "buf.yaml")); err != nil {
log.Warnf("未找到 %sbuf generate 可能失败", filepath.Join(dir, "buf.yaml"))
}
log.Info("buf 命令已存在,正在运行 buf generate...")
log.Info("PROTOBUF GUIDE: https://buf.build/docs/best-practices/style-guide/")
if dryRun {
log.Infof("[dry-run] (cd %s && buf generate)", dir)
return nil
}
generateCmd := exec.Command("buf", "generate")
generateCmd.Dir = dir
if err := generateCmd.Run(); err != nil {
return fmt.Errorf("运行 buf generate 失败: %v", err)
}

View File

@@ -16,6 +16,9 @@ func CommandFmt(root *cobra.Command) {
RunE: commandFmtE,
}
cmd.Flags().Bool("check", false, "Check formatting without writing changes")
cmd.Flags().String("path", ".", "Path to format (default .)")
root.AddCommand(cmd)
}
@@ -34,8 +37,25 @@ func commandFmtE(cmd *cobra.Command, args []string) error {
}
}
check, _ := cmd.Flags().GetBool("check")
path, _ := cmd.Flags().GetString("path")
if check {
log.Info("运行 gofumpt 检查模式...")
out, err := exec.Command("gofumpt", "-l", "-extra", path).CombinedOutput()
if err != nil {
return fmt.Errorf("运行 gofumpt 失败: %v", err)
}
if len(out) > 0 {
fmt.Fprintln(os.Stdout, string(out))
return fmt.Errorf("发现未格式化文件,请运行: gofumpt -l -extra -w %s", path)
}
log.Info("代码格式良好")
return nil
}
log.Info("运行 gofumpt...")
gofumptCmd := exec.Command("gofumpt", "-l", "-extra", "-w", ".")
gofumptCmd := exec.Command("gofumpt", "-l", "-extra", "-w", path)
gofumptCmd.Stdout = os.Stdout
gofumptCmd.Stderr = os.Stderr
if err := gofumptCmd.Run(); err != nil {

View File

@@ -32,6 +32,10 @@ func CommandGenModel(root *cobra.Command) {
RunE: commandGenModelE,
}
cmd.Flags().String("schema", "", "Override database schema")
cmd.Flags().Bool("rename-schemas", true, "Rename generated database/<db> to database/schemas")
cmd.Flags().String("schemas-out", "database/schemas", "Schemas output directory when renaming")
root.AddCommand(cmd)
}
@@ -45,6 +49,11 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "get db")
}
// optional schema override
if s := cmd.Flag("schema").Value.String(); s != "" {
dbConf.Schema = s
}
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile("database/transform.yaml")
@@ -166,14 +175,16 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return err
}
if err := os.RemoveAll("database/schemas"); err != nil {
if rename, _ := cmd.Flags().GetBool("rename-schemas"); rename {
out := cmd.Flag("schemas-out").Value.String()
if err := os.RemoveAll(out); err != nil {
return err
}
dataPath := fmt.Sprintf("database/%s", cfg.Database)
if err := os.Rename(dataPath, "database/schemas"); err != nil {
if err := os.Rename(dataPath, out); err != nil {
return err
}
}
if err := astModel.Generate(generatedTables, transformer); err != nil {
return err

View File

@@ -1,6 +1,7 @@
package cmd
import (
"fmt"
"io/fs"
"os"
"path/filepath"
@@ -21,6 +22,8 @@ func CommandGenRoute(root *cobra.Command) {
PostRunE: commandGenProviderE,
}
cmd.Flags().String("path", ".", "Base path to scan (defaults to CWD)")
root.AddCommand(cmd)
}
@@ -37,6 +40,11 @@ func commandGenRouteE(cmd *cobra.Command, args []string) error {
}
}
// allow overriding via --path flag
if f := cmd.Flag("path"); f != nil && f.Value.String() != "." && f.Value.String() != "" {
path = f.Value.String()
}
path, _ = filepath.Abs(path)
err = gomod.Parse(filepath.Join(path, "go.mod"))

View File

@@ -19,6 +19,8 @@ func CommandMigrate(root *cobra.Command) {
RunE: commandMigrate,
}
cmd.Flags().StringP("config", "c", "config.toml", "database config file")
cmd.Flags().String("dir", "database/migrations", "migrations directory")
cmd.Flags().String("table", "migrations", "migrations table name")
root.AddCommand(cmd)
}
@@ -42,7 +44,10 @@ func commandMigrate(cmd *cobra.Command, args []string) error {
action, args := args[0], args[1:]
log.Infof("migration action: %s args: %+v", action, args)
goose.SetTableName("migrations")
dir := cmd.Flag("dir").Value.String()
table := cmd.Flag("table").Value.String()
return goose.RunContext(context.Background(), action, db, "database/migrations", args...)
goose.SetTableName(table)
return goose.RunContext(context.Background(), action, db, dir, args...)
}

View File

@@ -13,13 +13,18 @@ func CommandSwagFmt(root *cobra.Command) {
RunE: commandSwagFmtE,
}
cmd.Flags().String("dir", "./app/http", "SearchDir for swag format")
cmd.Flags().String("main", "main.go", "MainFile for swag format")
root.AddCommand(cmd)
}
func commandSwagFmtE(cmd *cobra.Command, args []string) error {
dir := cmd.Flag("dir").Value.String()
main := cmd.Flag("main").Value.String()
return format.New().Build(&format.Config{
SearchDir: "./app/http",
SearchDir: dir,
Excludes: "",
MainFile: "main.go",
MainFile: main,
})
}

View File

@@ -18,27 +18,33 @@ func CommandSwagInit(root *cobra.Command) {
RunE: commandSwagInitE,
}
cmd.Flags().String("dir", ".", "SearchDir (project root)")
cmd.Flags().String("out", "docs", "Output dir for generated docs")
cmd.Flags().String("main", "main.go", "Main API file path")
root.AddCommand(cmd)
}
func commandSwagInitE(cmd *cobra.Command, args []string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
if len(args) > 0 {
pwd = args[0]
root := cmd.Flag("dir").Value.String()
if root == "" {
var err error
root, err = os.Getwd()
if err != nil { return err }
}
leftDelim, rightDelim := "{{", "}}"
outDir := cmd.Flag("out").Value.String()
mainFile := cmd.Flag("main").Value.String()
return gen.New().Build(&gen.Config{
SearchDir: pwd,
SearchDir: root,
Excludes: "",
ParseExtension: "",
MainAPIFile: "main.go",
MainAPIFile: mainFile,
PropNamingStrategy: swag.CamelCase,
OutputDir: filepath.Join(pwd, "docs"),
OutputDir: filepath.Join(root, outDir),
OutputTypes: []string{"go", "json", "yaml"},
ParseVendor: false,
ParseDependency: 0,