pre-commit-ci / issues

public issues for https://pre-commit.ci
17 stars 4 forks source link

Creating a hook from Go code #71

Closed nightlark closed 3 years ago

nightlark commented 3 years ago

We have been using https://github.com/jumanjihouse/pre-commit-hooks running on GitHub Actions using pre-commit/action for the shellcheck and shfmt hooks. As part of seeing if pre-commit.ci could work we ran into an issue with both of those hooks failing because shellcheck and shfmt are not installed on the runner image (would be nice if they were added to the default image... but I guess that could get hard to manage).

I noticed a previous issue mentioned https://github.com/shellcheck-py/shellcheck-py as an alternate option that can install a copy of shellcheck through pip, but I haven't found an equivalent for shfmt (https://github.com/mvdan/sh) -- how would I go about creating a pre-commit.ci compatible hook? Is there a Go specific way of saying "build this" from source, or would I have to bundle it up into a PyPI package that can be pip installed?

asottile commented 3 years ago

for shellcheck you're looking for https://github.com/shellcheck-py/shellcheck-py

for shfmt it should just work -- pre-commit and pre-commit.ci both have native support for go

nightlark commented 3 years ago

Thanks, I got shfmt working as a local hook -- I think I had a syntax error the first time I tried, but 2nd try this worked:

  - repo: local
    hooks:
      - id: shfmt
        name: shfmt
        language: golang
        additional_dependencies: [mvdan.cc/sh/v3/cmd/shfmt@v3.3.0]
        entry: shfmt -w
        types: [shell]
asottile commented 3 years ago

that looks perfect! awesome!

one thing you can do to ~slightly optimize is write entry: shfmt since args doesn't really make too much sense for repo: local when you can just inline it

nightlark commented 3 years ago

Awesome, that's really convenient for getting any Go tools to run -- after splitting the clang-format docker hook out to run on its own from GitHub Actions I think this means all our other checks will be runnable on pre-commit.ci

asottile commented 3 years ago

awesome!