DEV Community

devnull03
devnull03

Posted on

Get Started with Husky for go

Git hooks

Before understanding what husky is you should have some familiarity with git hooks. As you may know, git is a version control system that can be customized, one way of customizing git is using hooks. Hooks are custom scripts that can be used to run certain commands which can be triggered by events in git. Some examples of hooks are pre-commit, post commit, pre-receive and so on.

Why would I use them?

Hooks are useful since you can write certain scripts that can lint your code, test it automatically before pushing it to GitHub to make sure nothing breaks and many other things, you creativity is your limit in this case.

Husky

Well, hooks seem great and all but how do I use them? You can use them the complicated way with the tools git provides out of the box, or you can use a package like husky to let it do all the complicated work for you.

Setup

To use it in your go project you first need to install like any other package using the command below

$ go install github.com/automation-co/husky@latest
Enter fullscreen mode Exit fullscreen mode

After installing you can initialize husky by running husky init

After running that command you’ll see a new folder named .husky in your workspace with a sample hook inside it. The name of the file represents which event triggers the hook, in this case you’ll see a file named pre-commit the commands in this file run whenever you commit anything.

Adding some hooks

Now that we have setup husky to make adding hooks easier, the next thing to do is utilizing it.

As an example, we are going to setup a hooks that will lint and format our code before committing it.

For this example we are gonna use golangci-lint as the linter so go ahead and install it using the instructions here. After installing, all we need to do is open the sample hook file in .husky/hooks named pre-commit and add the command for running the linter. It should look something like this

#!/bin/sh

golangci-lint run
Enter fullscreen mode Exit fullscreen mode

or add the hook using the command

$ husky add pre-commit "golangci-lint run"
Enter fullscreen mode Exit fullscreen mode

But before linting our code we also want to format it, so go ahead and add the command for any formatter you use in the file before the lint command. For the purpose of this example we are going to use the built in formatter gofmt .

#!/bin/sh

go fmt .
golangci-lint run
Enter fullscreen mode Exit fullscreen mode

or

$ husky add pre-commit "
            go fmt .
            golangci-lint run
    "
Enter fullscreen mode Exit fullscreen mode

And with that we are all done. Now if you try and commit anything, git will run the two commands we added before committing any file. This will help you find any errors and format your code to look pretty.

Conclusion

Git hooks are a very powerful thing and husky can help us use them with little to no friction. In this article you learnt how to make a basic hook to lint and format your code automatically before committing. But that is not the only thing hooks are limited to you can make anything using the knowledge you gained in this article and apply it to make your own custom hooks that help achieve whatever you need them to.

Top comments (0)