Skip to content

Commit

Permalink
Merge pull request #6 from mehulgohil/remove_overwrite
Browse files Browse the repository at this point in the history
added logic to append the script to existing pre-commit
  • Loading branch information
acaloiaro authored Jun 13, 2023
2 parents b150950 + 72249a0 commit 45dbdc7
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -206,14 +205,14 @@ func gitDirPaths() (projectPath, gitDir string) {
return
}

var preCommitScriptTemplate = `#!/usr/bin/env bash
var preCommitScriptTemplate = `{{if .Appending}}{{ .OldPreCommitScript }}
# Below code generated by env-sample-sync https://github.com/acaloiaro/env-sample-sync
{{else}}#!/usr/bin/env bash
# File generated by env-sample-sync https://github.com/acaloiaro/env-sample-sync
ARGS=({{ .Args }})
{{end}}ARGS=({{ .Args }})
for SCRIPT in $(dirname -- "${BASH_SOURCE[0]}")/{{ .PreCommitHooksDir }}/*; do
exec "$SCRIPT" "${ARGS[@]}"
done
exec $(dirname -- "${BASH_SOURCE[0]}")/{{ .PreCommitHooksDir }}/0-env-sample-sync "${ARGS[@]}"
`

// installHook installs the env-sample-sync git hook to 'dir'
Expand All @@ -222,15 +221,26 @@ func installHook(gitDirPath string) (err error) {
hooksDirName := filepath.Join(gitDirPath, "hooks")
hooksScriptDirPath := filepath.Join(hooksDirName, hooksScriptDirName)
preCommitHookScriptPath := filepath.Join(hooksDirName, "pre-commit")
appendFlag := false
var oldPreCommitScript []byte

_, err = os.Stat(preCommitHookScriptPath)
if !os.IsNotExist(err) {
fmt.Printf("a pre-commit hook is already installed. Overwrite? [y/n]: ")
fmt.Printf("A pre-commit hook already exists. Would you like to cancel [c], overwrite [o], or append [a] the existing pre-commit hook script? [a, c, o]: ")
var response string
fmt.Scanln(&response)
if response != "y" {
if response == "c" || (response != "o" && response != "a") {
os.Exit(0)
}
if response == "a" {
// read the existing content of the pre-commit file
oldPreCommitScript, err = os.ReadFile(preCommitHookScriptPath)
if err != nil {
return err
}

appendFlag = true
}
}

// Create a directory to place pre-commit-hook executable within
Expand All @@ -243,8 +253,7 @@ func installHook(gitDirPath string) (err error) {
preCommitHookPath := filepath.Join(hooksScriptDirPath, "0-env-sample-sync")
preCommitHook, err := os.OpenFile(preCommitHookPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
fmt.Println("unable to install pre-commit hook", err)
os.Exit(1)
return
}

executablePath, err := exec.LookPath(os.Args[0])
Expand All @@ -263,29 +272,39 @@ func installHook(gitDirPath string) (err error) {

tmpl, err := template.New("pre-commit-script").Parse(preCommitScriptTemplate)
if err != nil {
log.Println("unable to parse pre-commit script template:", err)
os.Exit(1)
return err
}

args := fmt.Sprintf("--env-file=%s --sample-file=%s %s", envFileFlag, sampleFileFlag, examplesFlag)
var buff = bytes.NewBufferString("")
err = tmpl.Execute(buff, map[string]string{
"Args": args,
"PreCommitHooksDir": hooksScriptDirName,
})

templateValues := struct {
Args string
PreCommitHooksDir string
Appending bool
OldPreCommitScript string
}{
Args: args,
PreCommitHooksDir: hooksScriptDirName,
Appending: appendFlag,
OldPreCommitScript: string(oldPreCommitScript),
}

err = tmpl.Execute(buff, templateValues)
if err != nil {
return
return err
}

preCommitHookScript, err := os.OpenFile(preCommitHookScriptPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return
return err
}
defer preCommitHookScript.Close()

_, err = buff.WriteTo(preCommitHookScript)
if err != nil {
return
return err
}

fmt.Println("env-sample-sync pre-commit hook installed!")
return
return nil
}

0 comments on commit 45dbdc7

Please sign in to comment.