forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first class support for golang hooks
- Loading branch information
Showing
11 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from __future__ import unicode_literals | ||
|
||
import contextlib | ||
import os.path | ||
|
||
from pre_commit import git | ||
from pre_commit.envcontext import envcontext | ||
from pre_commit.envcontext import Var | ||
from pre_commit.languages import helpers | ||
from pre_commit.util import clean_path_on_failure | ||
from pre_commit.util import cmd_output | ||
from pre_commit.xargs import xargs | ||
|
||
|
||
ENVIRONMENT_DIR = 'golangenv' | ||
|
||
|
||
def get_env_patch(venv): # pragma: windows no cover | ||
return ( | ||
('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))), | ||
) | ||
|
||
|
||
@contextlib.contextmanager | ||
def in_env(repo_cmd_runner): # pragma: windows no cover | ||
envdir = repo_cmd_runner.path( | ||
helpers.environment_dir(ENVIRONMENT_DIR, 'default'), | ||
) | ||
with envcontext(get_env_patch(envdir)): | ||
yield | ||
|
||
|
||
def guess_go_dir(remote_url): | ||
if remote_url.endswith('.git'): | ||
remote_url = remote_url[:-1 * len('.git')] | ||
remote_url = remote_url.replace(':', '/') | ||
looks_like_url = '//' in remote_url or '@' in remote_url | ||
if looks_like_url: | ||
_, _, remote_url = remote_url.rpartition('//') | ||
_, _, remote_url = remote_url.rpartition('@') | ||
return remote_url | ||
else: | ||
return 'unknown_src_dir' | ||
|
||
|
||
def install_environment( | ||
repo_cmd_runner, | ||
version='default', | ||
additional_dependencies=(), | ||
): # pragma: windows no cover | ||
helpers.assert_version_default('golang', version) | ||
helpers.assert_no_additional_deps('golang', additional_dependencies) | ||
directory = repo_cmd_runner.path( | ||
helpers.environment_dir(ENVIRONMENT_DIR, 'default'), | ||
) | ||
|
||
with clean_path_on_failure(directory): | ||
remote = git.get_remote_url(repo_cmd_runner.path()) | ||
repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote)) | ||
|
||
# Clone into the goenv we'll create | ||
helpers.run_setup_cmd( | ||
repo_cmd_runner, ('git', 'clone', '.', repo_src_dir), | ||
) | ||
|
||
env = dict(os.environ, GOPATH=directory) | ||
cmd_output('go', 'get', './...', cwd=repo_src_dir, env=env) | ||
|
||
|
||
def run_hook(repo_cmd_runner, hook, file_args): # pragma: windows no cover | ||
with in_env(repo_cmd_runner): | ||
return xargs(helpers.to_cmd(hook), file_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- id: golang-hook | ||
name: golang example hook | ||
entry: golang-hello-world | ||
language: golang | ||
files: '' |
17 changes: 17 additions & 0 deletions
17
testing/resources/golang_hooks_repo/golang-hello-world/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
|
||
import ( | ||
"fmt" | ||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
type Config struct { | ||
What string | ||
} | ||
|
||
func main() { | ||
var conf Config | ||
toml.Decode("What = 'world'\n", &conf) | ||
fmt.Printf("hello %v\n", conf.What) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
|
||
import pytest | ||
|
||
from pre_commit.languages.golang import guess_go_dir | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('url', 'expected'), | ||
( | ||
('/im/a/path/on/disk', 'unknown_src_dir'), | ||
('[email protected]:golang/lint', 'github.com/golang/lint'), | ||
('git://github.com/golang/lint', 'github.com/golang/lint'), | ||
('http://github.com/golang/lint', 'github.com/golang/lint'), | ||
('https://github.com/golang/lint', 'github.com/golang/lint'), | ||
('ssh://[email protected]/golang/lint', 'github.com/golang/lint'), | ||
('[email protected]:golang/lint.git', 'github.com/golang/lint'), | ||
), | ||
) | ||
def test_guess_go_dir(url, expected): | ||
assert guess_go_dir(url) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters