Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rogersguedes
Created August 12, 2020 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogersguedes/3b62bd630c5c5546b9e1c35dd8d2f573 to your computer and use it in GitHub Desktop.
Save rogersguedes/3b62bd630c5c5546b9e1c35dd8d2f573 to your computer and use it in GitHub Desktop.
check if each protobuf definition file contains only one definition and its name is same on file name.
#!/bin/bash
find src/ -type f -name *.proto |
while read filePath; do
# do something with the file
fileName=`basename ${filePath}`
echo -n "### checking ${fileName}: "
msgs=""
enuns=""
foundDefs=`grep "message\s\+[a-zA-Z0-9\-\_]\+.*" ${filePath} | sed "s/\s*message\s\+\([a-zA-Z0-9\-\_]\+\).*/\1/"`
if [ -n "${foundDefs}" ]; then
msgs=${foundDefs}
# echo -e "\t${foundDefs}"
fi
foundDefs=`grep "enum\s\+[a-zA-Z0-9\-\_]\+.*" ${filePath} | sed "s/\s*enum\s\+\([a-zA-Z0-9\-\_]\+\).*/\1/"`
if [ -n "${foundDefs}" ]; then
enuns=${foundDefs}
# echo -e "\t${foundDefs}"
fi
# if [ -z "${msgs}" ] && [ -z ${enuns} ]; then
if [ -n "${msgs}" ]; then
if [ -n "${enuns}" ]; then
defType="mixed"
else
defType="msg"
defName=${msgs}
fi
else
if [ -n "${enuns}" ]; then
defType="enum"
defName=${enuns}
else
defType="empty"
fi
fi
errorFound=""
if [ ${defType} == "empty" ]; then
errorFound="${errorFound},empty"
fi
if [ ${defType} == "mixed" ]; then
errorFound="${errorFound},mixed"
fi
if [ "${defName}.proto" != "${fileName}" ]; then
errorFound="${errorFound},name unmatched"
fi
if [ -z "${errorFound}" ]; then
echo "OKAY"
else
echo "ERROR => ${errorFound}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment