Skip to content

Commit

Permalink
feat: add install cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostiantyn Kostikov committed Dec 1, 2024
1 parent 70efd97 commit 1d01687
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 11 deletions.
Empty file modified .hooky/git-hooks/pre-commit
100755 → 100644
Empty file.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
# Hooky CLI [WIP]

Hooky CLI is a command-line tool for managing Git hooks in Go projects. It helps developers easily set up, configure, and run Git hooks, such as pre-commit hooks, in a Go-centric environment.
**Hooky CLI** is a command-line tool for managing Git hooks in Go projects. Designed with simplicity and efficiency in mind, Hooky makes it easy to set up, configure, and run Git hooks such as pre-commit, pre-push, and more. Whether you’re enforcing coding standards, running tests, or automating tasks, Hooky streamlines the process in a Go-centric development workflow.

## Features
- **Manage Git Hooks**: Create, configure, and manage Git hooks like pre-commit, commit-msg, and more.
- **Cross-Platform**: Works on Linux, macOS, and Windows.
- **Easy Hook Management**: Install and configure Git hooks effortlessly.
- **Go-Friendly**: Optimized for Go projects and workflows.
- **Customizable Hooks**: Define and execute your custom Git hooks.
- **Seamless Integration**: Works out of the box with Git repositories.

## Installation
To install Hooky CLI, run the following command:

```bash
go install github.com/kostikovk/hooky@latest
```
**Note:** Replace @latest with a specific version if you want to install a particular release.

## Usage
To get started with Hooky CLI, run the following command:

```bash
hooky init
```
This command initializes Hooky in your Git repository and creates a `.hooky` directory with default hooks.

## Install specific hook
To install a specific hook, run the following command:

```bash
hooky install pre-commit
```
This command installs the `pre-commit` hook in your Git repository.
If it's already installed, it will overwrite the existing hook.
18 changes: 18 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/kostikovk/hooky/lib"
"github.com/spf13/cobra"
)

var installCmd = &cobra.Command{
Use: "install [hook]",
Short: "Install a specific Git hook",
Long: `Install a specific Git hook, such as pre-commit or pre-push, and set up the necessary scripts to execute the hook logic.`,
Args: cobra.ExactArgs(1),
Run: lib.RunInstall,
}

func init() {
rootCmd.AddCommand(installCmd)
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Version is the version of the CLI.
var Version string = "v0.0.1"
const Version string = "v1.0.0"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
5 changes: 0 additions & 5 deletions helpers/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ func IsGitRepository() bool {
return dirExists(AbsoluteGitPath)
}

// HasGitHooksDirectory checks if the current directory has a .git/hooks folder.
func HasGitHooksDirectory() bool {
return dirExists(AbsoluteGitHooksPath)
}

// InitGit initializes a Git repository.
func InitGit() error {
cmd := exec.Command("git", "init")
Expand Down
21 changes: 20 additions & 1 deletion helpers/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func IsHookyRepository() bool {
return dirExists(AbsoluteHookyPath)
}

func HasGitHooksDirectory() bool {
return dirExists(AbsoluteHookyGitHooksPath)
}

// CreateHookyGitDirectory creates a .hooky/ folder.
func CreateHookyGitDirectory() error {
return os.MkdirAll(AbsoluteHookyGitHooksPath, 0750)
Expand All @@ -26,12 +30,27 @@ func DeleteHookyDirectory() error {

// CreateGitHook creates a Hooky Git hook.
func CreateGitHook(hook string, cmd string) error {
// check if Hooky repository exists.
if !IsHookyRepository() {
fmt.Println("Hooky repository not found")
fmt.Println("Please, do 'hooky init' to create a Hooky repository")

return fmt.Errorf("Hooky repository not found")
}

if !HasGitHooksDirectory() {
fmt.Println("Git hooks directory not found in Hooky repository '.hooky/git-hooks'")
fmt.Println("Please, do 'hooky uninstall' and 'hooky init' to create a Hooky repository again")

return fmt.Errorf("Git hooks directory not found in Hooky repository '.hooky/git-hooks'")
}

// check if hook is valid Git hook.
if !GitHookExists(hook) {
return fmt.Errorf("invalid Git hook: %s", hook)
}

// check if GoHooks Git directory exists.
// check if Hooky Git directory exists.
files, err := os.ReadDir(AbsoluteHookyGitHooksPath)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
Expand Down
26 changes: 26 additions & 0 deletions lib/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lib

import (
"fmt"
"os"

"github.com/kostikovk/hooky/helpers"
"github.com/spf13/cobra"
)

func RunInstall(cmd *cobra.Command, args []string) {
installHook(cmd, args[0])
}

func installHook(cmd *cobra.Command, hook string) {
fmt.Printf("Installing %s hook...\n", hook)

err := helpers.CreateGitHook(hook, "# go test ./...")
if err != nil {
cmd.PrintErr(err)

os.Exit(1)
}

cmd.Printf("Hook %s installed.", hook)
}
2 changes: 1 addition & 1 deletion lib/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func RunUninstall(cmd *cobra.Command, args []string) {
cmd.Println("Error uninstalling GoHooks.")
}

cmd.Println("GoHooks uninstalled.")
cmd.Println("Hooky uninstalled :(")
}

func hookyUninstallHandler(cmd *cobra.Command) error {
Expand Down

0 comments on commit 1d01687

Please sign in to comment.