1+ #! /bin/bash
2+
3+ # Exit on any error
4+ set -e
5+
6+ # Script usage
7+ usage () {
8+ echo " Usage: $0 <cosmos-sdk-path> <injective-core-path>"
9+ echo
10+ echo " Extract error definitions from Cosmos SDK and Injective Core repositories"
11+ echo
12+ echo " Arguments:"
13+ echo " cosmos-sdk-path Path to the Cosmos SDK repository"
14+ echo " injective-core-path Path to the Injective Core repository"
15+ echo
16+ echo " Example:"
17+ echo " $0 /tmp/cosmos-sdk /tmp/injective-core"
18+ exit 1
19+ }
20+
21+ # Check arguments
22+ if [ $# -ne 2 ]; then
23+ usage
24+ fi
25+
26+ # Configuration
27+ COSMOS_SDK_DIR=" $1 "
28+ INJECTIVE_CORE_DIR=" $2 "
29+ BASE_OUTPUT_DIR=" source/json_tables"
30+
31+ # Store the original directory
32+ ORIGINAL_DIR=$( pwd)
33+
34+ # Check dependencies
35+ if ! command -v jq & > /dev/null; then
36+ echo " Error: jq is required but not installed. Please install jq first."
37+ exit 1
38+ fi
39+
40+ # Function to extract errors from a file and append to JSON array
41+ extract_errors () {
42+ local file=$1
43+ local json_objects=" "
44+ local error_pattern=' errors\.Register\([[:space:]]*([^,]+)[[:space:]]*,[[:space:]]*([^,]+)[[:space:]]*,[[:space:]]*\"([^\"]+)\"'
45+
46+ while IFS= read -r line; do
47+ if [[ $line =~ $error_pattern ]]; then
48+ error_code=$( echo " ${BASH_REMATCH[2]} " | tr -d ' ' )
49+ description=" ${BASH_REMATCH[3]} "
50+
51+ if [ -z " $json_objects " ]; then
52+ json_objects=" {\" Error Code\" : $error_code , \" Description\" : \" $description \" }"
53+ else
54+ json_objects=" $json_objects ,{\" Error Code\" : $error_code , \" Description\" : \" $description \" }"
55+ fi
56+ fi
57+ done < " $file "
58+
59+ if [ -n " $json_objects " ]; then
60+ echo " [$json_objects ]" | jq ' .'
61+ return 0
62+ fi
63+ return 1
64+ }
65+
66+ # Function to process modules in a directory
67+ process_modules () {
68+ local base_dir=$1
69+ local output_dir=$2
70+ local modules_path=$3
71+
72+ echo " Processing modules in $modules_path ..."
73+
74+ cd " $base_dir "
75+ for module in " $modules_path " /* ; do
76+ # Skip if not a directory
77+ [ ! -d " $module " ] && continue
78+
79+ module_name=$( basename " $module " )
80+ echo " Processing module: $module_name "
81+
82+ json_content=" "
83+
84+ # Check for errors.go in main folder
85+ if [ -f " $module /errors.go" ] && content=$( extract_errors " $module /errors.go" ) ; then
86+ json_content=" $content "
87+ fi
88+
89+ # Check for errors.go in types subfolder
90+ if [ -f " $module /types/errors.go" ] && types_content=$( extract_errors " $module /types/errors.go" ) ; then
91+ if [ -n " $json_content " ]; then
92+ json_content=$( echo " $json_content " " $types_content " | jq -s ' add' )
93+ else
94+ json_content=" $types_content "
95+ fi
96+ fi
97+
98+ # Save if we found any errors
99+ if [ -n " $json_content " ]; then
100+ echo " $json_content " > " $ORIGINAL_DIR /$output_dir /$module_name .json"
101+ echo " Found errors in module $module_name "
102+ else
103+ echo " No errors found in module $module_name "
104+ fi
105+ done
106+ cd " $ORIGINAL_DIR "
107+ }
108+
109+ # Clean up any existing files and create directory structure
110+ echo " Setting up directories..."
111+ rm -rf " $BASE_OUTPUT_DIR /errors" " $BASE_OUTPUT_DIR /chain/errors"
112+ mkdir -p " $BASE_OUTPUT_DIR /errors" " $BASE_OUTPUT_DIR /chain/errors"
113+
114+ # Process Cosmos SDK repository
115+ echo " Processing Cosmos SDK repository..."
116+ process_modules " $COSMOS_SDK_DIR " " $BASE_OUTPUT_DIR /errors" " x"
117+
118+ # Process Injective Core repository
119+ echo " Processing Injective Core repository..."
120+ process_modules " $INJECTIVE_CORE_DIR " " $BASE_OUTPUT_DIR /chain/errors" " injective-chain/modules"
121+
122+ echo " Error extraction complete. JSON files have been created in:"
123+ echo " - $BASE_OUTPUT_DIR /errors (Cosmos SDK modules)"
124+ echo " - $BASE_OUTPUT_DIR /chain/errors (Injective Core modules)"
0 commit comments