Initial commit
This commit is contained in:
46
skills/cli-patterns/examples/db-cli/README.md
Normal file
46
skills/cli-patterns/examples/db-cli/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Database CLI Tool Example
|
||||
|
||||
Complete database management CLI demonstrating:
|
||||
- Command categories (Schema, Data, Admin)
|
||||
- Before hook for connection validation
|
||||
- After hook for cleanup
|
||||
- Required and optional flags
|
||||
- Environment variable fallbacks
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
# Set connection string
|
||||
export DATABASE_URL="postgres://user:pass@localhost/mydb"
|
||||
|
||||
# Run migrations
|
||||
dbctl migrate
|
||||
dbctl migrate --direction down --steps 2
|
||||
|
||||
# Rollback
|
||||
dbctl rollback
|
||||
|
||||
# Seed database
|
||||
dbctl seed --file seeds/test-data.sql
|
||||
|
||||
# Backup and restore
|
||||
dbctl backup --output backups/db-$(date +%Y%m%d).sql
|
||||
dbctl restore --input backups/db-20240101.sql
|
||||
|
||||
# Admin tasks
|
||||
dbctl status
|
||||
dbctl vacuum
|
||||
|
||||
# Verbose output
|
||||
dbctl -v migrate
|
||||
```
|
||||
|
||||
## Features Demonstrated
|
||||
|
||||
1. **Command Categories**: Schema, Data, Admin
|
||||
2. **Global Flags**: --connection, --verbose
|
||||
3. **Before Hook**: Validates connection before any command
|
||||
4. **After Hook**: Closes connections after command completes
|
||||
5. **Required Flags**: backup/restore require file paths
|
||||
6. **Environment Variables**: DATABASE_URL fallback
|
||||
7. **Flag Aliases**: -v for --verbose, -d for --direction
|
||||
183
skills/cli-patterns/examples/db-cli/main.go
Normal file
183
skills/cli-patterns/examples/db-cli/main.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := &cli.App{
|
||||
Name: "dbctl",
|
||||
Usage: "Database management CLI tool",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "connection",
|
||||
Aliases: []string{"conn"},
|
||||
Usage: "Database connection string",
|
||||
EnvVars: []string{"DATABASE_URL"},
|
||||
Required: true,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "verbose",
|
||||
Aliases: []string{"v"},
|
||||
Usage: "Enable verbose output",
|
||||
},
|
||||
},
|
||||
|
||||
Before: func(c *cli.Context) error {
|
||||
conn := c.String("connection")
|
||||
verbose := c.Bool("verbose")
|
||||
|
||||
if verbose {
|
||||
fmt.Println("🔗 Validating database connection...")
|
||||
}
|
||||
|
||||
// Validate connection string
|
||||
if conn == "" {
|
||||
return fmt.Errorf("database connection string required")
|
||||
}
|
||||
|
||||
if verbose {
|
||||
fmt.Println("✅ Connection string validated")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
|
||||
After: func(c *cli.Context) error {
|
||||
if c.Bool("verbose") {
|
||||
fmt.Println("🔚 Closing database connections...")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
||||
Commands: []*cli.Command{
|
||||
// Schema category
|
||||
{
|
||||
Name: "migrate",
|
||||
Category: "Schema",
|
||||
Usage: "Run database migrations",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "direction",
|
||||
Aliases: []string{"d"},
|
||||
Usage: "Migration direction (up/down)",
|
||||
Value: "up",
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "steps",
|
||||
Usage: "Number of steps to migrate",
|
||||
Value: 0,
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
direction := c.String("direction")
|
||||
steps := c.Int("steps")
|
||||
|
||||
fmt.Printf("Running migrations %s", direction)
|
||||
if steps > 0 {
|
||||
fmt.Printf(" (%d steps)", steps)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "rollback",
|
||||
Category: "Schema",
|
||||
Usage: "Rollback last migration",
|
||||
Action: func(c *cli.Context) error {
|
||||
fmt.Println("Rolling back last migration...")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
||||
// Data category
|
||||
{
|
||||
Name: "seed",
|
||||
Category: "Data",
|
||||
Usage: "Seed database with test data",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "file",
|
||||
Aliases: []string{"f"},
|
||||
Usage: "Seed file path",
|
||||
Value: "seeds/default.sql",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
file := c.String("file")
|
||||
fmt.Printf("Seeding database from: %s\n", file)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "backup",
|
||||
Category: "Data",
|
||||
Usage: "Backup database",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "output",
|
||||
Aliases: []string{"o"},
|
||||
Usage: "Backup output path",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
output := c.String("output")
|
||||
fmt.Printf("Backing up database to: %s\n", output)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "restore",
|
||||
Category: "Data",
|
||||
Usage: "Restore database from backup",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "input",
|
||||
Aliases: []string{"i"},
|
||||
Usage: "Backup file path",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
input := c.String("input")
|
||||
fmt.Printf("Restoring database from: %s\n", input)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
||||
// Admin category
|
||||
{
|
||||
Name: "status",
|
||||
Category: "Admin",
|
||||
Usage: "Check database status",
|
||||
Action: func(c *cli.Context) error {
|
||||
fmt.Println("Database Status:")
|
||||
fmt.Println(" Connection: Active")
|
||||
fmt.Println(" Tables: 15")
|
||||
fmt.Println(" Size: 245 MB")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "vacuum",
|
||||
Category: "Admin",
|
||||
Usage: "Optimize database",
|
||||
Action: func(c *cli.Context) error {
|
||||
fmt.Println("Optimizing database...")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user