|
| 1 | +# Shopmon CLI |
| 2 | + |
| 3 | +A command-line tool for monitoring and managing Shopware applications with deployment telemetry. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Execute deployment commands with transparent output |
| 8 | +- Capture command execution metrics (output, return code, execution time) |
| 9 | +- Read composer.json dependencies and include in telemetry |
| 10 | +- Send telemetry data to monitoring service |
| 11 | +- Support for custom monitoring endpoint via environment variable |
| 12 | +- Authorization token support for secure telemetry submission |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Basic Usage |
| 17 | + |
| 18 | +```bash |
| 19 | +./shopmon-cli deploy -- <command> |
| 20 | +``` |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +```bash |
| 25 | +# Execute a simple command |
| 26 | +./shopmon-cli deploy -- echo "Hello World" |
| 27 | + |
| 28 | +# Run PHP artisan commands |
| 29 | +./shopmon-cli deploy -- php artisan migrate |
| 30 | + |
| 31 | +# Execute composer commands |
| 32 | +./shopmon-cli deploy -- composer install |
| 33 | + |
| 34 | +# Multiple word commands |
| 35 | +./shopmon-cli deploy -- npm run build |
| 36 | +``` |
| 37 | + |
| 38 | +### Environment Variables |
| 39 | + |
| 40 | +- `SHOPMON_BASE_URL`: Override the default monitoring service URL (default: `https://shopmon.fos.gg`) |
| 41 | +- `SHOPMON_API_KEY`: Authorization token for the monitoring service (required, sent as Bearer token) |
| 42 | +- `SHOPMON_SHOP_ID`: Shop ID to include in telemetry |
| 43 | +- `SHOPMON_DEPLOYMENT_VERSION_REFERENCE`: Override the version reference (defaults to `git rev-parse HEAD`) |
| 44 | + |
| 45 | +```bash |
| 46 | +# With custom URL |
| 47 | +SHOPMON_BASE_URL="http://localhost:8080" \ |
| 48 | +SHOPMON_API_KEY="your-secret-token" \ |
| 49 | +./shopmon-cli deploy -- php artisan migrate |
| 50 | + |
| 51 | +# With authorization token |
| 52 | +SHOPMON_API_KEY="your-secret-token" ./shopmon-cli deploy -- composer install |
| 53 | +``` |
| 54 | + |
| 55 | +## Telemetry Payload |
| 56 | + |
| 57 | +The deploy command sends the following JSON payload to the monitoring service endpoint (`/trpc/cli.createDeployment`): |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "shop_id": 1, |
| 62 | + "command": "php foo", |
| 63 | + "return_code": 0, |
| 64 | + "start_date": "2024-06-01T12:00:00Z", |
| 65 | + "end_date": "2024-06-01T12:00:01Z", |
| 66 | + "execution_time": 1.23, |
| 67 | + "composer": { |
| 68 | + "php": ">=8.1", |
| 69 | + "shopware/core": "6.5.0.0" |
| 70 | + }, |
| 71 | + "reference": "abc123..." |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +When `SHOPMON_API_KEY` is set, the request includes an `Authorization: Bearer <token>` header for authentication. |
| 76 | + |
| 77 | +## Development |
| 78 | + |
| 79 | +### Project Structure |
| 80 | + |
| 81 | +``` |
| 82 | +shopmon-cli/ |
| 83 | +├── cmd/ # Command definitions |
| 84 | +│ ├── root.go |
| 85 | +│ └── deploy.go |
| 86 | +├── internal/ |
| 87 | +│ └── deployment/ # Core deployment logic |
| 88 | +│ ├── types.go # Data structures |
| 89 | +│ ├── executor.go # Command execution |
| 90 | +│ ├── composer.go # Composer.json parsing |
| 91 | +│ ├── telemetry.go # Telemetry client |
| 92 | +│ └── deployment.go # Main orchestration |
| 93 | +├── main.go |
| 94 | +├── go.mod |
| 95 | +└── go.sum |
| 96 | +``` |
| 97 | + |
| 98 | +### Building |
| 99 | + |
| 100 | +```bash |
| 101 | +# Build the binary |
| 102 | +go build -o shopmon-cli . |
| 103 | + |
| 104 | +# Build for different platforms |
| 105 | +GOOS=linux GOARCH=amd64 go build -o shopmon-cli-linux |
| 106 | +GOOS=darwin GOARCH=amd64 go build -o shopmon-cli-darwin |
| 107 | +GOOS=windows GOARCH=amd64 go build -o shopmon-cli.exe |
| 108 | +``` |
| 109 | + |
| 110 | +## Architecture |
| 111 | + |
| 112 | +1. **cmd/**: CLI command definitions using Cobra |
| 113 | +2. **internal/deployment/**: Core logic split into: |
| 114 | + - **executor.go**: Runs the user's command and captures output, return code, and timing |
| 115 | + - **composer.go**: Reads `composer.json` to extract dependency info |
| 116 | + - **telemetry.go**: Sends deployment data to the monitoring service, uploads compressed output |
| 117 | + - **deployment.go**: Orchestrates the above into a single `Run()` function |
| 118 | + |
| 119 | +### Key Design Decisions |
| 120 | + |
| 121 | +- **Transparent Execution**: Command output is displayed to the user |
| 122 | +- **Error Resilience**: Telemetry failures don't affect command execution |
| 123 | +- **Exit Code Preservation**: The CLI exits with the same code as the executed command |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +MIT |
0 commit comments