Skip to content

Instantly share code, notes, and snippets.

@Peltoche
Last active September 19, 2022 09:48
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 Peltoche/60b8b81dfbf70164d0e2b88988003229 to your computer and use it in GitHub Desktop.
Save Peltoche/60b8b81dfbf70164d0e2b88988003229 to your computer and use it in GitHub Desktop.
Go 1.13 error format migration script
#!/bin/bash
FILES=$@
echo "Migrate: $FILES"
sed -i "s/errors.Wrapf(\(.*\), \"\(.*\)\", \(.*\))/fmt.Errorf(\"\2: %w\", \3, \1)/g" $FILES
sed -i "s/errors.Wrap(\(.*\), \"\(.*\)\")/fmt.Errorf(\"\2: %w\", \1)/g" $FILES
sed -i "s/errors.New(\"\(.*\)\")/fmt.Errorf(\"\1\")/g" $FILES
sed -i "s/errors.Errorf/fmt.Errorf/g" $FILES
goimports -w .
@lbcjbb
Copy link

lbcjbb commented Sep 19, 2022

sed -ri -e 's/errors\.Wrapf\((.*), "(.*)", (.*)\)/fmt.Errorf("\2: %w", \3, \1)/g' \
        -e 's/errors\.Wrap\((.*), "(.*)"\)/fmt.Errorf("\2: %w", \1)/g' \
        -e 's/errors\.New\("(.*)"\)/fmt.Errorf("\1")/g' \
        -e 's/errors\.Errorf/fmt.Errorf/g' \
        "$@" && \
goimports -w "$@"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment