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 a1c6784..d8aa43b 100644 --- a/ready.go +++ b/ready.go @@ -2,6 +2,7 @@ package main import ( "errors" + "flag" "fmt" "log" "os" @@ -20,13 +21,16 @@ type ( Name string `yaml:"name"` } config struct { + Path string Tasks []task `yaml:"tasks"` } ) func main() { - if len(os.Args) > 1 && os.Args[1] == "init" { - err := installHook() + 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) } @@ -36,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) } @@ -77,9 +81,9 @@ 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" - _, 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]") @@ -92,6 +96,12 @@ func installHook() 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 @@ -115,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) } @@ -147,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:]...)