diff --git a/.travis.yml b/.travis.yml index 85a745db..b4ba64af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.13.x + - 1.16.x notifications: email: false diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ec9d9235..e6873f25 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ First off, thanks for taking the time to contribute! Feel free to make Pull Requ # Requirements -Go >= 1.13.1 +Go >= 1.16.0 # Proccess diff --git a/cmd/add.go b/cmd/add.go index aca4303b..3ea8590d 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -57,41 +57,6 @@ func addHook(hookName string, fs afero.Fs) { VerbosePrint("Skip adding, because that name unavailable: ", hookName) return } - // TODO: text/template - template := `#!/bin/sh - -if [ "$LEFTHOOK" = "0" ]; then - exit 0 -fi - -if [ -t 1 ] ; then - exec < /dev/tty ; # <- enables interactive shell -fi - -dir="$(cd "$(dirname "$(dirname "$(dirname "$0")")")" >/dev/null 2>&1 || exit ; pwd -P)" - -` + autoInstall(hookName, fs) + "\n" + "cmd=\"lefthook run " + hookName + " $@\"" + - ` - -if lefthook -h >/dev/null 2>&1 -then - eval $cmd -elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" -then - eval $dir/node_modules/@arkweid/lefthook/bin/$cmd -elif bundle exec lefthook -h >/dev/null 2>&1 -then - bundle exec $cmd -elif npx lefthook -h >/dev/null 2>&1 -then - npx $cmd -elif yarn lefthook -h >/dev/null 2>&1 -then - yarn $cmd -else - echo "Can't find lefthook in PATH" -fi -` pathToFile := filepath.Join(getGitHooksPath(), hookName) @@ -109,36 +74,12 @@ fi } } - err := afero.WriteFile(fs, pathToFile, []byte(template), defaultFilePermission) + template := hookTemplate(hookName, fs) + err := afero.WriteFile(fs, pathToFile, template, defaultFilePermission) check(err) VerbosePrint("Added hook: ", pathToFile) } -func autoInstall(hookName string, fs afero.Fs) string { - if hookName == checkSumHook { - return "\n# lefthook_version: " + configChecksum(fs) + "\n\n" + - "cmd=\"lefthook install\"" + - ` - -if lefthook -h >/dev/null 2>&1 -then - eval $cmd -elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" -then - eval $dir/node_modules/@arkweid/lefthook/bin/$cmd -elif bundle exec lefthook -h >/dev/null 2>&1 -then - bundle exec $cmd -elif npx lefthook -h >/dev/null 2>&1 -then - npx $cmd -fi -` - } - - return "" -} - func addProjectHookDir(hookName string, fs afero.Fs) { err := fs.MkdirAll(filepath.Join(getSourceDir(), hookName), defaultFilePermission) check(err) diff --git a/cmd/install.go b/cmd/install.go index 821c73ea..d9a403ae 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -71,43 +71,7 @@ func InstallCmdExecutor(args []string, fs afero.Fs) { // AddConfigYaml write lefthook.yml in root project directory func AddConfigYaml(fs afero.Fs) { - template := `# EXAMPLE USAGE -# Refer for explanation to following link: -# https://github.com/evilmartians/lefthook/blob/master/docs/full_guide.md -# -# pre-push: -# commands: -# packages-audit: -# tags: frontend security -# run: yarn audit -# gems-audit: -# tags: backend security -# run: bundle audit -# -# pre-commit: -# parallel: true -# commands: -# eslint: -# glob: "*.{js,ts}" -# run: yarn eslint {staged_files} -# rubocop: -# tags: backend style -# glob: "*.rb" -# exclude: "application.rb|routes.rb" -# run: bundle exec rubocop --force-exclusion {all_files} -# govet: -# tags: backend style -# files: git ls-files -m -# glob: "*.go" -# run: go vet {files} -# scripts: -# "hello.js": -# runner: node -# "any.go": -# runner: go run -` - - err := afero.WriteFile(fs, getConfigYamlPath(), []byte(template), defaultDirPermission) + err := afero.WriteFile(fs, getConfigYamlPath(), configTemplate(), defaultDirPermission) check(err) log.Println("Added config: ", getConfigYamlPath()) } diff --git a/cmd/templates.go b/cmd/templates.go new file mode 100644 index 00000000..5540dfe6 --- /dev/null +++ b/cmd/templates.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "bytes" + "embed" + "text/template" + + "github.com/spf13/afero" +) + +//go:embed templates/* +var templatesFS embed.FS + +type hookTmplData struct { + AutoInstall string + HookName string +} + +func hookTemplate(hookName string, fs afero.Fs) []byte { + buf := &bytes.Buffer{} + t := template.Must(template.ParseFS(templatesFS, "templates/hook.tmpl")) + err := t.Execute(buf, hookTmplData{ + AutoInstall: autoInstall(hookName, fs), + HookName: hookName, + }) + check(err) + + return buf.Bytes() +} + +func configTemplate() []byte { + tmpl, err := templatesFS.ReadFile("templates/config.tmpl") + check(err) + + return tmpl +} + +func autoInstall(hookName string, fs afero.Fs) string { + if hookName != checkSumHook { + return "" + } + + return "# lefthook_version: " + configChecksum(fs) + "\n\ncall_lefthook \"lefthook install\"" +} diff --git a/cmd/templates/config.tmpl b/cmd/templates/config.tmpl new file mode 100644 index 00000000..af073d45 --- /dev/null +++ b/cmd/templates/config.tmpl @@ -0,0 +1,34 @@ +# EXAMPLE USAGE +# Refer for explanation to following link: +# https://github.com/evilmartians/lefthook/blob/master/docs/full_guide.md +# +# pre-push: +# commands: +# packages-audit: +# tags: frontend security +# run: yarn audit +# gems-audit: +# tags: backend security +# run: bundle audit +# +# pre-commit: +# parallel: true +# commands: +# eslint: +# glob: "*.{js,ts}" +# run: yarn eslint {staged_files} +# rubocop: +# tags: backend style +# glob: "*.rb" +# exclude: "application.rb|routes.rb" +# run: bundle exec rubocop --force-exclusion {all_files} +# govet: +# tags: backend style +# files: git ls-files -m +# glob: "*.go" +# run: go vet {files} +# scripts: +# "hello.js": +# runner: node +# "any.go": +# runner: go run diff --git a/cmd/templates/hook.tmpl b/cmd/templates/hook.tmpl new file mode 100644 index 00000000..4648b028 --- /dev/null +++ b/cmd/templates/hook.tmpl @@ -0,0 +1,37 @@ +#!/bin/sh + +if [ "$LEFTHOOK" = "0" ]; then + exit 0 +fi + +if [ -t 1 ] ; then + exec < /dev/tty ; # <- enables interactive shell +fi + +dir="$(cd "$(dirname "$(dirname "$(dirname "$0")")")" >/dev/null 2>&1 || exit ; pwd -P)" + +call_lefthook() +{ + if lefthook -h >/dev/null 2>&1 + then + eval $1 + elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" + then + eval $dir/node_modules/@arkweid/lefthook/bin/$1 + elif bundle exec lefthook -h >/dev/null 2>&1 + then + bundle exec $1 + elif npx lefthook -h >/dev/null 2>&1 + then + npx $1 + elif yarn lefthook -h >/dev/null 2>&1 + then + yarn $1 + else + echo "Can't find lefthook in PATH" + fi +} + +{{.AutoInstall}} + +call_lefthook "lefthook run {{.HookName}} $@" diff --git a/go.mod b/go.mod index c4254cf0..edefca89 100644 --- a/go.mod +++ b/go.mod @@ -16,4 +16,4 @@ require ( gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61 ) -go 1.13 +go 1.16 diff --git a/spec/fixtures/pre-commit b/spec/fixtures/pre-commit index 7ff6ddee..3d435141 100644 --- a/spec/fixtures/pre-commit +++ b/spec/fixtures/pre-commit @@ -10,24 +10,28 @@ fi dir="$(cd "$(dirname "$(dirname "$(dirname "$0")")")" >/dev/null 2>&1 || exit ; pwd -P)" +call_lefthook() +{ + if lefthook -h >/dev/null 2>&1 + then + eval $1 + elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" + then + eval $dir/node_modules/@arkweid/lefthook/bin/$1 + elif bundle exec lefthook -h >/dev/null 2>&1 + then + bundle exec $1 + elif npx lefthook -h >/dev/null 2>&1 + then + npx $1 + elif yarn lefthook -h >/dev/null 2>&1 + then + yarn $1 + else + echo "Can't find lefthook in PATH" + fi +} -cmd="lefthook run pre-commit $@" -if lefthook -h >/dev/null 2>&1 -then - eval $cmd -elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" -then - eval $dir/node_modules/@arkweid/lefthook/bin/$cmd -elif bundle exec lefthook -h >/dev/null 2>&1 -then - bundle exec $cmd -elif npx lefthook -h >/dev/null 2>&1 -then - npx $cmd -elif yarn lefthook -h >/dev/null 2>&1 -then - yarn $cmd -else - echo "Can't find lefthook in PATH" -fi + +call_lefthook "lefthook run pre-commit $@" diff --git a/spec/fixtures/pre-push b/spec/fixtures/pre-push index 8765c9ad..34d0b46b 100644 --- a/spec/fixtures/pre-push +++ b/spec/fixtures/pre-push @@ -10,24 +10,28 @@ fi dir="$(cd "$(dirname "$(dirname "$(dirname "$0")")")" >/dev/null 2>&1 || exit ; pwd -P)" +call_lefthook() +{ + if lefthook -h >/dev/null 2>&1 + then + eval $1 + elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" + then + eval $dir/node_modules/@arkweid/lefthook/bin/$1 + elif bundle exec lefthook -h >/dev/null 2>&1 + then + bundle exec $1 + elif npx lefthook -h >/dev/null 2>&1 + then + npx $1 + elif yarn lefthook -h >/dev/null 2>&1 + then + yarn $1 + else + echo "Can't find lefthook in PATH" + fi +} -cmd="lefthook run pre-push $@" -if lefthook -h >/dev/null 2>&1 -then - eval $cmd -elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook" -then - eval $dir/node_modules/@arkweid/lefthook/bin/$cmd -elif bundle exec lefthook -h >/dev/null 2>&1 -then - bundle exec $cmd -elif npx lefthook -h >/dev/null 2>&1 -then - npx $cmd -elif yarn lefthook -h >/dev/null 2>&1 -then - yarn $cmd -else - echo "Can't find lefthook in PATH" -fi + +call_lefthook "lefthook run pre-push $@"