Last active
January 28, 2022 09:35
-
-
Save nishidy/6bf8b35c63d7c0773e238ad9c57f85d1 to your computer and use it in GitHub Desktop.
To convert function name from snake case to camel case or pascal case in go on OS X with bash 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [[ $# -eq 1 ]] ;then | |
case $1 in | |
-c) option="camel" ;; | |
-p) option="pascal" ;; | |
*) echo "This option $1 is not supported."; exit 1 ;; | |
esac | |
else | |
echo "Give an option -c (Camel case) or -p (Pascal case)." | |
exit -1 | |
fi | |
for f in $(find . -name "*.go"); do | |
for g in $(grep -h ^func $f | sed -n 's/func \([^()]*\) *(.*/\1/p' | grep _ ) $(grep -h ^func $f | sed -n 's/func (.*) \([^()]*\) *(.*/\1/p' | grep _ ); do | |
snake=$g | |
if [[ $option = "camel" ]]; then | |
tocase=$(echo $g | perl -pe "s/_(.)/\u\1/g") | |
elif [[ $option = "pascal" ]]; then | |
tocase=$(echo $g | perl -pe "s/_(.)/\u\1/g; s/^(.)/\u\1/g") | |
fi | |
echo "$snake -> $tocase" | |
for h in $(find . -name "*.go"); do | |
# BSD sed | |
sed -i "" "s/$snake/$tocase/g" $h | |
done | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment