#!/usr/bin/env bash # Script to generate Go code from protobuf definitions set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" PROTO_DIR="$PROJECT_ROOT/pkg/proto" OUT_DIR="$PROJECT_ROOT/pkg/proto/gen" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}Generating Go code from protobuf definitions...${NC}" # Check for required tools check_tool() { if ! command -v "$1" &> /dev/null; then echo -e "${RED}Error: $1 is not installed${NC}" echo "Install with:" echo " $2" exit 1 fi } check_tool "protoc" "brew install protobuf (macOS) or apt install protobuf-compiler (Linux)" check_tool "protoc-gen-go" "go install google.golang.org/protobuf/cmd/protoc-gen-go@latest" check_tool "protoc-gen-go-grpc" "go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest" # Create output directory mkdir -p "$OUT_DIR" # Generate Go code for each proto file for proto_file in "$PROTO_DIR"/*.proto; do if [ -f "$proto_file" ]; then filename=$(basename "$proto_file" .proto) echo -e "${YELLOW}Processing $filename.proto...${NC}" protoc \ --proto_path="$PROTO_DIR" \ --go_out="$OUT_DIR" \ --go_opt=paths=source_relative \ --go-grpc_out="$OUT_DIR" \ --go-grpc_opt=paths=source_relative \ "$proto_file" echo -e "${GREEN}✓ Generated $filename.pb.go and ${filename}_grpc.pb.go${NC}" fi done echo -e "${GREEN}Done! Generated files are in $OUT_DIR${NC}" # List generated files echo "" echo "Generated files:" ls -la "$OUT_DIR"/*.go 2>/dev/null || echo "No files generated yet"