Add --filter batch mode, rename binary to gormcol
This commit is contained in:
26
README.md
26
README.md
@@ -69,15 +69,19 @@ go install ./cmd/gormcol
|
||||
### Usage
|
||||
|
||||
```
|
||||
gormcol-gen [options]
|
||||
gormcol [options]
|
||||
```
|
||||
|
||||
DSN can be provided via `--dsn` flag or `DSN` env var (from `.env` file).
|
||||
|
||||
**Modes:**
|
||||
- **Interactive (default)**: select tables with fzf
|
||||
- **Batch (`--filter` or `--all`)**: generate matching tables with confirmation
|
||||
|
||||
| Flag | Default | Description |
|
||||
|----------|------------------------|--------------------------------------------------|
|
||||
| `--dsn` | *(from DSN env)* | MySQL/MariaDB DSN, e.g. `user:pass@tcp(localhost:3306)/dbname` |
|
||||
| `--filter` | `(ps_|b2b_).*` | Regex matching table names to generate |
|
||||
| `--filter` | *(interactive)* | Regex matching table names to generate (triggers batch mode) |
|
||||
| `--all` | *(interactive)* | Generate all tables matching filter (shows confirmation) |
|
||||
| `--out` | `./app/model/dbmodel` | Output directory for generated files |
|
||||
| `--pkg` | `dbmodel` | Go package name for generated files |
|
||||
@@ -87,7 +91,7 @@ DSN can be provided via `--dsn` flag or `DSN` env var (from `.env` file).
|
||||
Without flags, the tool launches an interactive table selector:
|
||||
|
||||
```bash
|
||||
gormcol-gen --dsn "user:pass@tcp(localhost:3306)/mydb"
|
||||
gormcol --dsn "user:pass@tcp(localhost:3306)/mydb"
|
||||
```
|
||||
|
||||
Features:
|
||||
@@ -96,12 +100,20 @@ Features:
|
||||
- **Enter** - confirm selection
|
||||
- **Esc** - cancel
|
||||
|
||||
### Generate All Tables
|
||||
### Batch Mode (--filter)
|
||||
|
||||
Use `--all` to generate all tables matching the filter:
|
||||
Use `--filter` to generate all tables matching a regex pattern:
|
||||
|
||||
```bash
|
||||
gormcol-gen --dsn "user:pass@tcp(localhost:3306)/mydb" --all
|
||||
gormcol --dsn "user:pass@tcp(localhost:3306)/mydb" --filter "ps_product.*"
|
||||
```
|
||||
|
||||
### Generate All Tables (--all)
|
||||
|
||||
Use `--all` to generate all tables matching the default filter `(ps_|b2b_).*`:
|
||||
|
||||
```bash
|
||||
gormcol --dsn "user:pass@tcp(localhost:3306)/mydb" --all
|
||||
```
|
||||
|
||||
A confirmation prompt appears:
|
||||
@@ -115,7 +127,7 @@ WARNING: Generate all 325 tables? [Enter] confirm / [Esc] cancel
|
||||
### Example
|
||||
|
||||
```bash
|
||||
./gormcol-gen --dsn "user:pass@tcp(localhost:3306)/mydb" --filter "ps_.*" --out ./internal/model --pkg model
|
||||
./gormcol --dsn "user:pass@tcp(localhost:3306)/mydb" --filter "ps_.*" --out ./internal/model --pkg model
|
||||
```
|
||||
|
||||
This connects to the database, generates a `.go` model file for each matching table, and appends `<Model>Cols` variables with typed `gormcol.Field` descriptors to each file.
|
||||
|
||||
@@ -20,14 +20,17 @@ import (
|
||||
// and generates GORM models with column descriptors.
|
||||
func main() {
|
||||
dsn := flag.String("dsn", "", "database DSN (e.g. user:pass@tcp(host:3306)/dbname)")
|
||||
filter := flag.String("filter", "(ps_|b2b_).*", "regex to match table names")
|
||||
filter := flag.String("filter", "", "regex to match table names (triggers batch mode)")
|
||||
all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)")
|
||||
outDir := flag.String("out", "./app/model/dbmodel", "output directory for generated files")
|
||||
pkgName := flag.String("pkg", "dbmodel", "Go package name for generated files")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "gormcol-gen - generate GORM models with column descriptors\n\n")
|
||||
fmt.Fprintf(os.Stderr, "gormcol - generate GORM models with column descriptors\n\n")
|
||||
fmt.Fprintf(os.Stderr, "Usage:\n")
|
||||
fmt.Fprintf(os.Stderr, " gormcol-gen [options]\n\n")
|
||||
fmt.Fprintf(os.Stderr, " gormcol [options]\n\n")
|
||||
fmt.Fprintf(os.Stderr, "Modes:\n")
|
||||
fmt.Fprintf(os.Stderr, " - Interactive (default): select tables with fzf\n")
|
||||
fmt.Fprintf(os.Stderr, " - Batch (--filter or --all): generate matching tables with confirmation\n\n")
|
||||
fmt.Fprintf(os.Stderr, "DSN can be provided via --dsn flag or DSN env var (from .env)\n\n")
|
||||
fmt.Fprintf(os.Stderr, "Flags:\n")
|
||||
flag.PrintDefaults()
|
||||
@@ -65,15 +68,25 @@ func main() {
|
||||
TableFilter: *filter,
|
||||
}
|
||||
|
||||
// Interactive mode (default): select tables using fzf.
|
||||
// All mode: generate all tables matching filter with confirmation.
|
||||
if *all {
|
||||
// Modes:
|
||||
// 1. --all: generate all tables matching filter with confirmation
|
||||
// 2. --filter: generate all tables matching provided regex with confirmation
|
||||
// 3. Default: interactive mode with fzf
|
||||
if *all || *filter != "" {
|
||||
if *filter == "" {
|
||||
// --all without explicit filter, use default
|
||||
cfg.TableFilter = "(ps_|b2b_).*"
|
||||
}
|
||||
yellow := "\033[33m"
|
||||
reset := "\033[0m"
|
||||
fmt.Printf("%sBatch mode:%s generating tables matching filter '%s'\n", yellow, reset, cfg.TableFilter)
|
||||
if !confirmGenerateAll(db) {
|
||||
fmt.Println("Aborted.")
|
||||
os.Exit(0)
|
||||
}
|
||||
} else {
|
||||
// Check if fzf is available for interactive mode.
|
||||
// Interactive mode
|
||||
// Check if fzf is available.
|
||||
if _, err := exec.LookPath("fzf"); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error: fzf is required for interactive mode. Install from https://github.com/junegunn/fzf")
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user