Skip to content

Commit

Permalink
Extract templates
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed May 11, 2021
1 parent b510d22 commit be0bf50
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.13.x
- 1.16.x

notifications:
email: false
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 2 additions & 61 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
38 changes: 1 addition & 37 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
44 changes: 44 additions & 0 deletions cmd/templates.go
Original file line number Diff line number Diff line change
@@ -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\""
}
34 changes: 34 additions & 0 deletions cmd/templates/config.tmpl
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions cmd/templates/hook.tmpl
Original file line number Diff line number Diff line change
@@ -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}} $@"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ require (
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
)

go 1.13
go 1.16
42 changes: 23 additions & 19 deletions spec/fixtures/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@"
42 changes: 23 additions & 19 deletions spec/fixtures/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@"

0 comments on commit be0bf50

Please sign in to comment.