Skip to content

Instantly share code, notes, and snippets.

@nishidy
Last active January 28, 2022 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nishidy/6bf8b35c63d7c0773e238ad9c57f85d1 to your computer and use it in GitHub Desktop.
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
#!/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