From bb00305fba691ddf4e0dfe96a686fe125be92862 Mon Sep 17 00:00:00 2001 From: Jean-Thierry BONHOMME Date: Mon, 25 Oct 2021 18:11:00 +0200 Subject: [PATCH 1/3] feat(closes #2): add a second parameter to init command --- ready.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ready.go b/ready.go index a1c6784..1dda510 100644 --- a/ready.go +++ b/ready.go @@ -24,9 +24,18 @@ type ( } ) +const ( + DefaultGitHookPath string = "./.git/hooks/pre-commit" +) + func main() { if len(os.Args) > 1 && os.Args[1] == "init" { - err := installHook() + hook := DefaultGitHookPath + if len(os.Args) > 2 { + hook = os.Args[2] + } + fmt.Printf("hooks: %s\n", hook) + err := installHook(hook) if err != nil { log.Fatalf("Error installing hook: %v\n", err) } @@ -77,8 +86,7 @@ func main() { fmt.Printf("All tasks completed successfully in %v ✨\n\n", time.Since(start).Round(time.Millisecond)) } -func installHook() error { - hook := "./.git/hooks/pre-commit" +func installHook(hook string) error { _, err := os.Open(hook) if err == nil { fmt.Println("A pre-commit hook already exists ℹ️ Do you want to overwrite it? [yes/no]") From 2c2d2c307a157bbbedc9d5537f427a2bd7a568cd Mon Sep 17 00:00:00 2001 From: Jean-Thierry BONHOMME Date: Mon, 25 Oct 2021 18:14:25 +0200 Subject: [PATCH 2/3] fix: remove printf --- ready.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ready.go b/ready.go index 1dda510..0736e89 100644 --- a/ready.go +++ b/ready.go @@ -34,7 +34,6 @@ func main() { if len(os.Args) > 2 { hook = os.Args[2] } - fmt.Printf("hooks: %s\n", hook) err := installHook(hook) if err != nil { log.Fatalf("Error installing hook: %v\n", err) From 5fc6153f1f32902f0ff2140ee09151ba9c925599 Mon Sep 17 00:00:00 2001 From: Lewis Llobera Date: Mon, 25 Oct 2021 23:45:36 +0200 Subject: [PATCH 3/3] refactor: use flags --- README.md | 10 ++++++++-- ready.go | 43 +++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 694bec3..3cc0dcc 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,19 @@ Or download the binary from the [releases page](https://github.com/lewislbr/read 2. Install hook -> Must be run in the repository root path (where the folder `.git` is located). +> Must be run in the repository root path (where the `.git` folder is located). ```sh ready init ``` -This will check for any existing pre-commit hook, and if found, it will prompt to override it or abort the process. If no hook is found, a new one with execution rights will be created. +By default, this will check for any existing pre-commit hook in `./.git/hooks/pre-commit`, and if found, it will prompt to override it or abort the process. If no hook is found, a new one with execution rights will be created. + +A custom installation path may be provided to the `init` command with the `-path` flag: + +```sh +ready -path=foo/bar init # This will install the hook in `./foo/bar/pre-commit`, creating `./foo/bar` if not exists +``` 3. Create tasks file diff --git a/ready.go b/ready.go index 0736e89..d8aa43b 100644 --- a/ready.go +++ b/ready.go @@ -2,6 +2,7 @@ package main import ( "errors" + "flag" "fmt" "log" "os" @@ -20,21 +21,16 @@ type ( Name string `yaml:"name"` } config struct { + Path string Tasks []task `yaml:"tasks"` } ) -const ( - DefaultGitHookPath string = "./.git/hooks/pre-commit" -) - func main() { - if len(os.Args) > 1 && os.Args[1] == "init" { - hook := DefaultGitHookPath - if len(os.Args) > 2 { - hook = os.Args[2] - } - err := installHook(hook) + cfg := newConfig().withFlags() + + if flag.NArg() > 0 && flag.Args()[0] == "init" { + err := installHook(cfg.Path) if err != nil { log.Fatalf("Error installing hook: %v\n", err) } @@ -44,7 +40,7 @@ func main() { os.Exit(0) } - cfg, err := newConfig().withYAML() + cfg, err := cfg.withYAML() if err != nil { log.Fatalf("Failed to get config: %v\n", err) } @@ -85,8 +81,9 @@ func main() { fmt.Printf("All tasks completed successfully in %v ✨\n\n", time.Since(start).Round(time.Millisecond)) } -func installHook(hook string) error { - _, err := os.Open(hook) +func installHook(path string) error { + hook := "/pre-commit" + _, err := os.Open(path + hook) if err == nil { fmt.Println("A pre-commit hook already exists ℹ️ Do you want to overwrite it? [yes/no]") @@ -99,6 +96,12 @@ func installHook(hook string) error { os.Exit(0) } } + if err != nil { + err := os.MkdirAll(path, os.ModePerm) + if err != nil { + return fmt.Errorf("creating folder: %w", err) + } + } content := []byte(` #!/bin/sh @@ -122,7 +125,7 @@ fi exit 0 `) - err = os.WriteFile(hook, content, 0o755) + err = os.WriteFile(path+hook, content, 0o755) if err != nil { return fmt.Errorf("creating file: %w", err) } @@ -154,6 +157,18 @@ func (c *config) withYAML() (*config, error) { return c, nil } +func (c *config) withFlags() *config { + path := flag.String("path", "./.git/hooks", "Path where to install the git hook.") + + flag.Parse() + + if *path != "" { + c.Path = *path + } + + return c +} + func runTask(t task) (string, error) { parts := strings.Split(t.Command, " ") cmd := exec.Command(parts[0], parts[1:]...)