Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 09:04:14 +08:00
commit 70c36b5eff
248 changed files with 47482 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
#!/bin/bash
# Add a new command to existing CLI
set -euo pipefail
if [ $# -lt 2 ]; then
echo "Usage: $0 <app-name> <command-name> [category]"
echo "Example: $0 myapp backup Deploy"
exit 1
fi
APP_NAME="$1"
COMMAND_NAME="$2"
CATEGORY="${3:-General}"
if [ ! -d "$APP_NAME" ]; then
echo "Error: Directory $APP_NAME not found"
exit 1
fi
cd "$APP_NAME"
# Create command implementation
FUNC_NAME="${COMMAND_NAME}Command"
cat >> commands.go <<EOF
func ${FUNC_NAME}(c *cli.Context) error {
fmt.Println("Executing ${COMMAND_NAME} command...")
// TODO: Implement ${COMMAND_NAME} logic
return nil
}
EOF
# Generate command definition
cat > /tmp/new_command.txt <<EOF
{
Name: "${COMMAND_NAME}",
Category: "${CATEGORY}",
Usage: "TODO: Add usage description",
Action: ${FUNC_NAME},
},
EOF
echo "✅ Command stub created!"
echo ""
echo "Next steps:"
echo "1. Add the following to your Commands slice in main.go:"
cat /tmp/new_command.txt
echo ""
echo "2. Implement the logic in commands.go:${FUNC_NAME}"
echo "3. Add flags if needed"

View File

@@ -0,0 +1,109 @@
#!/bin/bash
# Generate basic CLI structure with urfave/cli
set -euo pipefail
APP_NAME="${1:-myapp}"
echo "Generating basic CLI: $APP_NAME"
# Create project structure
mkdir -p "$APP_NAME"
cd "$APP_NAME"
# Initialize Go module
go mod init "$APP_NAME" 2>/dev/null || true
# Install urfave/cli
echo "Installing urfave/cli v2..."
go get github.com/urfave/cli/v2@latest
# Create main.go
cat > main.go <<'EOF'
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "APP_NAME_PLACEHOLDER",
Usage: "A simple CLI tool",
Version: "0.1.0",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Enable verbose output",
EnvVars: []string{"VERBOSE"},
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to config file",
EnvVars: []string{"CONFIG_PATH"},
},
},
Action: func(c *cli.Context) error {
verbose := c.Bool("verbose")
config := c.String("config")
if verbose {
fmt.Println("Verbose mode enabled")
}
if config != "" {
fmt.Printf("Using config: %s\n", config)
}
fmt.Println("Hello from APP_NAME_PLACEHOLDER!")
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
EOF
# Replace placeholder
sed -i "s/APP_NAME_PLACEHOLDER/$APP_NAME/g" main.go
# Create README
cat > README.md <<EOF
# $APP_NAME
A CLI tool built with urfave/cli.
## Installation
\`\`\`bash
go install
\`\`\`
## Usage
\`\`\`bash
$APP_NAME --help
$APP_NAME --verbose
$APP_NAME --config config.yaml
\`\`\`
## Environment Variables
- \`VERBOSE\`: Enable verbose output
- \`CONFIG_PATH\`: Path to config file
EOF
# Build
echo "Building..."
go build -o "$APP_NAME" .
echo "✅ Basic CLI generated successfully!"
echo "Run: ./$APP_NAME --help"

View File

@@ -0,0 +1,313 @@
#!/bin/bash
# Generate complete CLI with all patterns
set -euo pipefail
APP_NAME="${1:-myapp}"
echo "Generating full-featured CLI: $APP_NAME"
# Create project structure
mkdir -p "$APP_NAME"
cd "$APP_NAME"
# Initialize Go module
go mod init "$APP_NAME" 2>/dev/null || true
# Install dependencies
echo "Installing dependencies..."
go get github.com/urfave/cli/v2@latest
# Create main.go with all patterns
cat > main.go <<'EOF'
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli/v2"
)
// AppContext holds shared state
type AppContext struct {
Verbose bool
Config string
}
func main() {
app := &cli.App{
Name: "APP_NAME_PLACEHOLDER",
Usage: "A full-featured CLI tool with all patterns",
Version: "0.1.0",
// Global flags available to all commands
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Enable verbose output",
EnvVars: []string{"VERBOSE"},
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to config file",
EnvVars: []string{"CONFIG_PATH"},
Value: "config.yaml",
},
},
// Before hook - runs before any command
Before: func(c *cli.Context) error {
verbose := c.Bool("verbose")
config := c.String("config")
if verbose {
fmt.Println("🚀 Initializing application...")
}
// Store context for use in commands
ctx := &AppContext{
Verbose: verbose,
Config: config,
}
c.App.Metadata["ctx"] = ctx
return nil
},
// After hook - runs after any command
After: func(c *cli.Context) error {
if ctx, ok := c.App.Metadata["ctx"].(*AppContext); ok {
if ctx.Verbose {
fmt.Println("✅ Application finished successfully")
}
}
return nil
},
// Commands organized by category
Commands: []*cli.Command{
{
Name: "build",
Category: "Build",
Usage: "Build the project",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output file path",
Value: "dist/app",
},
&cli.BoolFlag{
Name: "optimize",
Usage: "Enable optimizations",
Value: true,
},
},
Before: func(c *cli.Context) error {
fmt.Println("Preparing build...")
return nil
},
Action: func(c *cli.Context) error {
output := c.String("output")
optimize := c.Bool("optimize")
fmt.Printf("Building to: %s\n", output)
if optimize {
fmt.Println("Optimizations: enabled")
}
return nil
},
After: func(c *cli.Context) error {
fmt.Println("Build complete!")
return nil
},
},
{
Name: "test",
Category: "Build",
Usage: "Run tests",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "coverage",
Aliases: []string{"cov"},
Usage: "Generate coverage report",
},
},
Action: func(c *cli.Context) error {
coverage := c.Bool("coverage")
fmt.Println("Running tests...")
if coverage {
fmt.Println("Generating coverage report...")
}
return nil
},
},
{
Name: "deploy",
Category: "Deploy",
Usage: "Deploy the application",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "env",
Aliases: []string{"e"},
Usage: "Target environment",
Required: true,
Value: "staging",
},
},
Action: func(c *cli.Context) error {
env := c.String("env")
fmt.Printf("Deploying to %s...\n", env)
return nil
},
},
{
Name: "rollback",
Category: "Deploy",
Usage: "Rollback deployment",
Action: func(c *cli.Context) error {
fmt.Println("Rolling back deployment...")
return nil
},
},
{
Name: "logs",
Category: "Monitor",
Usage: "View application logs",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "tail",
Aliases: []string{"n"},
Usage: "Number of lines to show",
Value: 100,
},
&cli.BoolFlag{
Name: "follow",
Aliases: []string{"f"},
Usage: "Follow log output",
},
},
Action: func(c *cli.Context) error {
tail := c.Int("tail")
follow := c.Bool("follow")
fmt.Printf("Showing last %d lines...\n", tail)
if follow {
fmt.Println("Following logs...")
}
return nil
},
},
{
Name: "status",
Category: "Monitor",
Usage: "Check application status",
Action: func(c *cli.Context) error {
fmt.Println("Application status: healthy")
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
EOF
# Replace placeholder
sed -i "s/APP_NAME_PLACEHOLDER/$APP_NAME/g" main.go
# Create comprehensive README
cat > README.md <<EOF
# $APP_NAME
A full-featured CLI tool demonstrating all urfave/cli patterns.
## Features
- ✅ Global flags with environment variable fallbacks
- ✅ Command categories for organization
- ✅ Before/After hooks for lifecycle management
- ✅ Context management for shared state
- ✅ Comprehensive flag types
- ✅ Subcommands and aliases
- ✅ Help text and documentation
## Installation
\`\`\`bash
go install
\`\`\`
## Usage
### Build Commands
\`\`\`bash
$APP_NAME build
$APP_NAME build --output dist/myapp --optimize
$APP_NAME test --coverage
\`\`\`
### Deploy Commands
\`\`\`bash
$APP_NAME deploy --env staging
$APP_NAME deploy -e production
$APP_NAME rollback
\`\`\`
### Monitor Commands
\`\`\`bash
$APP_NAME logs
$APP_NAME logs --tail 50 --follow
$APP_NAME status
\`\`\`
### Global Flags
\`\`\`bash
$APP_NAME --verbose build
$APP_NAME --config custom.yaml deploy --env prod
\`\`\`
## Environment Variables
- \`VERBOSE\`: Enable verbose output
- \`CONFIG_PATH\`: Path to config file
## Examples
\`\`\`bash
# Build with optimizations
$APP_NAME -v build -o dist/app --optimize
# Deploy to production
$APP_NAME --config prod.yaml deploy -e production
# Follow logs
$APP_NAME logs -f -n 200
\`\`\`
EOF
# Build
echo "Building..."
go build -o "$APP_NAME" .
echo "✅ Full-featured CLI generated successfully!"
echo ""
echo "Try these commands:"
echo " ./$APP_NAME --help"
echo " ./$APP_NAME build --help"
echo " ./$APP_NAME -v build"

View File

@@ -0,0 +1,174 @@
#!/bin/bash
# Generate CLI with subcommands structure
set -euo pipefail
APP_NAME="${1:-myapp}"
echo "Generating CLI with subcommands: $APP_NAME"
# Create project structure
mkdir -p "$APP_NAME/commands"
cd "$APP_NAME"
# Initialize Go module
go mod init "$APP_NAME" 2>/dev/null || true
# Install urfave/cli
echo "Installing urfave/cli v2..."
go get github.com/urfave/cli/v2@latest
# Create main.go
cat > main.go <<'EOF'
package main
import (
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "APP_NAME_PLACEHOLDER",
Usage: "A multi-command CLI tool",
Version: "0.1.0",
Commands: []*cli.Command{
{
Name: "start",
Aliases: []string{"s"},
Usage: "Start the service",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: 8080,
Usage: "Port to listen on",
},
},
Action: func(c *cli.Context) error {
return startCommand(c)
},
},
{
Name: "stop",
Usage: "Stop the service",
Action: stopCommand,
},
{
Name: "status",
Usage: "Check service status",
Action: statusCommand,
},
{
Name: "config",
Usage: "Configuration management",
Subcommands: []*cli.Command{
{
Name: "show",
Usage: "Show current configuration",
Action: configShowCommand,
},
{
Name: "set",
Usage: "Set configuration value",
Action: configSetCommand,
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
EOF
# Create commands.go
cat > commands.go <<'EOF'
package main
import (
"fmt"
"github.com/urfave/cli/v2"
)
func startCommand(c *cli.Context) error {
port := c.Int("port")
fmt.Printf("Starting service on port %d...\n", port)
return nil
}
func stopCommand(c *cli.Context) error {
fmt.Println("Stopping service...")
return nil
}
func statusCommand(c *cli.Context) error {
fmt.Println("Service status: running")
return nil
}
func configShowCommand(c *cli.Context) error {
fmt.Println("Current configuration:")
fmt.Println(" port: 8080")
fmt.Println(" host: localhost")
return nil
}
func configSetCommand(c *cli.Context) error {
key := c.Args().Get(0)
value := c.Args().Get(1)
if key == "" || value == "" {
return fmt.Errorf("usage: config set <key> <value>")
}
fmt.Printf("Setting %s = %s\n", key, value)
return nil
}
EOF
# Replace placeholder
sed -i "s/APP_NAME_PLACEHOLDER/$APP_NAME/g" main.go
# Create README
cat > README.md <<EOF
# $APP_NAME
A CLI tool with subcommands built with urfave/cli.
## Installation
\`\`\`bash
go install
\`\`\`
## Usage
\`\`\`bash
# Start service
$APP_NAME start --port 8080
$APP_NAME s -p 3000
# Stop service
$APP_NAME stop
# Check status
$APP_NAME status
# Configuration
$APP_NAME config show
$APP_NAME config set host 0.0.0.0
\`\`\`
EOF
# Build
echo "Building..."
go build -o "$APP_NAME" .
echo "✅ CLI with subcommands generated successfully!"
echo "Run: ./$APP_NAME --help"

View File

@@ -0,0 +1,103 @@
#!/bin/bash
# Validate CLI structure and best practices
set -euo pipefail
PROJECT_PATH="${1:-.}"
echo "🔍 Validating CLI project: $PROJECT_PATH"
cd "$PROJECT_PATH"
ERRORS=0
# Check if main.go exists
if [ ! -f "main.go" ]; then
echo "❌ main.go not found"
ERRORS=$((ERRORS + 1))
else
echo "✅ main.go exists"
fi
# Check if go.mod exists
if [ ! -f "go.mod" ]; then
echo "❌ go.mod not found (run 'go mod init')"
ERRORS=$((ERRORS + 1))
else
echo "✅ go.mod exists"
fi
# Check for urfave/cli dependency
if grep -q "github.com/urfave/cli/v2" go.mod 2>/dev/null; then
echo "✅ urfave/cli dependency found"
else
echo "⚠️ urfave/cli dependency not found"
fi
# Check for App definition
if grep -q "cli.App" main.go 2>/dev/null; then
echo "✅ cli.App definition found"
else
echo "❌ cli.App definition not found"
ERRORS=$((ERRORS + 1))
fi
# Check for Usage field
if grep -q "Usage:" main.go 2>/dev/null; then
echo "✅ Usage field defined"
else
echo "⚠️ Usage field not found (recommended)"
fi
# Check for Version field
if grep -q "Version:" main.go 2>/dev/null; then
echo "✅ Version field defined"
else
echo "⚠️ Version field not found (recommended)"
fi
# Check if commands have descriptions
if grep -A 5 "Commands:" main.go 2>/dev/null | grep -q "Usage:"; then
echo "✅ Commands have usage descriptions"
else
echo "⚠️ Some commands might be missing usage descriptions"
fi
# Check for proper error handling
if grep -q "if err := app.Run" main.go 2>/dev/null; then
echo "✅ Proper error handling in main"
else
echo "❌ Missing error handling for app.Run"
ERRORS=$((ERRORS + 1))
fi
# Try to build
echo ""
echo "🔨 Attempting build..."
if go build -o /tmp/test_build . 2>&1; then
echo "✅ Build successful"
rm -f /tmp/test_build
else
echo "❌ Build failed"
ERRORS=$((ERRORS + 1))
fi
# Run go vet
echo ""
echo "🔍 Running go vet..."
if go vet ./... 2>&1; then
echo "✅ go vet passed"
else
echo "⚠️ go vet found issues"
fi
# Summary
echo ""
echo "================================"
if [ $ERRORS -eq 0 ]; then
echo "✅ Validation passed! No critical errors found."
exit 0
else
echo "❌ Validation failed with $ERRORS critical error(s)"
exit 1
fi