Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement Don't Repeat Yourself principal #20

Merged
merged 5 commits into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ echo 'woof'
hook := args[0]
cmdStr := args[1]

lib.Add(hook, cmdStr)
lib.Install()
if err := lib.Add(hook, cmdStr); err != nil {
panic(err)
}

if err := lib.Install(); err != nil {
panic(err)
}
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ For more information, please visit
https://github.com/automation-co/husky
`,
Run: func(cmd *cobra.Command, args []string) {
lib.Init()
if err := lib.Init(); err != nil {
panic(err)
}
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ var installCmd = &cobra.Command{
Short: "Install hooks",
Long: `Install hooks from the .hooks folder`,
Run: func(cmd *cobra.Command, args []string) {
lib.Install()
if err := lib.Install(); err != nil {
panic(err)
}
},
}

Expand Down
53 changes: 12 additions & 41 deletions internal/lib/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,36 @@ import (
"os"
)

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}

return false
}

func Add(hook string, cmd string) error {

// check if hook name is valid
validHooks := []string{
"applypatch-msg",
"commit-msg",
"fsmonitor-watchman",
"post-checkout",
"post-update",
"pre-applypatch",
"pre-commit",
"pre-push",
"pre-rebase",
"prepare-commit-msg",
"update",
"pre-receive",
"pre-merge-commit",
"push-to-checkout",
}
if !contains(validHooks, hook) {
fmt.Println("Invalid hook name.")
// validate hooks
if !isValidHook(hook) {
return errors.New("invalid hook name")
}

// check if .git exists
_, err := os.Stat(".git")
if os.IsNotExist(err) {
fmt.Println("git not initialized")
if isExists, err := gitExists(); err == nil && !isExists {
return errors.New("git not initialized")
} else if err == nil {
return err
}

// check if .husky exists
_, err = os.Stat(".husky")

if os.IsNotExist(err) {
fmt.Println(".husky not initialized.")
if isExists, err := huskyExists(); err == nil && !isExists {
return errors.New(".husky not initialized")
} else if err != nil {
return err
}

// check if .husky/hooks exists
_, err = os.Stat(".husky/hooks")
_, err := os.Stat(".husky/hooks")

if os.IsNotExist(err) {
fmt.Println("no pre-existing hooks found")

// create .husky/hooks
err = os.Mkdir(".husky/hooks", 0755)
if err != nil {
panic(err)
return err
}

fmt.Println("created .husky/hooks")
Expand All @@ -73,7 +44,7 @@ func Add(hook string, cmd string) error {
// create hook
file, err := os.Create(".husky/hooks/" + hook)
if err != nil {
panic(err)
return err
}

//goland:noinspection GoUnhandledErrorResult
Expand All @@ -82,7 +53,7 @@ func Add(hook string, cmd string) error {
cmd = "#!/bin/sh\n" + cmd
_, err = file.WriteString(cmd)
if err != nil {
panic(err)
return err
}

return nil
Expand Down
26 changes: 11 additions & 15 deletions internal/lib/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,47 @@ package lib

import (
"errors"
"fmt"
"os"
)

func Init() error {

// check if .git exists
_, err := os.Stat(".git")
if os.IsNotExist(err) {
fmt.Println("git not initialized")
if isExists, err := gitExists(); err == nil && !isExists {
return errors.New("git not initialized")
} else if err != nil {
return err
}

// check if .husky exists
_, err = os.Stat(".husky")

if err == nil {
fmt.Println(".husky already exist.")
if isExists, err := huskyExists(); err == nil && isExists {
return errors.New(".husky already exist")
} else if err != nil {
return err
}

// if not, create .husky
err = os.Mkdir(".husky", 0755)
err := os.Mkdir(".husky", 0755)
if err != nil {
panic(err)
return err
}

err = os.Mkdir(".husky/hooks", 0755)
if err != nil {
panic(err)
return err
}

// create default pre-commit hook
file, err := os.Create(".husky/hooks/pre-commit")
if err != nil {
panic(err)
return err
}

//goland:noinspection GoUnhandledErrorResult
defer file.Close()

_, err = file.WriteString(`#!/bin/sh`)

if err != nil {
panic(err)
return err
}

// add hooks to .git/hooks
Expand Down
44 changes: 18 additions & 26 deletions internal/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,64 @@ func Install() error {
fmt.Println("Installing hooks")

// check if .git exists
_, err := os.Stat(".git")
if os.IsNotExist(err) {
fmt.Println("git not initialized")
if isExists, err := gitExists(); err == nil && !isExists {
return errors.New("git not initialized")
} else if err != nil {
return err
}

// check if .husky exists
_, err = os.Stat(".husky")

if os.IsNotExist(err) {
fmt.Println(".husky not initialized.")
if isExists, err := huskyExists(); err == nil && !isExists {
return errors.New(".husky not initialized")
} else if err != nil {
return err
}

// check if .husky/hooks exists
_, err = os.Stat(".husky/hooks")

_, err := os.Stat(".husky/hooks")
if os.IsNotExist(err) {
fmt.Println("no hooks found")
return errors.New("no hooks found")
}

root := ".husky/hooks"
gitDir := ".git/hooks"

// delete all files in .git/hooks
err = os.RemoveAll(gitDir)
if err != nil {
panic(err)
if err := os.RemoveAll(gitHooksDir); err != nil {
return err
}

// create .git/hooks
err = os.Mkdir(gitDir, 0755)
if err != nil {
panic(err)
if err := os.Mkdir(gitHooksDir, 0755); err != nil {
return err
}

// copy all files in .husky/hooks to .git/hooks
var hooks []string
err = filepath.Walk(root,
err = filepath.Walk(huskyHooksDir,
func(path string, info os.FileInfo, err error) error {
hooks = append(hooks, path)
return nil
})
if err != nil {
panic(err)
return err
}
for _, hook := range hooks {

// skip .husky/hooks
if hook == root {
if hook == huskyHooksDir {
continue
}

fmt.Println(hook)

// copy file to .git/hooks
err = os.Link(hook, filepath.Join(gitDir, filepath.Base(hook)))
err = os.Link(hook, filepath.Join(gitHooksDir, filepath.Base(hook)))
if err != nil {
panic(err)
return err
}

// make file executable
err = os.Chmod(filepath.Join(gitDir, filepath.Base(hook)), 0755)
err = os.Chmod(filepath.Join(gitHooksDir, filepath.Base(hook)), 0755)
if err != nil {
panic(err)
return err
}

}
Expand Down
72 changes: 72 additions & 0 deletions internal/lib/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package lib

import (
"os"
)

const (
huskyHooksDir = ".husky/hooks"
gitHooksDir = ".git/hooks"
)

// git hooks currently supported
var validHooks = []string{
"applypatch-msg",
"commit-msg",
"fsmonitor-watchman",
"post-checkout",
"post-update",
"pre-applypatch",
"pre-commit",
"pre-push",
"pre-rebase",
"prepare-commit-msg",
"update",
"pre-receive",
"pre-merge-commit",
"push-to-checkout",
}

// contains will return true if str exists in s
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}

return false
}

// isValidHook will call the contains function internally.
func isValidHook(hook string) bool {
return contains(validHooks, hook)
}

// gitExists will return true if the comman is executed under .git directory
// TODO: support recursive find .git directory till home
func gitExists() (bool, error) {
// check if .git exists
_, err := os.Stat(".git")
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}

return true, nil
}

// huskyExists will return true if exists, otherwise false
// TODO: support recursive find .husky directory till home
func huskyExists() (bool, error) {
_, err := os.Stat(".husky")

if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}

return true, nil
}