File size: 1,761 Bytes
4b1daed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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"