feat: 增加命令行工具的参数支持,包括路径、干运行模式和输出目录选项
This commit is contained in:
15
cmd/buf.go
15
cmd/buf.go
@@ -15,10 +15,15 @@ func CommandBuf(root *cobra.Command) {
|
|||||||
RunE: commandBufE,
|
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)
|
root.AddCommand(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func commandBufE(cmd *cobra.Command, args []string) error {
|
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 {
|
if _, err := exec.LookPath("buf"); err != nil {
|
||||||
log.Warn("buf 命令不存在,正在安装 buf...")
|
log.Warn("buf 命令不存在,正在安装 buf...")
|
||||||
log.Info("go install github.com/bufbuild/buf/cmd/buf@v1.48.0")
|
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("未找到 %s,buf generate 可能失败", filepath.Join(dir, "buf.yaml"))
|
||||||
|
}
|
||||||
|
|
||||||
log.Info("buf 命令已存在,正在运行 buf generate...")
|
log.Info("buf 命令已存在,正在运行 buf generate...")
|
||||||
log.Info("PROTOBUF GUIDE: https://buf.build/docs/best-practices/style-guide/")
|
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 := exec.Command("buf", "generate")
|
||||||
|
generateCmd.Dir = dir
|
||||||
if err := generateCmd.Run(); err != nil {
|
if err := generateCmd.Run(); err != nil {
|
||||||
return fmt.Errorf("运行 buf generate 失败: %v", err)
|
return fmt.Errorf("运行 buf generate 失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
22
cmd/fmt.go
22
cmd/fmt.go
@@ -16,6 +16,9 @@ func CommandFmt(root *cobra.Command) {
|
|||||||
RunE: commandFmtE,
|
RunE: commandFmtE,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd.Flags().Bool("check", false, "Check formatting without writing changes")
|
||||||
|
cmd.Flags().String("path", ".", "Path to format (default .)")
|
||||||
|
|
||||||
root.AddCommand(cmd)
|
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...")
|
log.Info("运行 gofumpt...")
|
||||||
gofumptCmd := exec.Command("gofumpt", "-l", "-extra", "-w", ".")
|
gofumptCmd := exec.Command("gofumpt", "-l", "-extra", "-w", path)
|
||||||
gofumptCmd.Stdout = os.Stdout
|
gofumptCmd.Stdout = os.Stdout
|
||||||
gofumptCmd.Stderr = os.Stderr
|
gofumptCmd.Stderr = os.Stderr
|
||||||
if err := gofumptCmd.Run(); err != nil {
|
if err := gofumptCmd.Run(); err != nil {
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ func CommandGenModel(root *cobra.Command) {
|
|||||||
RunE: commandGenModelE,
|
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)
|
root.AddCommand(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,6 +49,11 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
return errors.Wrap(err, "get db")
|
return errors.Wrap(err, "get db")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// optional schema override
|
||||||
|
if s := cmd.Flag("schema").Value.String(); s != "" {
|
||||||
|
dbConf.Schema = s
|
||||||
|
}
|
||||||
|
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
v.SetConfigFile("database/transform.yaml")
|
v.SetConfigFile("database/transform.yaml")
|
||||||
@@ -166,14 +175,16 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dataPath := fmt.Sprintf("database/%s", cfg.Database)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := astModel.Generate(generatedTables, transformer); err != nil {
|
if err := astModel.Generate(generatedTables, transformer); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -21,6 +22,8 @@ func CommandGenRoute(root *cobra.Command) {
|
|||||||
PostRunE: commandGenProviderE,
|
PostRunE: commandGenProviderE,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmd.Flags().String("path", ".", "Base path to scan (defaults to CWD)")
|
||||||
|
|
||||||
root.AddCommand(cmd)
|
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)
|
path, _ = filepath.Abs(path)
|
||||||
|
|
||||||
err = gomod.Parse(filepath.Join(path, "go.mod"))
|
err = gomod.Parse(filepath.Join(path, "go.mod"))
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ func CommandMigrate(root *cobra.Command) {
|
|||||||
RunE: commandMigrate,
|
RunE: commandMigrate,
|
||||||
}
|
}
|
||||||
cmd.Flags().StringP("config", "c", "config.toml", "database config file")
|
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)
|
root.AddCommand(cmd)
|
||||||
}
|
}
|
||||||
@@ -42,7 +44,10 @@ func commandMigrate(cmd *cobra.Command, args []string) error {
|
|||||||
action, args := args[0], args[1:]
|
action, args := args[0], args[1:]
|
||||||
log.Infof("migration action: %s args: %+v", action, args)
|
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...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,18 @@ func CommandSwagFmt(root *cobra.Command) {
|
|||||||
RunE: commandSwagFmtE,
|
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)
|
root.AddCommand(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func commandSwagFmtE(cmd *cobra.Command, args []string) error {
|
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{
|
return format.New().Build(&format.Config{
|
||||||
SearchDir: "./app/http",
|
SearchDir: dir,
|
||||||
Excludes: "",
|
Excludes: "",
|
||||||
MainFile: "main.go",
|
MainFile: main,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,27 +18,33 @@ func CommandSwagInit(root *cobra.Command) {
|
|||||||
RunE: commandSwagInitE,
|
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)
|
root.AddCommand(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func commandSwagInitE(cmd *cobra.Command, args []string) error {
|
func commandSwagInitE(cmd *cobra.Command, args []string) error {
|
||||||
pwd, err := os.Getwd()
|
root := cmd.Flag("dir").Value.String()
|
||||||
if err != nil {
|
if root == "" {
|
||||||
return err
|
var err error
|
||||||
}
|
root, err = os.Getwd()
|
||||||
if len(args) > 0 {
|
if err != nil { return err }
|
||||||
pwd = args[0]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
leftDelim, rightDelim := "{{", "}}"
|
leftDelim, rightDelim := "{{", "}}"
|
||||||
|
|
||||||
|
outDir := cmd.Flag("out").Value.String()
|
||||||
|
mainFile := cmd.Flag("main").Value.String()
|
||||||
|
|
||||||
return gen.New().Build(&gen.Config{
|
return gen.New().Build(&gen.Config{
|
||||||
SearchDir: pwd,
|
SearchDir: root,
|
||||||
Excludes: "",
|
Excludes: "",
|
||||||
ParseExtension: "",
|
ParseExtension: "",
|
||||||
MainAPIFile: "main.go",
|
MainAPIFile: mainFile,
|
||||||
PropNamingStrategy: swag.CamelCase,
|
PropNamingStrategy: swag.CamelCase,
|
||||||
OutputDir: filepath.Join(pwd, "docs"),
|
OutputDir: filepath.Join(root, outDir),
|
||||||
OutputTypes: []string{"go", "json", "yaml"},
|
OutputTypes: []string{"go", "json", "yaml"},
|
||||||
ParseVendor: false,
|
ParseVendor: false,
|
||||||
ParseDependency: 0,
|
ParseDependency: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user