From b94f8d2f9dbe1bf2810fe71a984e41b619b201c5 Mon Sep 17 00:00:00 2001 From: Christopher Black Date: Wed, 16 May 2018 11:07:40 -0700 Subject: [PATCH] Add goimports hook --- .pre-commit-hooks.yaml | 8 ++++++++ hooks/goimports.sh | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100755 hooks/goimports.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index ceede25c..d47c2fcd 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -22,6 +22,14 @@ files: \.go$ exclude: vendor\/.*$ +- id: goimports + name: goimports + description: Goimports updates imports and formats in the same style as gofmt + entry: hooks/goimports.sh + language: script + files: \.go$ + exclude: vendor\/.*$ + - id: golint name: golint description: Golint is a linter for Go source code. diff --git a/hooks/goimports.sh b/hooks/goimports.sh new file mode 100755 index 00000000..1e77fb96 --- /dev/null +++ b/hooks/goimports.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +# OSX GUI apps do not pick up environment variables the same way as Terminal apps and there are no easy solutions, +# especially as Apple changes the GUI app behavior every release (see https://stackoverflow.com/q/135688/483528). As a +# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here. +export PATH=$PATH:/usr/local/bin + +for file in "$@"; do + goimports -l -w "$(dirname "$file")" +done