This repository has no description
1#!/bin/bash
2
3# Exit on any error
4set -e
5
6# Function to extract event constants from Go file within the same const block
7extract_events() {
8 local file="$1"
9 # Create a temporary file to store the const block
10 local tempfile=$(mktemp)
11
12 # Extract the const block containing Event definitions
13 awk '/^const \($/ {p=1;next} /^\)$/ {p=0} p' "$file" > "$tempfile"
14
15 # Extract event values from the const block
16 grep -o 'Event = "[^"]*"' "$tempfile" | cut -d'"' -f2 | tr '\n' '=' | sed 's/=$//' | sed 's/=/,enum=/g'
17
18 # Clean up
19 rm "$tempfile"
20}
21
22# Function to update JSON schema in Go file
23update_schema() {
24 local file="$1"
25 local events="$2"
26
27 # Create temporary file
28 local tempfile=$(mktemp)
29
30 # Flag to track if we found the generate comment
31 local next_line_update=0
32
33 # Process the file line by line
34 while IFS= read -r line; do
35 if [ $next_line_update -eq 1 ]; then
36 # Update the line after the comment
37 echo "$line" | sed "s/jsonschema:\"enum=[^\"]*\"/jsonschema:\"enum=$events\"/" >> "$tempfile"
38 next_line_update=0
39 elif [[ $line =~ "next-line-generate event-enum-jsonschema-values" ]]; then
40 # Mark the next line for update
41 echo "$line" >> "$tempfile"
42 next_line_update=1
43 else
44 echo "$line" >> "$tempfile"
45 fi
46 done < "$file"
47
48 # Replace original file with updated content
49 mv "$tempfile" "$file"
50}
51
52# Main script
53main() {
54 local file="${1:-events.go}" # Default to events.go if no file specified
55
56 if [ ! -f "$file" ]; then
57 echo "Error: File $file not found"
58 exit 1
59 fi
60
61 echo "Processing $file..."
62
63 # Extract events and update schema
64 local events=$(extract_events "$file")
65
66 if [ -z "$events" ]; then
67 echo "Error: No events found in const block"
68 exit 1
69 fi
70
71 update_schema "$file" "$events"
72
73 echo "Successfully updated JSON schema enum values"
74 echo "New values: $events"
75}
76
77# Run the script
78main "$@"