Real-World Project - Building a CLI Tool
The Script That Grew Into a Production Tool
#!/bin/bash
# deploy.sh
kubectl config use-context production
docker build -t myapp:$VERSION .
docker push myapp:$VERSION
kubectl set image deployment/myapp myapp=myapp:$VERSION
kubectl rollout status deployment/myapp# New workflow
deploy --env=staging --version=v1.2.3 --wait
# Output:
β Building Docker image myapp:v1.2.3
β Pushing to registry
β Deploying to staging
β Waiting for rollout... (45s)
β Running health checks
β Deployment successful!
Deployed by: [email protected]
Deploy time: 2024-01-15 14:23:45 UTCWhy Go for CLI Tools?
Single Binary
Cross-Compilation
Fast Startup
Building CLI with Cobra
Installation
Basic CLI
Adding a Command
Adding Flags
Configuration with Viper
Installation
Basic Configuration
Environment Variables
Combining Cobra and Viper
Structured Logging
Using logrus
Using zap (High Performance)
Building the Binary
Basic Build
Optimized Build
Cross-Compilation
Build Script
Distributing Binaries
GitHub Releases
Using GoReleaser
Homebrew (macOS/Linux)
Real Example: Deployment Tool
Your Challenge
Key Takeaways
What I Learned
Last updated