Skip to content
Open
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 29 additions & 7 deletions ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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]")

Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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:]...)
Expand Down